Federico Piragua
Federico Piragua

Reputation: 707

While mousedown and mousemove get coordinates of the mouse

I have this jQuery that does when the user is clicking the slider get the coordinates but when he lifts up the mouse the function still runs

$("slider").mousedown(function(){
    $(this).mousemove(function(e){
       console.log(e.clientX);
    });
};

But when the users lift up the mouse the code still runs and still logs the coordinates.

Upvotes: 0

Views: 271

Answers (2)

Dvir
Dvir

Reputation: 3339

Do like this:

$("slider").mousedown(function(){
    $(this).mousemove(function(e){
       console.log(e.clientX);
    });
};
$("slider").mouseup(function(){
    $(this).unbind("mousemove");
});

Upvotes: 1

Hamza Kubba
Hamza Kubba

Reputation: 2269

Your mousedown function changes the mousemove function permanently. So you want to add something like this:

$("slider").mouseup(function(){
    $(this).unbind("mousemove")
};

Upvotes: 1

Related Questions