最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery mouseup not firing after drag off link - Stack Overflow

programmeradmin1浏览0评论

See this jsfiddle:

/

Click and hold the button, drag off the button and release. As you can see, the mouseup event never fires if the mouse is not over the element when the mouse button is released. Thus, the styling in my example never changes back to its original. How do you fire the mouseup event if the mouse button is released when not over the clicked element?

Edit1: BTW I have looked at several solutions on this site, implemented them, and the mouseup event still did not fire.

See this jsfiddle:

http://jsfiddle.net/CB87X/6/

Click and hold the button, drag off the button and release. As you can see, the mouseup event never fires if the mouse is not over the element when the mouse button is released. Thus, the styling in my example never changes back to its original. How do you fire the mouseup event if the mouse button is released when not over the clicked element?

Edit1: BTW I have looked at several solutions on this site, implemented them, and the mouseup event still did not fire.

Share Improve this question asked Sep 10, 2012 at 9:22 preahkumpiipreahkumpii 1,3014 gold badges21 silver badges38 bronze badges 2
  • 2 Note that this is the default behavior of regular buttons as well, at least in chrome. Demo – David Hedlund Commented Sep 10, 2012 at 9:26
  • 1 like this way – Sender Commented Sep 10, 2012 at 9:32
Add a comment  | 

4 Answers 4

Reset to default 10

The mouseup event is relative to where the pointer is and this is expected behaviour. If you want your button to style properly, bind the mouseleave event as well.

This should do the trick. If you left click on the button (.but) and drag off, you can mouseup anywhere on the page and still fire the click event. If you mouseleave the page 'body' and mouseup, the binded mouseleave event is unbinded.

$('.but').mousedown( function(e) {
    if (e.which == 1) {
        var $this = $(this);
        $this.bind('mouseleave', function(){
            $('body').one('mouseup', function() {
                $this.click();
            });
        });
        $this.mouseup(function() {
            $(this).unbind('mouseleave');
        });
    }
});

Forked your exemple to provide a working example:

http://jsfiddle.net/67Rrs/2/

Once the button is mousedowned, an event is bound to the next mouseup, wherever it happens, that resets the style.

Just use $(document).on('mouseup dragend', somefunc);

发布评论

评论列表(0)

  1. 暂无评论