当用户点击链接时,我想要一个 throbber 出现。
问题在于,同一个链接可能会被点击并拖动以重新排列。在这种情况下,我不需要 throbber 出现,只有在等待跳转到某个地方时才需要出现。
如何使用 jQuery 创建事件监听器,仅在点击链接进行跳转时 允许 throbber 出现,而不是点击并拖动呢?
当用户点击链接时,我想要一个 throbber 出现。
问题在于,同一个链接可能会被点击并拖动以重新排列。在这种情况下,我不需要 throbber 出现,只有在等待跳转到某个地方时才需要出现。
如何使用 jQuery 创建事件监听器,仅在点击链接进行跳转时 允许 throbber 出现,而不是点击并拖动呢?
在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
上检查一个特定的阈值。http://jsfiddle.net/W7tvD/1649/
现在对我非常有效,谢谢! - halfbit由于某些原因,上述解决方案对我不起作用。 我采用了以下解决方案:
$('#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
不会很慢,也不会影响用户体验,即使您重复使用了数百次。 - NateS只需使用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"函数来绑定和解除绑定鼠标移动事件("bind"和"unbind"已被弃用)。您还可以检测两个鼠标移动事件之间鼠标X和Y位置的变化。
mousedown
事件都会向 this
添加另一个事件处理程序,导致该函数的代码体被执行多次并逐渐增加。 - LuKenneth$(function() {
var isDragging = false;
$("#status").html("status:");
$("a")
.mousedown(function() {
$("#status").html("status: DRAGGED");
})
.mouseup(function() {
$("#status").html("status: dropped");
});
$("ul").sortable();
});
// 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");
});
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);
}
}
);
请确保将元素的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;
});
});
/**
* 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").on('touchstart mousedown', function(ev) {
ev.preventDefault();
$(this).on("touchmove mousemove",function(e){
var x = e.pageX || e.changedTouches[0].pageX;
var y = e.pageY || e.changedTouches[0].pageY;
var p1 = { x: x, y: y };
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", p0);
});
}).on('touchend mouseup', function(ev) {
ev.preventDefault();
$(this).off("touchmove mousemove");
});