gpt4 book ai didi

jQuery 检测连续的鼠标按下

转载 作者:行者123 更新时间:2023-12-01 02:47:25 24 4
gpt4 key购买 nike

有没有办法检测连续的鼠标按下?换句话说,我希望一个函数在按下鼠标左键时继续运行。这是我正在尝试做的一个例子。我有一个控制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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com