Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQUERY How to disable not-allowed cursor while dragging?

I've got issue with the not-allowed cursor. While dragging the "drag" element, not-allowed cursor appears and I can't drag it anymore. How can i prevent that? I want to make my "drag" element always be "absolute" while mouse is down.

Note: I know that it can happens becouse "pointer-events" but I need it to be contained into this code.

enter image description here

Some code:

$("#drag").bind({
  mousedown : function (e) {
      var dragged = $(this);
      dragged.css({
          left : e.pageX - (50 / 2),
          top : e.pageY - (50 / 2)
      });
      dragged.addClass("absolute");
      dragged.css({
          'pointer-events' : 'none'
      })
      var upHandler = function () {
          dragged.removeClass("absolute");
          dragged.css({
              'pointer-events' : 'all'
          })
          $("body").off('mouseup', upHandler);
          $("body").off('mousemove', moveHandler);
      }
      var moveHandler = function (e) {
          dragged.css({
              left : e.pageX - (50 / 2),
              top : e.pageY - (50 / 2)
          });
      }

      $("body").bind({
          mouseup : upHandler,
          mousemove : moveHandler
      })
  }
  });

  $("body").mousemove(function (event) {
     $("#log").text("pageX: " + event.pageX + ", pageY: " + event.pageY);
  });

https://jsfiddle.net/38zecoL1/1/

Thanks for any help.

like image 680
Hvrxld Avatar asked Aug 06 '17 16:08

Hvrxld


1 Answers

Before handling your mouse events, make a call to

e.preventDefault();

It cancels the event which prevents the browser from performing the default behavior. Normally it would show a "not allowed" cursor on elements that typically are not draggable.

$("box").mouseover(function() {
  $(this).addClass("green");
  var box = $(this).attr("id");
  $("#result").html(box);
});

/*
    Solution
*/
$("box").mousedown(function(e) {
  // Booooom! This should stop the undesired default beahvior.
  e.preventDefault();
});

$("box").mouseleave(function() {
  $(this).removeClass("green");
});

$("#drag").bind({
  mousedown: function() {
    $(this).addClass("absolute");
  },
  mouseup: function() {
    $(this).removeClass("absolute");
  },
  mousemove: function(e) {
    $(this).css({
      left: e.pageX - (50 / 2),
      top: e.pageY - (50 / 2)
    });
  }
});

$("body").mousemove(function(event) {
  $("#log").text("pageX: " + event.pageX + ", pageY: " + event.pageY);
});
box {
  float: left;
  width: 100px;
  height: 100px;
  background-color: red;
  margin: 20px;
}

#log {
  position: absolute;
  top: 150px;
}

.green {
  background-color: green;
}

#drag {
  width: 50px;
  height: 50px;
  float: left;
  background-color: blue;
}

#drag.absolute {
  position: absolute;
}

html,
body {
  width: 100%;
  height: 100%;
}

* {
  margin: 0;
  padding: 0;
}
<box id="box1">
  <div id="drag"></div>
</box>
<box id="box2"></box>

<div id="result"></div>
<div id="log"></div>

Original fiddle: https://jsfiddle.net/38zecoL1/4/

like image 55
Remembered to Post Solution Avatar answered Nov 14 '22 08:11

Remembered to Post Solution