我设法使鼠标拖动滚动div,但使用鼠标进行缩放不完整。
它能够工作,但是我希望鼠标指针在保持图像位置的同时缩放图像,就像这样:
我需要使用scrollBy()
将滚动返回到缩放之前的点。有人知道怎么做吗?
这是有人制作的一个示例 https://jsfiddle.net/xta2ccdt/13/ ,这正是我所需要的,但该代码使用了translate()
和其他一些不适用于这里的东西,因为我也有滚动/拖动。
这是我的jsfiddle代码:https://jsfiddle.net/catalinu/1f6e0jna/
以下是stackoverflow中的代码:
请帮忙。我已经努力了好几天。
for (const divMain of document.getElementsByClassName('main')) {
// drag the section
for (const divSection of divMain.getElementsByClassName('section')) {
// when mouse is pressed store the current mouse x,y
let previousX, previousY
divSection.addEventListener('mousedown', (event) => {
previousX = event.pageX
previousY = event.pageY
})
// when mouse is moved, scrollBy() the mouse movement x,y
divSection.addEventListener('mousemove', (event) => {
// only do this when the primary mouse button is pressed (event.buttons = 1)
if (event.buttons) {
let dragX = 0
let dragY = 0
// skip the drag when the x position was not changed
if (event.pageX - previousX !== 0) {
dragX = previousX - event.pageX
previousX = event.pageX
}
// skip the drag when the y position was not changed
if (event.pageY - previousY !== 0) {
dragY = previousY - event.pageY
previousY = event.pageY
}
// scrollBy x and y
if (dragX !== 0 || dragY !== 0) {
divMain.scrollBy(dragX, dragY)
}
}
})
}
// zoom in/out on the section
let scale = 1
const scaleFactor = 0.05
divMain.addEventListener('wheel', (event) => {
// preventDefault to stop the onselectionstart event logic
event.preventDefault()
for (const divSection of divMain.getElementsByClassName('section')) {
// set the scale change value
const scaleChange = (event.deltaY < 0) ? scaleFactor : -scaleFactor
// don't allow the scale to go outside of [0,5 - 2]
if (scale + scaleChange < 0.5 || scale + scaleChange > 2) {
return
}
// round the value when using high dpi monitors
scale = Math.round((scale + scaleChange) * 100) / 100
// apply the css scale
divSection.style.transform = `scale(${scale}, ${scale})`
// re-adjust the scrollbars
const x = Math.round(divMain.scrollLeft * scaleChange)
const y = Math.round(divMain.scrollTop * scaleChange)
divMain.scrollBy(x, y)
}
})
}
body {
margin: 0;
}
.main {
width: 100%; /* percentage fixes the X axis white space when zoom out */
height: 100vh; /* this is still an issue where you see white space when zoom out in the Y axis */
overflow: scroll; /* needed for safari to show the x axis scrollbar */
}
.main .section {
width: 200%;
height: 200vh;
background-image: url('https://iso.500px.com/wp-content/uploads/2014/07/big-one.jpg');
transform-origin: 0 0;
}
<main class="main">
<section class="section"></section>
</main>