-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add logic for "zoom on hover" setting
- Loading branch information
Showing
3 changed files
with
36 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,37 @@ | ||
const images = document.querySelectorAll('.product__media-image'); | ||
|
||
const magnify = (image, scale) => { | ||
let glass, w, h, bw; | ||
|
||
glass = document.createElement("div"); | ||
glass.setAttribute("class", "img-magnifier-glass"); | ||
image.parentElement.insertBefore(glass, image); | ||
|
||
glass.addEventListener('click', () => { | ||
glass.remove(); | ||
}); | ||
|
||
/* Set background properties for the magnifier glass: */ | ||
glass.style.backgroundImage = "url('" + image.src + "')"; | ||
glass.style.backgroundRepeat = "no-repeat"; | ||
glass.style.backgroundSize = (image.width * scale) + "px " + (image.height * scale) + "px"; | ||
bw = 3; | ||
w = glass.offsetWidth / 2; | ||
h = glass.offsetHeight / 2; | ||
|
||
/* Execute a function when someone moves the magnifier glass over the image: */ | ||
glass.addEventListener("mousemove", moveMagnifier); | ||
image.addEventListener("mousemove", moveMagnifier); | ||
|
||
function moveMagnifier(e) { | ||
var pos, x, y; | ||
/* Prevent any other actions that may occur when moving over the image */ | ||
e.preventDefault(); | ||
/* Get the cursor's x and y positions: */ | ||
pos = getCursorPos(e); | ||
x = pos.x; | ||
y = pos.y; | ||
/* Prevent the magnifier glass from being positioned outside the image: */ | ||
if (x > image.width - (w / scale)) {x = image.width - (w / scale);} | ||
if (x < w / scale) {x = w / scale;} | ||
if (y > image.height - (h / scale)) {y = image.height - (h / scale);} | ||
if (y < h / scale) {y = h / scale;} | ||
/* Set the position of the magnifier glass: */ | ||
glass.style.left = (x - w) + "px"; | ||
glass.style.top = (y - h) + "px"; | ||
/* Display what the magnifier glass "sees": */ | ||
glass.style.backgroundPosition = "-" + ((x * scale) - w + bw) + "px -" + ((y * scale) - h + bw) + "px"; | ||
} | ||
|
||
function getCursorPos(e) { | ||
var a, x = 0, y = 0; | ||
e = e || window.event; | ||
/* Get the x and y positions of the image: */ | ||
a = image.getBoundingClientRect(); | ||
/* Calculate the cursor's x and y coordinates, relative to the image: */ | ||
x = e.pageX - a.left; | ||
y = e.pageY - a.top; | ||
/* Consider any page scrolling: */ | ||
x = x - window.pageXOffset; | ||
y = y - window.pageYOffset; | ||
return {x : x, y : y}; | ||
} | ||
// create a container and set the full-size image as its background | ||
const createOverlay = (image) => { | ||
overlay = document.createElement('div'); | ||
overlay.setAttribute('class', 'image--full-size'); | ||
overlay.setAttribute('aria-hidden', 'true'); | ||
overlay.style.backgroundImage = `url('${image.src}')`; | ||
return overlay; | ||
}; | ||
|
||
const moveWithHover = (image, event, zoomRatio) => { | ||
// calculate mouse position | ||
const ratio = image.height / image.width; | ||
const container = event.target.getBoundingClientRect(); | ||
const xPosition = event.clientX - container.left; | ||
const yPosition = event.clientY - container.top; | ||
const xPercent = `${xPosition / (overlay.clientWidth / 100)}%`; | ||
const yPercent = `${yPosition / ((overlay.clientWidth * ratio) / 100)}%`; | ||
|
||
// determine what to show in the frame | ||
overlay.style.backgroundPosition = `${xPercent} ${yPercent}`; | ||
overlay.style.backgroundSize = `${image.width * zoomRatio}px`; | ||
}; | ||
|
||
const magnify = (image, zoomRatio) => { | ||
// add full-size image on top of original | ||
const overlay = createOverlay(image); | ||
image.parentElement.insertBefore(overlay, image); | ||
|
||
overlay.onclick = () => overlay.remove(); | ||
overlay.onmousemove = (event) => moveWithHover(image, event, zoomRatio); | ||
} | ||
|
||
const images = document.querySelectorAll('.image--original'); | ||
|
||
images.forEach(image => { | ||
image.addEventListener('click', () => { | ||
magnify(image, 2); | ||
}); | ||
image.onclick = () => magnify(image, 3); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters