如何检查鼠标在一个元素上停留了几秒钟
How to check mouse holded some sec on a element
如何在元素上检查鼠标洞。
表示只有当用户在一个元素上停留鼠标超过最小时间(例如:3秒)时,函数才应该执行。
在堆栈中找到了许多答案,但是解决方案延迟了执行,但是我想检查鼠标是否有洞,如果是,执行函数否则不做任何操作。
之前已经问过同样的问题,但还没有得到我想要的答案
有可能吗?
我想你正在寻找这个,如果一个div
得到悬停并按住鼠标至少3秒然后做你的东西像下面
var myTimeout;
$('div').mouseenter(function() {
myTimeout = setTimeout(function() {
alert("do your stuff now");
}, 3000);
}).mouseleave(function() {
clearTimeout(myTimeout);
});
这是一个自定义的jquery函数
$.fn.mouseHover = function(time, callback){
var timer;
$(this).on("mouseover", function(e){
timer = setTimeout(callback.bind(this, e), time);
}.bind(this)).on("mouseout", function(e){
clearTimeout(timer);
})
};
$('#my-element').mouseHover(3000, function(){ alert("WHOOPWhOOP");});
以防OP意味着点击并按住
$.fn.mouseHold = function(time, callback) {
var timer;
$(this).on("mousedown", function(e){
e.preventDefault();
timer = setTimeout(callback.bind(this, e), time);
}.bind(this)).on("mouseup", function(e){
clearTimeout(timer);
})
}
jsfiddle: http://jsbin.com/huhagiju/1/
应该足够简单:
$('.your-element').on('mousedown', function(event) {
var $that = $(this);
// This timeout will run after 3 seconds.
var t = setTimeout(function() {
if ($that.data('mouse_down_start') != null) {
// If it's not null, it means that the user hasn't released the click yet
// so proceed with the execution.
runMouseDown(event, $that);
// And remove the data.
$(that).removeData('mouse_down_start');
}
}, 3000);
// Add the data and the mouseup function to clear the data and timeout
$(this)
.data('mouse_down_start', true)
.one('mouseup', function(event) {
// Use .one() here because this should only fire once.
$(this).removeData('mouse_down_start');
clearTimeout(t);
});
});
function runMouseDown(event, $that) {
// do whatever you need done
}
结帐
逻辑- 鼠标下移处理程序记录点击开始时间
- 鼠标启动处理程序记录鼠标启动时间并计算时差,如果超过3秒则报警时间,否则报警时间小于3秒
HTML
<p>Press mouse and release here.</p>
Jquery var flag, flag2;
$( "p" )
.mouseup(function() {
$( this ).append( "<span style='color:#f00;'>Mouse up.</span>" );
flag2 = new Date().getTime();
var passed = flag2 - flag;
if(passed>='3000')
alert(passed);
else
alert("left before");
console.log(passed); //time passed in milliseconds
})
.mousedown(function() {
$( this ).append( "<span style='color:#00f;'>Mouse down.</span>" );
flag = new Date().getTime();
});
这都是逻辑问题。
你只是有一个变量告诉你是否已经听了一段时间,比如3秒。
如果你正在监听超过这个值,这是不可能的,因为你应该重置它,所以重置它。否则你做你的工作。
var mySpecialFunc = function() { alert("go go go..."); };
var lastTime = 0;
var main_id = "some_id" ;// supply the id of a div over which to check mouseover
document.getElementById(main_id).addEventListener("mouseover",function(e) {
var currTime = new Date().getTime();
var diffTime = currTime - lastTime;
if(diffTime > 4000) {
// more than 4 seconds reset the lastTime
lastTime = currTime;
alert("diffTime " + diffTime);
return ;
}
if(diffTime > 3000) {
// user had mouseover for too long
lastTime = 0;
mySpecialFunc("info");
}
// else do nothing.
});
这是一个基本的代码,我认为你可以根据你的要求改进和调整
这里有一些代码(带有一个fiddle),可以做你想做的事情…
(这也表明我今晚有多无聊)
var props = {
1000: { color: 'red', msg: 'Ready' },
2000: { color: 'yellow', msg: 'Set' },
3000: { color: 'green' , msg: 'Go!' }
};
var handles = [];
var $d = $('#theDiv');
$d.mouseenter(function () {
$.each(props, function (k, obj) {
handles[k] = setTimeout(function () {
changeStuff($d, obj);
}, k);
});
}).mouseleave(function () {
$.each(handles, function (i, h) {
clearTimeout(h);
});
reset($d);
});
function reset($d) {
$d.css('backgroundColor', 'orange');
$d.text('Hover here...');
}
function changeStuff($node, o) {
$node.css('backgroundColor', o.color);
$node.text(o.msg);
}
相关文章:
- 显示5秒后隐藏潜水
- 操作员”;新的“;根据我想在几个JavaScript文件中使用的类,在JavaScript中使用
- X秒后刷新select元素
- 如何每10.6秒从时间戳增加+1
- 如果30秒未单击,请应用CSS一次,将其删除,然后重新迭代
- 可以't将几个数字设置为<输入类型=“;数字“>
- 淡入淡出每隔几秒就会发生变化的图像
- 在画布上显示字符串仅显示几分之一秒
- 在网页中停留 10 秒后打开弹出窗口
- 每隔x秒重复几次触发器('click')功能
- HTML5 -视频播放时每隔几秒触发一次事件
- 使用JavaScript在视频结束前几秒运行一个函数
- 使用setTimeout在X秒后隐藏几个元素
- 反应-每隔几秒渲染相对时间
- 显示前的几秒延迟:无
- jQuery检测鼠标停留在元素上的秒数
- 如何检查鼠标在一个元素上停留了几秒钟
- 如何创建加载弹出停留5秒
- 使用socket.io每隔几秒以编程方式触发一个事件
- 在每隔几秒播放的动画中添加声音