我使用的是Paul爱尔兰的idle-timer插件,可以在这里找到:http://paulirish.com/2009/jquery-idletimer-plugin/。
我想在5秒不活动后隐藏一些div,并在捕捉到用户活动时显示它们。
下面是我的代码:
$(document).ready(function(){
$.idleTimer(5000);
$(document).bind("idle.idleTimer", function(){
$("#audio_container").fadeOut(1000);
$(".breadcrumb").fadeOut(1000);
});
$(document).bind("active.idleTimer", function(){
$("#audio_container").fadeIn(1000);
$(".breadcrumb").fadeIn(1000);
});
});它在Firefox / Safari / Mobile Safari上运行得很好,但我不能让它在Chrome或IE8/9上运行。显然,onmousemove事件才是问题所在,如果我取消绑定onmousemove事件,它就会工作(但我需要它,所以这对我来说不是一个可接受的修复)。
你可以在这里找到一个例子:
诚挚的问候,
编辑:
mousemouve事件位于idle-timer插件中。
$.idleTimer = function(newTimeout, elem){
// defaults that are to be stored as instance props on the elem
var idle = false, //indicates if the user is idle
enabled = true, //indicates if the idle timer is enabled
timeout = 30000, //the amount of time (ms) before the user is considered idle
events = 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove'; // activity is one of these events如果我从插件中删除mousemove事件,它就能正常工作。
发布于 2011-08-26 20:55:56
如果你不需要使用插件并在5秒不活动后隐藏div或wathever元素,并再次显示它处于活动状态,我想出了一个脚本,如下所示(在chrome,firefox,explorer中测试过):
http://jsfiddle.net/e8KXw/1/
(使用输入而不是div进行演示)
<input type="text" > /* Use div or any element */Jquery:
var interval = 1;
setInterval(function(){
if(interval == 5){
/* if intervall reaches 5 the user is inactive hide element/s */
$('input').hide();
interval = 1;
}
interval = interval+1;
console.log(interval);
},1000);
$(document).bind('mousemove keypress', function() {
/* on mousemove or keypressed show the hidden input (user active) */
$('input').show();
interval = 1;
});希望它离你想要的不远。干杯!
https://stackoverflow.com/questions/6741192
复制相似问题