mousedown event not happening - javascript

in the snippet below, only the 'mousemove' event is working. 'mousedown' has no effect and I'm not able to decipher why this is happening. If I replace 'mousedown' with 'click', it does work, however I want to use mousedown so the fadeOut event happens immediately (at the start of a click, rather than using click which relies on the users mouse going down and then back up.
$('.issue-carousel').on({
// On mousemove, controls follow cursor
mousemove: function(e) {
var parentOffset = $(this).offset();
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
$('.drag-indicator').css({
left: relX,
top: relY
});
},
mousedown: function() {
$('.drag-indicator').fadeOut(300);
}
});

As per my comment, your code works fine - with one exception. The mousedown fadeout doesn't work because you technically aren't clicking the carousel but instead the drag indicator. I've updated the code as per below where I have changed the mousedown target accordingly;
$('.issue-carousel').on({
// On mousemove, controls follow cursor
mousemove: function(e) {
var parentOffset = $(this).offset();
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
$('.drag-indicator').css({
left: relX,
top: relY
});
}
});
$('.drag-indicator').on({
mousedown: function(e){
$(this).fadeOut(300);
}
})
A working fiddle can be found here: https://jsfiddle.net/o3cdLjp7/2/

Related

How can I prevent a jquery resizeable div to receive the resize click event

I create a div and then:
$("#div").resizable({
disabled:true
});
Then I enable the resize behaviour and at the same time i want to handle a click on the div:
$("#div").on("click", myHandler);
$("#div").resizable( "option", "disabled", false);
function myHandler() {
console.log("div clicked");
}
If now I drag the bottom-right corner of the div I can resize it, but when I release the drag if the mouse pointer is inside the div (this happens when shrinking it) myHandler() is also called. Since myHandler() is supposed to handle a different behavior - and not the resizing - how can I solve this?
What I've tried so far with no success:
$("#div").resizable({
disabled:true,
stop: function(event) { event.stopPropagation(); }
});
How about only doing the click handler when the mouse position is at the same place as it started?
var left = 0;
var top = 0;
("#div").on({
mousedown: function(e) {
left = e.pageX;
top = e.pageY;
},
mouseup: function(e) {
if (left === e.pageX && top === e.pageY) {
console.log('click');
}
}
});

Touchmove into Mousemove

How do I get touchmove recognised as mousemove, I want to use the script below which works on pc but not mobile or tablets. Is there a way to recognise touchmove as mousemove?
<script>
$(document).bind('mousemove', function(e){
$('#try').css({
left: e.pageX -300,
top: e.pageY -145
});
});
</script>
I'd put the common code in a separate function, like this:
function onMouseMove(x, y) {
$('#try').css({
left: x -300,
top: y -145
});
}
Then register event listeners for both mousemove and touchmove that forward the coordinates to your common function:
$(document).bind('mousemove', function(e){
onMouseMove(e.pageX, e.pageY);
});
$(document).bind('touchmove', function(e){
var touch = e.changedTouches[0];
onMouseMove(touch.pageX, touch.pageY);
});

No e.clientX or e.clientY on drag event in Firefox

