Skip to content

Commit bf09672

Browse files
committed
fix: sometimes the original image doesn't get restored after hover out
1 parent f946994 commit bf09672

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,25 +2510,17 @@ function RemoteFunctions(config = {}) {
25102510
img.alt = image.alt_text || 'Unsplash image';
25112511
img.loading = 'lazy';
25122512

2513-
// this is the original image, we store it so that we can show new images on hover
2514-
const originalImageSrc = this.element.src;
2515-
// we also store its dimensions to show the new image with the same dimension
2516-
const computedStyle = window.getComputedStyle(this.element);
2517-
const originalWidth = computedStyle.width;
2518-
const originalHeight = computedStyle.height;
2519-
const originalObjectFit = computedStyle.objectFit;
2520-
25212513
// show hovered image along with dimensions
25222514
thumbDiv.addEventListener('mouseenter', () => {
2523-
this.element.style.width = originalWidth;
2524-
this.element.style.height = originalHeight;
2525-
this.element.style.objectFit = originalObjectFit || 'cover';
2515+
this.element.style.width = this._originalImageStyle.width;
2516+
this.element.style.height = this._originalImageStyle.height;
2517+
this.element.style.objectFit = this._originalImageStyle.objectFit || 'cover';
25262518
this.element.src = image.url || image.thumb_url;
25272519
});
25282520

25292521
// show original image when hover ends
25302522
thumbDiv.addEventListener('mouseleave', () => {
2531-
this.element.src = originalImageSrc;
2523+
this.element.src = this._originalImageSrc;
25322524
});
25332525

25342526
// attribution overlay, we show this only in the image ribbon gallery
@@ -2671,6 +2663,15 @@ function RemoteFunctions(config = {}) {
26712663
create: function() {
26722664
this.remove(); // remove existing ribbon if already present
26732665

2666+
// when image ribbon gallery is created we get the original image along with its dimensions
2667+
// so that on hover in we can show the hovered image and on hover out we can restore the original image
2668+
this._originalImageSrc = this.element.src;
2669+
this._originalImageStyle = {
2670+
width: window.getComputedStyle(this.element).width,
2671+
height: window.getComputedStyle(this.element).height,
2672+
objectFit: window.getComputedStyle(this.element).objectFit
2673+
};
2674+
26742675
this._style();
26752676
window.document.body.appendChild(this.body);
26762677
this._attachEventHandlers();

0 commit comments

Comments
 (0)