Skip to content

Commit

Permalink
Add full-screen image display feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dipesh-rawat committed May 9, 2024
1 parent 702b06c commit ddbc2c2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
37 changes: 37 additions & 0 deletions assets/scss/_custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1352,3 +1352,40 @@ div.alert > em.javascript-required {
outline: none;
padding: .5em 0 .5em 0;
}

/* CSS for 'figure' full-screen display */

/* Define styles for full-screen overlay */
.figure-fullscreen-overlay {
position: fixed;
inset: 0;
z-index: 9999;
background-color: rgba(255, 255, 255, 0.95); /* White background with some transparency */
display: flex;
justify-content: center;
align-items: center;
padding: calc(5% + 20px);
box-sizing: border-box;
}

/* CSS class to scale the image when zoomed */
.figure-zoomed {
transform: scale(1.2);
}

/* Define styles for full-screen image */
.figure-fullscreen-img {
max-width: 100%;
max-height: 100%;
object-fit: contain; /* Maintain aspect ratio and fit within the container */
}

/* Define styles for close button */
.figure-close-button {
position: absolute;
top: 1%;
right: 2%;
cursor: pointer;
font-size: calc(5vw + 10px);
color: #333;
}
5 changes: 5 additions & 0 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.min.js" integrity="sha384-hHTwgxzjpO1G1NI0wMHWQYUxnGtpWyDjVSZrFnDrlWa5OL+DFY57qnDWw/5WSJOl" crossorigin="anonymous"></script>
{{- end -}}

<!-- Enable zoom-on-click for figures that opt in to this -->
{{- if .HasShortcode "figure" -}}
<script defer src="{{ "js/zoom.js" | relURL }}"></script>
{{- end -}}

{{- if eq (lower .Params.cid) "community" -}}
{{- if eq .Params.community_styles_migrated true -}}
<link href="/css/community.css" rel="stylesheet"><!-- legacy styles -->
Expand Down
30 changes: 30 additions & 0 deletions layouts/shortcodes/figure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{ $src := (.Page.Resources.GetMatch (printf "**%s*" (.Get "src"))) }}
{{ $clickToZoom := .Get "clicktozoom" }}
<figure{{ with .Get "class" }} class="{{ . }} {{ if $clickToZoom }}clickable-zoom{{ end }}"{{ end }}>
{{- if .Get "link" -}}
<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
{{- end }}
<img src="{{ if $src }}{{ $src.RelPermalink }}{{ else }}{{ .Get "src" }}{{ end }}"
{{- if or (.Get "alt") (.Get "caption") }}
alt="{{ with .Get "alt" }}{{ . }}{{ else }}{{ .Get "caption" | markdownify| plainify }}{{ end }}"
{{- end -}}
{{- with .Get "width" }} width="{{ . }}"{{ end -}}
{{- with .Get "height" }} height="{{ . }}"{{ end -}}
/> <!-- Closing img tag -->
{{- if .Get "link" }}</a>{{ end -}}
{{- if or (or (.Get "title") (.Get "caption")) (.Get "attr") -}}
<figcaption>
{{ with (.Get "title") -}}
<h4>{{ . }}</h4>
{{- end -}}
{{- if or (.Get "caption") (.Get "attr") -}}<p>
{{- .Get "caption" | markdownify -}}
{{- with .Get "attrlink" }}
<a href="{{ . }}">
{{- end -}}
{{- .Get "attr" | markdownify -}}
{{- if .Get "attrlink" }}</a>{{ end }}</p>
{{- end }}
</figcaption>
{{- end }}
</figure>
69 changes: 69 additions & 0 deletions static/js/zoom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// The page and script is loaded successfully
$(document).ready(function() {
// Function to handle hover over <figure> elements
function handleFigureHover() {
// Only change cursor to zoom-in if figure has 'clickable-zoom' class
if ($(this).hasClass('clickable-zoom') && !$(this).hasClass('figure-fullscreen-content')) {
$(this).css('cursor', 'zoom-in');
}
}

// Attach hover event to <figure> elements with 'clickable-zoom' class
$('figure.clickable-zoom').hover(handleFigureHover, function() {
// Mouse out - revert cursor back to default
$(this).css('cursor', 'default');
});

// Attach click event to <figure> elements with 'clickable-zoom' class
$('figure.clickable-zoom').click(function() {
var $figure = $(this);

// Check if the figure has 'clickable-zoom' class
if ($figure.hasClass('clickable-zoom')) {
var $img = $figure.find('img'); // Get the <img> element within the clicked <figure>

// Toggle 'figure-zoomed' class to scale the image
$img.toggleClass('figure-zoomed');

// Create a full-screen overlay
var $fullscreenOverlay = $('<div class="figure-fullscreen-overlay"></div>');

// Clone the <img> element to display in full-screen
var $fullscreenImg = $img.clone();
$fullscreenImg.addClass('figure-fullscreen-img');

// Append the full-screen image to the overlay
$fullscreenOverlay.append($fullscreenImg);

// Create a close button for the full-screen overlay
var $closeButton = $('<span class="figure-close-button">&times;</span>');
$closeButton.click(function() {
// Remove the full-screen overlay when close button is clicked
$fullscreenOverlay.remove();
$('body').css('overflow', 'auto'); // Restore scrolling to the underlying page

// Remove 'figure-zoomed' class to reset image scale
$img.removeClass('figure-zoomed');
});
$fullscreenOverlay.append($closeButton);

// Append the overlay to the body
$('body').append($fullscreenOverlay);

// Disable scrolling on the underlying page
$('body').css('overflow', 'hidden');

// Close full-screen figure when clicking outside of it (on the overlay)
$fullscreenOverlay.click(function(event) {
if (event.target === this) {
// Clicked on the overlay area (outside the full-screen image)
$fullscreenOverlay.remove();
$('body').css('overflow', 'auto'); // Restore scrolling to the underlying page

// Remove 'figure-zoomed' class to reset image scale
$img.removeClass('figure-zoomed');
}
});
}
});
});

0 comments on commit ddbc2c2

Please sign in to comment.