javascript - 如何根据 Jquery 中的鼠标位置拖动子对象?

标签 javascript jquery html css jquery-ui

我有一张可拖动的大 map ,可以查看 map 上的所有地点。但是我希望能够根据鼠标位置拖动它。

Jquery中如何根据鼠标位置拖动子对象?

这是我的拖动代码:

$(".map").draggable({
containment:[(parseInt($(".hk").width()) - parseInt($(".hkc").width())) * -1, 
(parseInt($(".hk").height()) - parseInt($(".hkc").height())) * -1, 0, 0], 
cursor: "move"
})

我的演示是 here

这是截图:

enter image description here

enter image description here

enter image description here

最佳答案

    // Variables for current position
var x, y;

function handleMouse(e) {
  // Verify that x and y already have some value
  if (x && y) {
    // Scroll window by difference between current and previous positions
    window.scrollBy(e.clientX - x, e.clientY - y);
  }

  // Store current position
  x = e.clientX;
  y = e.clientY;
}

// Assign handleMouse to mouse movement events
document.onmousemove = handleMouse;

关于javascript - 如何根据 Jquery 中的鼠标位置拖动子对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26330029/

相关文章:

javascript - 带有 javascript 变量的多个 css 类

javascript - Visual Studio 12和正则表达式错误

javascript - 1 个下拉菜单、2 个 radio 、2 个输入

javascript - 如何使用 Javascript、CSS 和 HTML 加速移动设备的滑动和滚动?

html - 如何将 CSS 属性从一个 div 复制到另一个?

javascript - 在变量JS中使用onclick ="myFunction()"

java - 查看不同输出格式时的内容协商

javascript - 提交表单(以过滤内容)后,结果表将显示在同一页面上。我怎么做?

php - Bootstrap Grid 有时从新行的右侧开始

jquery - 带有按钮的简单 AddClass 和 RemoveClass,jQuery 脚本