简体   繁体   English

使用鼠标滚轮缩放页面,单击并拖动以平移

[英]Zoom page with mouse wheel, click and drag to pan

How would I go about making a scroll-and-pan effect with the mouse wheel for the entire page?我 go 如何使用鼠标滚轮为整个页面制作滚动和平移效果? Basically like any 2D editor with the scroll to zoom, click and drag to pan but for the entire body.基本上就像任何 2D 编辑器一样,滚动可缩放,单击并拖动可平移,但适用于整个身体。

I haven't been able to find anything online about it.我一直无法在网上找到任何关于它的信息。

If you don't know where to start, you will very soon hit another wall, because knowing where to start is the easy part.如果您不知道从哪里开始,您很快就会碰壁,因为知道从哪里开始是比较容易的部分。 Here is a rough guide:这是一个粗略的指南:

  1. Add event listener to the wheel event (learn how]( https://stackoverflow.com/a/51276012/104380 ))将事件侦听器添加到wheel事件(了解如何]( https://stackoverflow.com/a/51276012/104380 ))
  2. Start with an initial (current) zoom value and calculate the delta from the wheel event and update your zoom value.从初始(当前) zoom值开始,计算滚轮事件的增量并更新zoom值。
  3. Use the zoom value to manipulate the scale of the page in some form.使用zoom值以某种形式操纵页面的比例。 You can use CSS transform: scale() for that, on the body element.您可以为此在body元素上使用 CSS transform: scale()
  4. Add event listener for the mousemove event (learn how]( https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event ))mousemove事件添加事件侦听器(了解如何]( https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event ))
  5. Act upon the detected mouse move, and calculate how much you need to pan the zoomed body element in order to reach any of the 4 edges, and not beyond.根据检测到的鼠标移动采取行动,并计算您需要平移缩放的body元素多少才能到达 4 个边缘中的任何一个,而不是超出。 panning may be done via CSS transform: translate(x,y)可以通过 CSS transform: translate(x,y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM