我有一个用户单击链接时会出现的异常现象。
问题是可以单击并拖动相同的链接来重新排列。在这种情况下,我不需要出现跳动者。仅当它实际上等待去某个地方时才需要出现。
我如何使用jQuery创建一个事件侦听器,该事件侦听器仅在单击链接时显示跳动,而不是单击并拖动才能出现?
我有一个用户单击链接时会出现的异常现象。
问题是可以单击并拖动相同的链接来重新排列。在这种情况下,我不需要出现跳动者。仅当它实际上等待去某个地方时才需要出现。
我如何使用jQuery创建一个事件侦听器,该事件侦听器仅在单击链接时显示跳动,而不是单击并拖动才能出现?
Answers:
在mousedown上,开始设置状态,如果触发了mousemove事件,则将其记录下来,最后在mouseup上,检查鼠标是否移动。如果它移动了,我们一直在拖。如果我们没有移动,那就单击一下。
var isDragging = false;
$("a")
.mousedown(function() {
isDragging = false;
})
.mousemove(function() {
isDragging = true;
})
.mouseup(function() {
var wasDragging = isDragging;
isDragging = false;
if (!wasDragging) {
$("#throbble").toggle();
}
});
这是一个演示:http : //jsfiddle.net/W7tvD/1399/
mousemove即使没有任何移动,它仍然会至少触发一次,因此我对其进行了更改,以增加移动时的变量,然后在mouseup jsfiddle.net/W7tvD/1649中检查某个阈值 现在对我来说就像魅力一样,谢谢!
由于某种原因,上述解决方案对我不起作用。我进行以下操作:
$('#container').on('mousedown', function(e) {
$(this).data('p0', { x: e.pageX, y: e.pageY });
}).on('mouseup', function(e) {
var p0 = $(this).data('p0'),
p1 = { x: e.pageX, y: e.pageY },
d = Math.sqrt(Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2));
if (d < 4) {
alert('clicked');
}
})
您可以将距离限制调整为任意值,甚至可以一直调整为零。
sqrt不会慢,并且不会影响UX,即使您执行了数百次也不会。
使用jQuery UI即可!
$( "#draggable" ).draggable({
start: function() {
},
drag: function() {
},
stop: function() {
}
});
$(".draggable")
.mousedown(function(e){
$(this).on("mousemove",function(e){
var p1 = { x: e.pageX, y: e.pageY };
var p0 = $(this).data("p0") || p1;
console.log("dragging from x:" + p0.x + " y:" + p0.y + "to x:" + p1.x + " y:" + p1.y);
$(this).data("p0", p1);
});
})
.mouseup(function(){
$(this).off("mousemove");
});
此解决方案使用“ on”和“ off”功能来绑定取消绑定的mousemove事件(不建议使用bind和unbind)。您还可以检测到两个mousemove事件之间鼠标x和y位置的变化。
我从接受的答案中分支出来,仅在单击并拖动单击时才运行。
当我不按住鼠标时,函数正在运行。如果您还需要此功能,请参见以下更新的代码:
var isDragging = false;
var mouseDown = false;
$('.test_area')
.mousedown(function() {
isDragging = false;
mouseDown = true;
})
.mousemove(function(e) {
isDragging = true;
if (isDragging === true && mouseDown === true) {
my_special_function(e);
}
})
.mouseup(function(e) {
var wasDragging = isDragging;
isDragging = false;
mouseDown = false;
if ( ! wasDragging ) {
my_special_function(e);
}
}
);
基于Simen Echholt答案的jQuery插件。我称它为“单击”。
/**
* jQuery plugin: Configure mouse click that is triggered only when no mouse move was detected in the action.
*
* @param callback
*/
jQuery.fn.singleclick = function(callback) {
return $(this).each(function() {
var singleClickIsDragging = false;
var element = $(this);
// Configure mouse down listener.
element.mousedown(function() {
$(window).mousemove(function() {
singleClickIsDragging = true;
$(window).unbind('mousemove');
});
});
// Configure mouse up listener.
element.mouseup(function(event) {
var wasDragging = singleClickIsDragging;
singleClickIsDragging = false;
$(window).unbind('mousemove');
if(wasDragging) {
return;
}
// Since no mouse move was detected then call callback function.
callback.call(element, event);
});
});
};
正在使用:
element.singleclick(function(event) {
alert('Single/simple click!');
});
^^
确保将元素的draggable属性设置为false,以便在监听mouseup事件时没有副作用:
<div class="thing" draggable="false">text</div>
然后,您可以使用jQuery:
$(function() {
var pressed, pressX, pressY,
dragged,
offset = 3; // helps detect when the user really meant to drag
$(document)
.on('mousedown', '.thing', function(e) {
pressX = e.pageX;
pressY = e.pageY;
pressed = true;
})
.on('mousemove', '.thing', function(e) {
if (!pressed) return;
dragged = Math.abs(e.pageX - pressX) > offset ||
Math.abs(e.pageY - pressY) > offset;
})
.on('mouseup', function() {
dragged && console.log('Thing dragged');
pressed = dragged = false;
});
});
您需要设置一个计时器。计时器超时后,请启动节拍器并注册点击。发生拖动时,请清除计时器,使其永远不会完成。
如果您使用的是jQueryUI,则存在onDrag事件。如果不是,则将侦听器附加到mouseup(),而不是click()。
// here is how you can detect dragging in all four directions
var isDragging = false;
$("some DOM element").mousedown(function(e) {
var previous_x_position = e.pageX;
var previous_y_position = e.pageY;
$(window).mousemove(function(event) {
isDragging = true;
var x_position = event.pageX;
var y_position = event.pageY;
if (previous_x_position < x_position) {
alert('moving right');
} else {
alert('moving left');
}
if (previous_y_position < y_position) {
alert('moving down');
} else {
alert('moving up');
}
$(window).unbind("mousemove");
});
}).mouseup(function() {
var wasDragging = isDragging;
isDragging = false;
$(window).unbind("mousemove");
});
我需要一个始终跟踪鼠标位置并检测向左,向右,向上,向下拖动的功能。它也不会触发点击,但至少需要移动15像素
/**
* Check for drag when moved minimum 15px
* Same time keep track of mouse position while dragging
*/
// Variables to be accessed outside in other functions
var dragMouseX;
var dragMouseY;
var myDragging = false; // true or false
var dragDirectionX = false; // left or right
var dragDirectionY = false; // top or bottom
$(document).on("mousedown", function(e) {
// Reset some variables on mousedown
var lastDirectionCheck = e.timeStamp;
var dragStartX = e.pageX;
var dragStartY = e.pageY;
dragMouseX = e.pageX;
dragMouseY = e.pageY;
myDragging = false;
dragDirectionX = false;
dragDirectionY = false;
// On the move
$(document).on("mousemove", function(e) {
dragMouseX = e.pageX;
dragMouseY = e.pageY;
// Recalculate drag direction every 200ms in case user changes his mind
if (e.timeStamp > (lastDirectionCheck + 200)) {
dragStartX = dragMouseX;
dragStartY = dragMouseY;
lastDirectionCheck = e.timeStamp;
}
// Check for drag when moved minimum 15px in any direction
if (!myDragging && Math.abs(dragStartX - dragMouseX) > 15 || Math.abs(dragStartY - dragMouseY) > 15) {
myDragging = true;
}
if (myDragging) {
// Check drag direction X
if (dragStartX > dragMouseX) dragDirectionX = 'left';
if (dragStartX < dragMouseX) dragDirectionX = 'right';
// Check drag direction Y
if (dragStartY > dragMouseY) dragDirectionY = 'top';
if (dragStartY < dragMouseY) dragDirectionY = 'bottom';
// console.log(dragDirectionX + ' ' + dragDirectionY);
}
});
});
// Reset some variables again on mouseup
$(document).on("mouseup", function() {
$(document).off("mousemove");
myDragging = false;
dragDirectionX = false;
dragDirectionY = false;
});