Skip to content

Commit ddbc2c2

Browse files
committed
Add full-screen image display feature
1 parent 702b06c commit ddbc2c2

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

assets/scss/_custom.scss

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,3 +1352,40 @@ div.alert > em.javascript-required {
13521352
outline: none;
13531353
padding: .5em 0 .5em 0;
13541354
}
1355+
1356+
/* CSS for 'figure' full-screen display */
1357+
1358+
/* Define styles for full-screen overlay */
1359+
.figure-fullscreen-overlay {
1360+
position: fixed;
1361+
inset: 0;
1362+
z-index: 9999;
1363+
background-color: rgba(255, 255, 255, 0.95); /* White background with some transparency */
1364+
display: flex;
1365+
justify-content: center;
1366+
align-items: center;
1367+
padding: calc(5% + 20px);
1368+
box-sizing: border-box;
1369+
}
1370+
1371+
/* CSS class to scale the image when zoomed */
1372+
.figure-zoomed {
1373+
transform: scale(1.2);
1374+
}
1375+
1376+
/* Define styles for full-screen image */
1377+
.figure-fullscreen-img {
1378+
max-width: 100%;
1379+
max-height: 100%;
1380+
object-fit: contain; /* Maintain aspect ratio and fit within the container */
1381+
}
1382+
1383+
/* Define styles for close button */
1384+
.figure-close-button {
1385+
position: absolute;
1386+
top: 1%;
1387+
right: 2%;
1388+
cursor: pointer;
1389+
font-size: calc(5vw + 10px);
1390+
color: #333;
1391+
}

layouts/partials/head.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
<script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.min.js" integrity="sha384-hHTwgxzjpO1G1NI0wMHWQYUxnGtpWyDjVSZrFnDrlWa5OL+DFY57qnDWw/5WSJOl" crossorigin="anonymous"></script>
100100
{{- end -}}
101101

102+
<!-- Enable zoom-on-click for figures that opt in to this -->
103+
{{- if .HasShortcode "figure" -}}
104+
<script defer src="{{ "js/zoom.js" | relURL }}"></script>
105+
{{- end -}}
106+
102107
{{- if eq (lower .Params.cid) "community" -}}
103108
{{- if eq .Params.community_styles_migrated true -}}
104109
<link href="/css/community.css" rel="stylesheet"><!-- legacy styles -->

layouts/shortcodes/figure.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{{ $src := (.Page.Resources.GetMatch (printf "**%s*" (.Get "src"))) }}
2+
{{ $clickToZoom := .Get "clicktozoom" }}
3+
<figure{{ with .Get "class" }} class="{{ . }} {{ if $clickToZoom }}clickable-zoom{{ end }}"{{ end }}>
4+
{{- if .Get "link" -}}
5+
<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
6+
{{- end }}
7+
<img src="{{ if $src }}{{ $src.RelPermalink }}{{ else }}{{ .Get "src" }}{{ end }}"
8+
{{- if or (.Get "alt") (.Get "caption") }}
9+
alt="{{ with .Get "alt" }}{{ . }}{{ else }}{{ .Get "caption" | markdownify| plainify }}{{ end }}"
10+
{{- end -}}
11+
{{- with .Get "width" }} width="{{ . }}"{{ end -}}
12+
{{- with .Get "height" }} height="{{ . }}"{{ end -}}
13+
/> <!-- Closing img tag -->
14+
{{- if .Get "link" }}</a>{{ end -}}
15+
{{- if or (or (.Get "title") (.Get "caption")) (.Get "attr") -}}
16+
<figcaption>
17+
{{ with (.Get "title") -}}
18+
<h4>{{ . }}</h4>
19+
{{- end -}}
20+
{{- if or (.Get "caption") (.Get "attr") -}}<p>
21+
{{- .Get "caption" | markdownify -}}
22+
{{- with .Get "attrlink" }}
23+
<a href="{{ . }}">
24+
{{- end -}}
25+
{{- .Get "attr" | markdownify -}}
26+
{{- if .Get "attrlink" }}</a>{{ end }}</p>
27+
{{- end }}
28+
</figcaption>
29+
{{- end }}
30+
</figure>

static/js/zoom.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// The page and script is loaded successfully
2+
$(document).ready(function() {
3+
// Function to handle hover over <figure> elements
4+
function handleFigureHover() {
5+
// Only change cursor to zoom-in if figure has 'clickable-zoom' class
6+
if ($(this).hasClass('clickable-zoom') && !$(this).hasClass('figure-fullscreen-content')) {
7+
$(this).css('cursor', 'zoom-in');
8+
}
9+
}
10+
11+
// Attach hover event to <figure> elements with 'clickable-zoom' class
12+
$('figure.clickable-zoom').hover(handleFigureHover, function() {
13+
// Mouse out - revert cursor back to default
14+
$(this).css('cursor', 'default');
15+
});
16+
17+
// Attach click event to <figure> elements with 'clickable-zoom' class
18+
$('figure.clickable-zoom').click(function() {
19+
var $figure = $(this);
20+
21+
// Check if the figure has 'clickable-zoom' class
22+
if ($figure.hasClass('clickable-zoom')) {
23+
var $img = $figure.find('img'); // Get the <img> element within the clicked <figure>
24+
25+
// Toggle 'figure-zoomed' class to scale the image
26+
$img.toggleClass('figure-zoomed');
27+
28+
// Create a full-screen overlay
29+
var $fullscreenOverlay = $('<div class="figure-fullscreen-overlay"></div>');
30+
31+
// Clone the <img> element to display in full-screen
32+
var $fullscreenImg = $img.clone();
33+
$fullscreenImg.addClass('figure-fullscreen-img');
34+
35+
// Append the full-screen image to the overlay
36+
$fullscreenOverlay.append($fullscreenImg);
37+
38+
// Create a close button for the full-screen overlay
39+
var $closeButton = $('<span class="figure-close-button">&times;</span>');
40+
$closeButton.click(function() {
41+
// Remove the full-screen overlay when close button is clicked
42+
$fullscreenOverlay.remove();
43+
$('body').css('overflow', 'auto'); // Restore scrolling to the underlying page
44+
45+
// Remove 'figure-zoomed' class to reset image scale
46+
$img.removeClass('figure-zoomed');
47+
});
48+
$fullscreenOverlay.append($closeButton);
49+
50+
// Append the overlay to the body
51+
$('body').append($fullscreenOverlay);
52+
53+
// Disable scrolling on the underlying page
54+
$('body').css('overflow', 'hidden');
55+
56+
// Close full-screen figure when clicking outside of it (on the overlay)
57+
$fullscreenOverlay.click(function(event) {
58+
if (event.target === this) {
59+
// Clicked on the overlay area (outside the full-screen image)
60+
$fullscreenOverlay.remove();
61+
$('body').css('overflow', 'auto'); // Restore scrolling to the underlying page
62+
63+
// Remove 'figure-zoomed' class to reset image scale
64+
$img.removeClass('figure-zoomed');
65+
}
66+
});
67+
}
68+
});
69+
});

0 commit comments

Comments
 (0)