用mousedrag替换mousemove?

标题解释了这一切。我在提供拖动事件时遇到了问题。我尝试用mousedown替换mousemove,可能会被mousedown激发一次然后稳定下来,但它会帮助。如何以编程方式执行此操作。

如果您想引用我想要操作的代码,您可以参考此代码。请忽略它。

 $(document).ready(function () { $(".toolImage").mouseover(function () { $(this).parent(".toolTip").find(".toolTipDesc").css("display", "block"); }); $(".toolImage").mouseleave(function () { $(this).parent(".toolTip").find(".toolTipDesc").css("display", "none"); }); var native_width = 0; var native_height = 0; //Now the mousemove function $(".magnify").mousemove(function (e) { if (!native_width && !native_height) { //This will create a new image object with the same image as that in .small //We cannot directly get the dimensions from .small because of the //width specified to 200px in the html. To get the actual dimensions we have //created this image object. var image_object = new Image(); image_object.src = $(".small").attr("src"); native_width = image_object.width; native_height = image_object.height; } else { var magnify_offset = $(this).offset(); var mx = e.pageX - magnify_offset.left; var my = e.pageY - magnify_offset.top; //Finally the code to fade out the glass if the mouse is outside the container if (mx < $(this).width() && my  0 && my > 0) { $(".large").fadeIn(100); } else { $(".large").fadeOut(100); } if ($(".large").is(":visible")) { var rx = Math.round(mx / $(".small").width() * native_width - $(".large").width() / 2) * -1; var ry = Math.round(my / $(".small").height() * native_height - $(".large").height() / 2) * -1; var bgp = rx + "px " + ry + "px"; //Time to move the magnifying glass with the mouse var px = (mx - $(".large").width() / 2); var py = (my - $(".large").height() / 2); $(".large").css({ left: px, top: py + 10, backgroundPosition: bgp }); } } }) $('.t1').mouseenter(function () { $('.pointerTxt').fadeIn(); }); $('.t1').mouseleave(function () { $('.pointerTxt').fadeOut(); }); });  

只有在按下鼠标按钮时,你想在你的mousemove()做所有事情,对吧?

 $(document).ready(function() { var lbDown = false; $(".magnify").mousedown(function(e) { if (e.which === 1) { lbDown = true; } }); $(".magnify").mouseup(function(e) { if(e.which === 1) { lbDown = false; } }); $(".magnify").mousemove(function(e) { if(lbDown) { //your code here } }); }); 

我们使用自己的变量来跟踪鼠标左键( lbDown )。 在mousedown上将其设置为true ,在mouseup上将其设置为false ,然后在mousemove()函数中对此做出反应。

编辑
这是一个小提琴 。
当然,一旦用户停止拖动放大镜,您将需要添加一些代码来隐藏放大镜。

根据要求,这里是对其背后逻辑的解释:
由于JS没有检查鼠标按钮状态的原生方式,我们需要自己实现此function。
鼠标按钮有两种状态:它是updown ,因此我们必须引入一个布尔变量来跟踪鼠标按钮状态(在我的代码中向左按下按钮 lbDown )。
现在我们要做的就是根据状态设置这个变量。 幸运的是,JS提供了事件处理程序:一旦鼠标按钮关闭(单击), mousedown就会被触发,一旦用户释放单击的按钮,就会触发mouseup 。 所以我们在mousedown中将布尔变量设置为true ,在mouseup中将布尔变量设置为false
我们现在可以使用简单的if(lbDown)检查代码中的任何位置,如果其中一个鼠标按钮当前是在检查时关闭的话。

缺点是,到现在为止,每个鼠标按钮都是如此! 我们还没有实现任何区分按钮的东西。
JS中的每个事件都具有属性,该属性允许您确定按下哪个键或按钮来触发(键盘 – /鼠标 – )事件。
现在,在阅读我们读取的jQuery API时which如果是鼠标事件,鼠标左键为1 ,那么我们添加另一个if mousedownmouseup ,所以lbDown只会设置,当左边单击按钮。

我没有足够的声誉来评论,但我想补充一点,上面的代码(来自正确答案)可能会引发问题。 如果你只想要一个特殊的“东西”,例如在拖动时移动(例如用当前的clientX位置移动图像),最好使用$(document).mouseup …而不是$("thing").mouseup因为您可能不想在那个确切的东西上释放鼠标按钮。 这给我带来了一个问题,我只想提一下。