0

I need to disable temporaly event mouseup when user scroll the div. I test bind scroll event on div and preventDefault and stopImmadiatePropagation on event but not work.

Prototype of code:

$(document).ready(function() {
  $(document).on("mouseup", "#test", function(e) {
      $("body").append('<b style="color:red">Mouse up | </b>');
  });
  $("#test").scroll(function(e) {
      $("body").append('<b style="color:green">Scroll | </b>');
      e.preventDefault();
      e.stopImmediatePropagation();
  });
});

I provide this plunker example: plunker

Davide
  • 475
  • 4
  • 19

2 Answers2

2

Try to use .off()

JQuery API documentaion

Update:

The right solution: Plunker

1
$(document).ready(function() {
  $(document).on("mouseup", "#test", function(e) {
      $("body").append('<b style="color:red">Mouse up | </b>');
  });
  var isScrolling = false;
  $("#test").scroll(function(e) {
      $("body").append('<b style="color:green">Scroll | </b>');
      e.preventDefault();
      e.stopImmediatePropagation();
      isScrolling = true;
      setTimeout(function(){
        isScrolling = false;
        setTimeout(function(){
          if(!isScrolling){
            $(document).off("mouseup", "#test");
            $(document).on("mouseup", "#test", function(e) {
              $("body").append('<b style="color:red">Mouse up | </b>');
            });
          }
        }, 500);
      }, 500);
      $(document).off("mouseup", "#test");
  });
});

It will disable the mouseup event when the scroll event is fired.

http://plnkr.co/edit/630M9zZpNxXGBABvPq8q?p=preview

Rapid
  • 46
  • 4