简体   繁体   English

.mouseup()和.mousedown()无法与.mousemove()一起使用

[英].mouseup() and .mousedown() not working with .mousemove()

For some reason, my .mouseup() event is not being called. 由于某种原因,未调用我的.mouseup()事件。 Ideally I'd like to console.log when the user activates mouseup() . 理想情况下,当用户激活mouseup()时,我想console.log。

Would anyone know what is wrong? 有谁知道这是怎么回事?

http://jsfiddle.net/cBh3B/ http://jsfiddle.net/cBh3B/

$('.circle').mousedown(function () {
    console.log('mousedone');
    $(document).mousemove(function (e) {
        console.log(e.pageY);
        $('.circle').offset({
            left: e.pageX,
            top: e.pageY
        });
    });
});

$('.circle').mouseup(function () {
    console.log('up');
});

var p = $('.square').position();
console.log(p.left);

You are moving the circle away from the cursor once the mousedown event is triggered. 一旦触发mousedown事件,您就将圆圈从光标移开。 By the time the mouse button is moved up, the cursor is no longer over the circle, which means that the $('.circle').mouseup event will not be called. 当鼠标按钮向上移动时,光标不再位于圆上,这意味着将不会调用$('.circle').mouseup事件。

If you keep the circle under the mouse then the mouseup event will be captured. 如果将圆圈保持在鼠标下方,则将捕获mouseup事件。 ( See this jsFiddle demo .) 请参阅此jsFiddle演示 。)

EDIT: If your element is actually a 'circle' then your problem will most likely be the one evoked by @Peter Olson. 编辑:如果您的元素实际上是一个“圆圈”,那么您的问题很可能是@Peter Olson引起的。

I recommend you try using 我建议您尝试使用

$('.example').on('mouseup', function(){ console.log('mouseup'); });

Here is an example: http://jsfiddle.net/Grimbode/qsE56/ 这是一个示例: http : //jsfiddle.net/Grimbode/qsE56/

Seems this way your events are being called. 似乎是通过这种方式来调用您的事件。

$('.circle').on('mousedown',function () {
    console.log('mousedone');
    $(document).on('mousemove', function (e) {
        console.log(e.pageY);
        $('.circle').offset({
            left: e.pageX,
            top: e.pageY
        });
    });
});

$('.circle').on('mouseup', function () {
    console.log('up');
});

var p = $('.square').position();
console.log(p.left);

You can also check the log, there is a clear 'up' written on it. 您也可以检查日志,上面有清晰的“向上”字样。

暂无
暂无

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

相关问题 通过单击和拖动div使用mousemove,mousedown和mouseup - making use of mousemove, mousedown, and mouseup with a click and drag div 像mousedown / mousemove / mouseup一样处理touchstart / touchmove / touchend - Handling touchstart/touchmove/touchend the same way as mousedown/mousemove/mouseup 使用标准javascript而不是jquery中的mousedown,mousemove和mouseup的触摸事件句柄 - Touch event handle with mousedown, mousemove and mouseup in standard javascript not jquery 如何在Backbonejs视图方法中监听mousedown,mousemove和mouseup - How to listen to mousedown, mousemove and mouseup in a Backbonejs view method 在Chrome中mousedown或mouseup后如何防止鼠标移动[Chrome中可能存在错误]? - How to prevent mousemove after mousedown or mouseup in Chrome [possible bug in Chrome]? 为什么(MouseUp mouseDown)在移动浏览器上不起作用? - Why (MouseUp mouseDown) are not working on mobile browser? 将javascript mousedown / mouseup / mousemove / keypress侦听器移植到移动设备时,我应该注意些什么? - What should I be aware of when porting javascript mousedown/mouseup/mousemove/keypress listeners to mobile? Chrome mousedown和mouseup事件不再有效,其他浏览器也没问题 - Chrome mousedown and mouseup events no longer working, other browsers are fine ng-mousedown和ng-mouseup无法在手机中使用 - ng-mousedown and ng-mouseup not working in mobile phone mouseup 在 mousedown 后不会被触发 - mouseup is not fired after mousedown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM