I have some line of codes which will move an element to mouse position after it is mousedown
-ed.
I want to remove the event attached to it, so it won't following the mouse position anymore after it is mouseup
-ed!
The Problem
The element still follows the mouse after mouseup
!
I want it to follow the mouse on mousedown
and stop following the mouse after mouseup
! How do I remove the mousemove
listener from the element?
Here is the JS
jQuery(document).ready(function ($) {
$(".crossY").on("mousedown", function (e) {
var j = $(this);
$(document).on("mousemove", function (e) {
j.css({
"top": e.pageY,
"left": e.pageX
});
});
})
$(".crossY").on("mouseup", function (e) {
var j = $(this);
$(document).on("mousemove", function (e) {
j.css({
"top": j.css("top"),
"left": j.css("left")
});
});
});
});
and the FIDDLE DEMO