【问题标题】:Hide a bunch of Divs after user inactivity用户不活动后隐藏一堆 Div
【发布时间】:2011-10-08 03:28:07
【问题描述】:

我正在使用 Paul Irish 的空闲计时器插件: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 或 IE 8 / 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 事件,它会起作用。

【问题讨论】:

  • “显然 onmousemove 事件是问题所在” - 但您没有包含那部分代码
  • 抱歉,实际上我正在加载空闲计时器插件。在插件中,有如下代码:codeevents = 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove';code
  • @citizen con : 谢谢你的帮助,我已经编辑了主帖
  • 对不起,我一定很困惑,但它在 Chrome 中对我有用。 IE8 实际上每次都崩溃。
  • 还有一点,在 Chrome 和 IE 上,如果鼠标指针不在窗口顶部,则 div 会淡出。

标签: jquery jquery-plugins onmousemove idle-timer


【解决方案1】:

如果您不必使用插件并在 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;
});

希望它离你所追求的不远。 干杯!

【讨论】:

  • 我也为移动设备添加了 touchmove。 $(document).bind('mousemove keypress touchmove', function() {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多