我知道“mousedown”是用户按下鼠标时,“mouseup”是用户松开鼠标时。但我想在用户按下鼠标并按住它直到它释放后监听事件。有任何想法吗?
问问题
53580 次
4 回答
10
如果您想要保持状态,那么它将是您处于mousedown事件状态一段时间时的状态。当您按下mousedown而不是mouseup时,此状态存在。因此,您需要获取一个记录事件当前状态的变量。
JS
$('div').on('mousedown mouseup', function mouseState(e) {
if (e.type == "mousedown") {
//code triggers on hold
console.log("hold");
}
});
于 2013-09-03T05:22:26.467 回答
4
尝试这个
将相应的鼠标事件添加到以下功能
mouse = false;
function mousedown()
{
mouse = true;
callEvent();
}
function mouseup()
{
mouse =false;
}
function callEvent()
{
if(mouse)
{
// do whatever you want
// it will continue executing until mouse is not released
setTimeout("callEvent()",1);
}
else
return;
}
于 2013-09-03T05:17:58.960 回答
4
var setint = '';
$(document).ready(function() {
var val = 0;
$('#hold').on('mousedown',function (e) {
clearInterval(setint);
val = 0;
setint = setInterval(function () {
$("#putdata").val(++val);
console.log("mousehold");
},50);
});
$('#hold').on("mouseleave mouseup", function () {
val = 0;
$("#putdata").val(val);
clearInterval(setint);
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<input type="text" id="putdata" />
<input type="button" value="mouse-hold" id="hold" />
</body>
<html>
于 2017-06-03T18:49:41.813 回答
2
来自 jquery.com:
HTML:
<p>Press mouse and release here.</p>
脚本:
$("p").mouseup(function(){
$(this).append('<span style="color:#F00;">Mouse up.</span>');
}).mousedown(function(){
$(this).append('<span style="color:#00F;">Mouse down.</span>');
});
于 2013-09-03T05:06:15.020 回答