有没有办法检测连续的鼠标按下?换句话说,我希望一个函数在按下鼠标左键时继续运行。这是我正在尝试做的一个例子。我有一个控制scrollLeft 位置的按钮。每次点击它都会增加 20,但我希望它在按住鼠标按钮并停止 onmouseup 时继续增加。
与按住鼠标按钮时滚动条上的箭头的工作方式类似
var wrapperDiv = $("#wrapper-div");
var scrollWidth = 0;
$("#right-button").click(function() {
if(scrollWidth < wrapperDiv[0].scrollWidth) {
scrollWidth = scrollWidth+20;
wrapperDiv.scrollLeft(scrollWidth);
}
});
最佳答案
不要使用点击
处理程序。使用两个单独的处理程序,一个用于 mousedown
和一个 mouseup
.
var $wrapper = $('#wrapper');
function startScrolling()
{
// contintually increase scroll position
$wrapper.animate({scrollLeft: '+=20'}, startScrolling);
}
function stopScrolling()
{
// stop increasing scroll position
$wrapper.stop();
}
$('#right-button').mousedown(startScrolling).mouseup(stopScrolling);
演示:http://jsfiddle.net/mattball/dkWGy/
关于jQuery 检测连续的鼠标按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8216333/