JSFIDDLE 演示
每当用户在#gallery 上触发 mousemove 或 keypress 事件时,延迟计时器都会重置。您还可以在其中添加悬停事件,以确保菜单在悬停在#gallery 上时永远不会隐藏。
var interval = 1;
function delayCheck()
{
if(interval == 5)
{
$('#menuwrap').animate({top: '-50px'}, 100);
interval = 1;
}
interval = interval+1; console.log(interval);
}
$('#gallery').bind('mousemove keypress', function() {
$('#menuwrap').animate({top: '0'}, 100);
interval = 1;
// reset the delay timer
clearInterval(_delay); console.log('reset timer');
_delay = setInterval(delayCheck, 500);
});
// starts delay timer when page loads
_delay = setInterval(delayCheck, 500);
回答第 2 部分
JSFIDDLE 演示 #2
仅在触发 #gallery 事件 mousemove 或 keypress 后检查不活动。一旦用户移出#gallery 框,它将终止delayCheck() 并将#menuwrap 设置回顶部:-50px。
var interval = 1;
_delay = 0;
function delayCheck()
{
if(interval == 5)
{
$('#menuwrap').animate({top: '-50px'}, 100);
interval = 1;
clearInterval(_delay);
}
interval = interval+1;
console.log(interval);
}
// turn delayCheck() ON, when mousing to #gallery
$('#gallery').bind('mousemove keypress', function()
{
console.log("mousemove keypress");
$('#menuwrap').animate({top: '0'}, 100);
interval = 1;
// reset delayCheck
clearInterval(_delay);
_delay = setInterval(delayCheck, 500);
});
// turn delayCheck() OFF, when mousing out of #gallery
$('#gallery').mouseout(function(){
console.log("mouseout");
$('#menuwrap').animate({top: '-50px'}, 100);
interval = 1;
clearInterval(_delay);
});