views:

169

answers:

1

Hi, take a look at this code:

$(document).ready(function() {
    document.getElementById(sliderId).onmousedown = sliderMouseDown;
});

function sliderMouseDown() {
    document.onmousemove = sliderMouseMove; 
    document.onmouseup   = sliderMouseUp;
}

function sliderMouseMove() {
    $('#test').html("sliding");
}

function sliderMouseUp() {
    $('#test').html("not sliding");
    document.removeEventListener('mousemove', sliderMouseMove, false);
    document.removeEventListener('mouseup', sliderMouseUp, false);
}

Everything works as it should, until the sliderMouseUp function is called. Now, the function is called alright, and the #test-div says "not sliding", but if I move my mouse around in the document afterwards, it displays "sliding" in the #test-div again, like if the removeEventListener function does not work. Am I doing something wrong here?

+2  A: 

If jQuery is an option, and seems it is since you're using it, do this:

$(function() {
  $("#" + sliderId).mousedown(function() { 
    $(document).mousemove(function() { 
      $("#test").html("sliding");
    }).mouseup(function() {
      $("#test").html("not sliding");
      $(document).unbind("mousemove mouseup");
    });
  });
});

Using the jQuery event model will result in a lot less hair-pulling, let it take care of the cross-browser and event quirks for you.

Nick Craver
Thank you so much, I had begun working on a custom cross-browser script.. One problem though using your script (only in firefox, IE works fine), after I have "slided" the #test-div the first time, firefox thinks that I want to "drag" the div somewhere on the second mousemove, sort of like you would drag a picture from the browser.
soren.qvist
Seems like this is a known issue with non-IE browsers, I'll have a look at it myself. Anyway, thanks for your help
soren.qvist
@user281434 - Welcome :) If more problems arise leave another comment here so I see it pop up and I'll try and help.
Nick Craver