Javascript 放大/缩小到鼠标 x/y 坐标

2024-05-15

我设法让鼠标拖动来滚动div,但是用鼠标放大/缩小不完整。

它有效,但我希望鼠标指针将图像保持在该位置并同时缩放它,如下所示:

我需要使用scrollBy()将滚动返回到缩放之前的上一点。有人知道该怎么做吗?

这是某人制作的小提琴https://jsfiddle.net/xta2ccdt/13/ https://jsfiddle.net/xta2ccdt/13/这正是我所需要的,但是代码使用translate()以及其他不适用于此处的内容,因为我也有滚动/拖动功能。

这是我的 jsfiddle 代码https://jsfiddle.net/catalinu/1f6e0jna/ 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>

您的问题主要围绕以下几行

const x = Math.round(divMain.scrollLeft * scaleChange)
const y = Math.round(divMain.scrollTop * scaleChange)

带比例滚动的方式如下所示

  • 计算未缩放的x, y缩放发生的坐标
  • 计算新的缩放比例x, y协调我将其与新比例相乘
  • 现在您希望这个新坐标保留在现有坐标所在的位置。所以基本上如果你减去offset x,y从新的scaled x,y,您可以向左和顶部滚动。

更新后的代码如下

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 factor = 0.05
  const max_scale =4

  divMain.addEventListener('wheel', (e) => {
    // preventDefault to stop the onselectionstart event logic
    for (const divSection of divMain.getElementsByClassName('section')) {
        e.preventDefault();
        var delta = e.delta || e.wheelDelta;
        if (delta === undefined) {
          //we are on firefox
          delta = e.originalEvent.detail;
        }
        delta = Math.max(-1,Math.min(1,delta)) // cap the delta to [-1,1] for cross browser consistency
            offset = {x: divMain.scrollLeft, y: divMain.scrollTop};
     image_loc = {
        x: e.pageX + offset.x,
        y: e.pageY + offset.y
     }

     zoom_point = {x:image_loc.x/scale, y: image_loc.y/scale}

        // apply zoom
        scale += delta*factor * scale
        scale = Math.max(1,Math.min(max_scale,scale))

     zoom_point_new = {x:zoom_point.x * scale, y: zoom_point.y * scale}

     newScroll = {
        x: zoom_point_new.x - e.pageX,
        y: zoom_point_new.y - e.pageY
     }


      divSection.style.transform = `scale(${scale}, ${scale})`
      divMain.scrollTop = newScroll.y
      divMain.scrollLeft = newScroll.x
    }


  })
}

更新的小提琴是

https://jsfiddle.net/uy390v8t/1/ https://jsfiddle.net/uy390v8t/1/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript 放大/缩小到鼠标 x/y 坐标 的相关文章