I've implemented a simple drag and drop system using directives in Angular. It works fine in Chrome, but Firefox doesn't expose event.clientX, event.clientY properties on drag event (They just refuse to fix it).
So I'm looking for a good alternative to expose these properties on drag event: the x,y coordinates are needed for visual feedback on drag event.
Code is here - check out in Chrome and Firefox to see the problem.
In Chrome, drag an item in the folders, you'll have the same item displayed as visual feedback following the mouse, not in Firefox (because Firefox doesn't support e.clientX and e.clientY in the drag event).
the problem is here (beginning line 45):
.on('drag', function(e) {
if (e.originalEvent.clientX) {
el.css({
'top': e.originalEvent.clientY + 10,
'left': e.originalEvent.clientX + 10
});
} else {
el.css('display', 'none');
}
});
So how can I get the mouse position on screen during a drag event, in Firefox (the angular way, I mean with directives, no global variable, or whatever)?
You can hook up to dragover on document -- clientX and clientY are exposed there.
Use functional closure to not populating global scope. Here is updated PLNKR (tested in Chrome and FF).
Changes to js:
.directive('mpDrag', function($timeout, $window, $document) {
// keeping coordinates private and
// shared among all instances of the directive
var mouseX, mouseY;
$document.on("dragover", function(event){
mouseX = event.originalEvent.clientX;
mouseY = event.originalEvent.clientY;
})
return {
...
link: function($scope, element, attrs) {
...
$timeout(function() {
...
.on('drag', function(e) {
// just use mouseX, mouseY directely here
// (btw. you should detect differently when to hide the element)
console.log(mouseX, mouseY);
if (e.originalEvent.clientX) {
el.css({
'top': mouseY,
'left': mouseX
});
} else {
el.css('display', 'none');
}
});
});
}
};
})
You must borrow drag coordinates from the document itself:
var dragX = 0,
dragY = 0;
element.on('dragstart', function(e) {
document.ondragover = function(event) {
event = event || window.event;
dragX = event.pageX,
dragY = event.pageY;
};
});
element.on('drag', function(e) {
el.css({
'top': dragY + 10,
'left': dragX + 10
});
});
Updated plunker

.mouseup() and .mousedown() not working with .mousemove()

For some reason, my .mouseup() event is not being called. Ideally I'd like to console.log when the user activates mouseup().
Would anyone know what is wrong?
http://jsfiddle.net/cBh3B/
$('.circle').mousedown(function () {
console.log('mousedone');
$(document).mousemove(function (e) {
console.log(e.pageY);
$('.circle').offset({
left: e.pageX,
top: e.pageY
});
});
});
$('.circle').mouseup(function () {
console.log('up');
});
var p = $('.square').position();
console.log(p.left);
You are moving the circle away from the cursor once the mousedown event is triggered. By the time the mouse button is moved up, the cursor is no longer over the circle, which means that the $('.circle').mouseup event will not be called.
If you keep the circle under the mouse then the mouseup event will be captured. (See this jsFiddle demo.)
EDIT: If your element is actually a 'circle' then your problem will most likely be the one evoked by #Peter Olson.
I recommend you try using
$('.example').on('mouseup', function(){ console.log('mouseup'); });
Here is an example: http://jsfiddle.net/Grimbode/qsE56/
Seems this way your events are being called.
$('.circle').on('mousedown',function () {
console.log('mousedone');
$(document).on('mousemove', function (e) {
console.log(e.pageY);
$('.circle').offset({
left: e.pageX,
top: e.pageY
});
});
});
$('.circle').on('mouseup', function () {
console.log('up');
});
var p = $('.square').position();
console.log(p.left);
You can also check the log, there is a clear 'up' written on it.

How to restart event mousemove jquery?

I have to move a div by clicking on it, clicking again I must stop the div in that position. Now the problem is: when I want to move again the div, does not activate the mousemove event ... how can I fix it?
$('.move_div').live('click', function() {
$('html').on('mousemove', function(e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
$('div').css({'top': y, 'left': x});
});
$("html").live('click', function() {
$('html').off('mousemove');
});
});
var ele = '.move_adv';
var moveBool = false;
$(function () {
$('html').on('mousemove', function (e) {
console.log($(this).width());
if (moveBool == true) {
var x = e.pageX - $(ele).width()/2;
var y = e.pageY - $(ele).height()/2;
$(ele).css({
'top': y,
'left': x
});
}
});
});
$(ele).live('click', function () {
moveBool = !moveBool;
});
http://jsfiddle.net/6y24s/2/
The main logic is storing the 'moveability' state of the div in a boolean.
You would also like to refine the code more.
Fiddle here , if you want to keep your code then only thing you need to add is
event.stopPropagation();
When you click on the div the mousemove handler is added to the div. Then an event handler is added to the document that removes any mousemove event handlers.
You then move the mouse and the div follows, you click and the mousemove handler is deleted.
You click on the div again, a mousemove event handler is added, though then the click event handler from the document takes away the mousemove handler.
So whenever you click after the first two clicks the mousemove handler is simultaneously created and destroyed.
Also use .on() instead of .live()
.live() was deprecated in JQuery 1.7

Categories