Joe
Joe

Reputation: 191

mousemove is fired automatically IE 9

I'm having a problem in IE 9 (haven't tested 8), What the code is supposed to do is detect whether or not the mouse was dragged after clicking on a certain element. The problem is that IE 9 automatically enters the $(window).mousemove event handler even though I don't move the mouse. Works Fine in Chrome and FF.

   $(Element).mousedown(function() {
            $(window).mousemove(function() {
                isDragging = true;
                $(window).unbind("mousemove");
            });
        }).mouseup(function() {
            $(window).unbind("mousemove");
        });

Upvotes: 1

Views: 1349

Answers (2)

Steven
Steven

Reputation: 21

Because the movement of the mouse is so minute with a laser mouse I changed your method a bit to better suit my need of monitoring just the fact of weather or not the mouse is moving:

$(window).mousemove(function (e) {
    var smallNo = 0;
    var x = e.clientX;
    var y = e.clientY;
    var minMovement = 1;
    if ((x - smallNo) > minMovement || (y - smallNo) > minMovement) {
        countDownTime = logoutTime;
    }
});

Even though the mousemove event is firing constantly the coordinates do not change unless you physically move your mouse. I used that to figure out weather or not the mouse had moved or not.

Upvotes: 0

jfriend00
jfriend00

Reputation: 707238

You should be only starting a drag after the mouse has moved a certain minimum distance. To do that, record the mouse position in the mousedown handler and then in mousemove, only start your drag when the mouse has moved a minimum distance.

    $(Element).mousedown(function(e) {
        var x = e.clientX;
        var y = e.clientY;
        var minMovement = 3;
        $(window).mousemove(function(e) {
            if (Math.abs(e.clientX - x) > minMovement || Math.abs(e.clientY - y) > minMovement) {
                isDragging = true;
                $(window).unbind("mousemove");
            }
        });
    }).mouseup(function() {
        $(window).unbind("mousemove");
    });

FYI, some mice can record very, very tiny movements (less than one pixel on the screen) so it's likely that the mouse does actually move after you press the mouse down and IE is probably just reporting that movement. Other browsers may wait until the mouse moves a whole pixel. In any case, if you require a minimum number of pixels of movement then you won't have this issue.

Upvotes: 3

Related Questions