javascript jQuery - 简单的屏幕保护程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3745076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - Simple Screensaver
提问by Peter
I try to build a really simple screensaver, but it is not as easy as I thought.
我尝试构建一个非常简单的屏幕保护程序,但它并不像我想象的那么容易。
My solution did not really work and it is IMHO really dirty.
我的解决方案并没有真正奏效,恕我直言,它真的很脏。
Did anyone have a good clean idea? Maybe without a timeout?
有没有人有一个干净的好主意?也许没有超时?
HTML
HTML
<div id="screensaver" style="width:100%; height:100%; background-color:#000000; display:none;" > </div>
JS
JS
$('body').live('mousemove', function (e)
{
if (e.type == 'mousemove')
{
clearTimeout(s_saver);
s_saver = setTimeout('$(\'#screensaver\').fadeIn();', 4000);
$('#screensaver').hide();
}
});
Thanks in advance!
Peter
提前致谢!
彼得
回答by Yi Jiang
The main problem with your script is that the s_savervariable is not declared properly, and is in the wrong scope - you need it to still be read the next time the event handler is called, so you should declare it outside the scope of the handler. This should work (jsfiddle version):
您的脚本的主要问题是s_saver变量没有正确声明,并且在错误的范围内 - 下次调用事件处理程序时您仍然需要读取它,因此您应该在处理程序的范围之外声明它。这应该有效(jsfiddle 版本):
var s_saver;
$('body').mousemove(function() {
clearTimeout(s_saver);
s_saver = setTimeout(function(){
$('#screensaver').fadeIn(900);
}, 4000);
$('#screensaver').fadeOut(100);
});
Of course this is still dependent on what you want to achieve. If, for instance, you want to show something while your user isn't looking at this particular tab/window instead of just not moving the mouse, then the solution provided in this question should do: How to detect inactive tab and fill it with color
当然,这仍然取决于您想要实现的目标。例如,如果您想在用户没有查看此特定选项卡/窗口而不是只是不移动鼠标时显示某些内容,则此问题中提供的解决方案应该这样做:如何检测非活动选项卡并用颜色
回答by rem1x
Dont you think the browser will hang listening to contineous mouse move events....? not to demotivate you but just an idea.
你不认为浏览器会挂起听连续的鼠标移动事件......?不是为了让你失去动力,而只是一个想法。

