Reputation: 6541
I'm creating a header menu that slides down when you move the mouse within in the browser window.
But I want to have it slide up after the mouse hasn't moved for 5 seconds.
Here is what I have so far: http://jsfiddle.net/BEzbw/
Upvotes: 0
Views: 759
Reputation: 9429
jQuery throttle/debounce is a great plugin for doing things like this safely. jsFiddle
$('html').mousemove( $.debounce( 250, true, function(e){
$('header').animate({ top: '0' }, 300)
}))
.mousemove( $.debounce( 5000, false, function(e){
$('header').animate({ top: '-60px' }, 300)
}));
ps: bear in mind that attaching to <html>
in that way could get your event blocked by other page elements (although unlikely for a mousemove event).
Upvotes: 1
Reputation: 13134
Something like this?
$(document).ready( function () {
$('header').css('top', '-60px');
$('html').mousemove(function(event) {
$('header').animate({ top: '0' }, 300);
});
//Increment the idle time counter every second.
idleTime = 0;
var idleInterval = setInterval("timerIncrement()", 6000);
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 4) { // 5sec
$('header').animate({ top: '-60px' }, 300);
}
}
});
Upvotes: 0
Reputation: 2481
give this a try
http://jsfiddle.net/lastrose/BEzbw/3/
might need to work on the timing though
Upvotes: 0