Reputation: 179
Here is zoom working example. I'd like the image to be zoomed on actual mouse position over image. How to achieve this? No libraries.
const image = document.querySelector('img');
const zoom = {
min: 1,
max: 3,
value: 1,
step: 0.1
};
image.addEventListener('wheel', event => {
event.preventDefault();
if (event.deltaY < 0) {
zoom.value = zoom.value >= zoom.max ? zoom.max : zoom.value + zoom.step;
} else if (event.deltaY > 0) {
zoom.value = zoom.value <= zoom.min ? zoom.min : zoom.value - zoom.step;
}
image.style.transform = `scale(${zoom.value})`
}
)
div {
width: 500px;
overflow: hidden;
}
img {
width: 100%;
}
<div>
<img src="https://picjumbo.com/wp-content/uploads/free-stock-photos-san-francisco-1080x720.jpg">
</div>
Upvotes: 3
Views: 1173
Reputation: 1903
const image = document.querySelector('img');
const zoom = {
min: 1,
max: 3,
value: 1,
step: 0.1
};
var div = document.querySelector('div');
div.addEventListener('wheel', event => {
event.preventDefault();
if (event.deltaY < 0) {
zoom.value = zoom.value >= zoom.max ? zoom.max : zoom.value + zoom.step;
} else if (event.deltaY > 0) {
zoom.value = zoom.value <= zoom.min ? zoom.min : zoom.value - zoom.step;
}
image.style.transform = `scale(${zoom.value})`
var xPerc = (x * 100) / image.width;
var yPerc = (y * 100) / image.height;
image.style.transformOrigin = xPerc + '%' + ' ' + yPerc + '%';
}
)
var x, y;
div.addEventListener('mousemove', e => {
x = e.clientX - div.offsetLeft;
y = e.clientY - div.offsetTop;
})
div {
width: 500px;
overflow: hidden;
}
img {
width: 100%;
}
<div>
<img src="https://picsum.photos/200">
</div>
Played a little bit with your example and found out transform-origin
might do the trick. Added coordinates x,y relative to image's container and converted them to a percentage in order to set image's tranform-origin
style property.
Upvotes: 2
Reputation: 92427
Change you image.style.transform...
to (500x337 is div-image size)
let x = (-event.offsetX+500/2)*(zoom.value-1);
let y = (-event.offsetY+337/2)*(zoom.value-1);
image.style.transform = `translate(${x}px,${y}px) scale(${zoom.value}) `
const image = document.querySelector('img');
const zoom = {
min: 1,
max: 3,
value: 1,
step: 0.1
};
image.addEventListener('wheel', event => {
event.preventDefault();
if (event.deltaY < 0) {
zoom.value = zoom.value >= zoom.max ? zoom.max : zoom.value + zoom.step;
} else if (event.deltaY > 0) {
zoom.value = zoom.value <= zoom.min ? zoom.min : zoom.value - zoom.step;
}
let x = (-event.offsetX+500/2)*(zoom.value-1);
let y = (-event.offsetY+337/2)*(zoom.value-1);
image.style.transform = `translate(${x}px,${y}px) scale(${zoom.value}) `
})
div {
width: 500px;
overflow: hidden;
}
img {
width: 100%;
}
<div>
<img src="https://picjumbo.com/wp-content/uploads/free-stock-photos-san-francisco-1080x720.jpg">
</div>
Upvotes: 0
Reputation: 5895
add
image.style.transformOrigin = `${event.x}px ${event.y}px`
const image = document.querySelector('img');
const zoom = {
min: 1,
max: 3,
value: 1,
step: 0.1,
originX: 0,
originY: 0
};
image.addEventListener('wheel', event => {
event.preventDefault();
zoom.originX += (event.x-zoom.originX)/zoom.value;
zoom.originY += (event.y-zoom.originY)/zoom.value;
image.style.transformOrigin = zoom.originX+'px '+zoom.originY+'px';
if (event.deltaY < 0) {
zoom.value = zoom.value >= zoom.max ? zoom.max : zoom.value + zoom.step;
} else if (event.deltaY > 0) {
zoom.value = zoom.value <= zoom.min ? zoom.min : zoom.value - zoom.step;
}
image.style.transformOrigin = `${event.x}px ${event.y}px`
image.style.transform = `scale(${zoom.value})`
}
)
div {
width: 500px;
overflow: hidden;
}
img {
width: 100%;
}
<div>
<img src="https://picjumbo.com/wp-content/uploads/free-stock-photos-san-francisco-1080x720.jpg">
</div>
Upvotes: 0