随机推荐

  • 有没有办法阻止 iOS 上的 Safari 在关闭时清除网站的 cookie?

    我的移动网络应用程序的一位用户抱怨说 每次他关闭手机屏幕后使用该应用程序时 他都必须重新登录该应用程序 发生的情况是 当屏幕关闭时 或者当您通过双击主页按钮并滑开 Safari 来完全关闭 Safari 时 Safari 会清除该网站的 C
  • WPF 中按钮的启用和禁用状态的不同图像

    我想根据按钮的状态更改下面代码中按钮的图像 即使用不同的图像来启用和禁用状态
  • 如何在 Google 地图 InfoWindow 中使用 Jquery?

    我想在 Google 地图 InfoWindow 中显示 jQuery 效果 显示和隐藏 div 我该怎么做 尽管我个人还没有尝试过 但这 gt http code google com p jquery ui map http code
  • Facebook SDK 登录/注销 ngrok

    我正在尝试将我正在构建的网络应用程序与 Facebook 集成 Facebook 现在要求所有 API 调用都必须从 https 站点进行 我正在构建的这个应用程序只是为了好玩 所以我使用的是 localhost 我在用着ngrok将我的请
  • 解决方案文件的修改权限

    我有用VS2005开发的项目解决方案 它绑定到 TFS Team Foundation Server 我想将解决方案转换为 VS2008 版本 保持绑定不变 但是在转换时出现错误 错误信息 解决方案文件 C abc abc sln 无法转换
  • Android Studio 模拟器未找到符号:_pwritev

    上次更新 Android 模拟器后 我无法启动任何虚拟设备 即使我创建了一个新的 模拟器显示几秒钟 然后出现错误 并且 macOS 在问题报告中显示 qemu system x86 64 意外退出 Dyld Error Message Sy
  • 带点 (.) 的 Spring MVC @PathVariable 被截断

    这是问题的延续Spring MVC PathVariable 被截断 https stackoverflow com questions 3526523 spring mvc pathvariable getting truncated S
  • IE 10 Javascript 错误未显示[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我刚刚将浏览器IE8升级到IE10 在 IE8 中任何java script发生错误 它显示在状态栏中 但是IE10错误未显示在状态栏中
  • 使用 Qt 上的看门狗更改 QLabel 文本

    我正在尝试做什么 每当在受监视的文件夹中创建新文件 使用 Watchdog 时 更改 QLabel 文本 Problem 使用以下代码创建新文件时 QLabel 文本不会更新 但成功调试打印到控制台 class MainWindow QtW
  • 如何在 Ionic 3 中调整图像大小而不降低质量和目标宽度和高度?

    我想减小相机 API 拍摄的图像的大小 但质量降低不好 最好的办法是降低分辨率 但我不想对所有图像使用目标宽度和高度 例如 我希望图像宽度为 1280 图像高度按其比例自动变化 但在 API 中我应该使用精确的宽度和高度 如何通过图像比例更
  • windows关闭tomcat后保持端口锁定

    我遇到了一个问题 该问题发生在不同站点的 3 台不同服务器上 问题是 当我关闭 Windows 服务器上的 Tomcat 7 和 8 5 版本 服务并尝试重新启动该服务后 该服务将无法启动 因为 tomcat 认为端口仍在使用中 以下是错误
  • 从 Ruby 中的 DateTime 变量获取时间

    我在 ruby 中工作 我有一个包含数据库中今天的日期时间的对象 我只想要时间截断数据 我怎样才能得到那个 Try 日期时间 strftime http www ruby doc org stdlib 1 9 3 libdoc date r
  • 为什么 System.Math 有 Sin、Cos 等外部方法?

    MethodImpl MethodImplOptions InternalCall public static extern double Sin double a 其原因何在 MethodImplOptions InternalCall表
  • 必填字段验证器中的数字验证

    是否可以将数字验证放入 asp net 文本框中的必填字段验证器中 您应该使用比较验证器 http msdn microsoft com en us library system web ui webcontrols comparevali
  • 自动过滤/排序列表框项目 (Windows Phone)

    我想确保添加到列表框中的项目根据每个项目的序列号按升序排序 例如 1 项目 2 项目 4 项目 3 项目应根据其编号自动排序 1 2 3 10 这是 C 源代码 namespace XeroQuiz public partial class
  • 如何使用 webpack 正确捆绑 vscode 扩展

    我遇到的问题是当我跑步时vsce package我仍然明白This extension consists of 3587 separate files For performance reasons you should bundle yo
  • 调试器异常错误和内存超调

    我尝试运行以下代码 但它显示有关内存地址的错误 并显示一条消息 循环后 n 可能未定义 请看一看 var n max integer n integer r R1 f h0 Array of Real const h 0 00889 nip
  • 使用 mosca MQTT 的请求响应模式

    有没有办法使用 mosca MQTT 实现请求响应模式 以 检查客户端的回复 如果在预期时间内未收到预期回复 则重新发布 我相信这在 Mqtt 5 中是可能的 但截至目前 我必须使用 QoS 1 的 Mosca 代理 支持直到 Mqtt 3
  • 错误 11 秒后等待 Protractor 与页面同步超时

    我的量角器测试有问题 我的角度应用程序使用 timeout 不断从服务器轮询某些内容 不幸的是 当我运行测试时 这会导致以下错误消息 There was a webdriver error Error Timed out waiting f
  • Javascript 放大/缩小到鼠标 x/y 坐标

    我设法让鼠标拖动来滚动div 但是用鼠标放大 缩小不完整 它有效 但我希望鼠标指针将图像保持在该位置并同时缩放它 如下所示 我需要使用scrollBy 将滚动返回到缩放之前的上一点 有人知道该怎么做吗 这是某人制作的小提琴https jsf