0

I have this code to fade out some stuff on a page after you don't move your mouse for a second or so:

idleTime = 0;
var idleInterval = setInterval(function() { 
    idleTime++;
    if (idleTime > 1) {
        var isHovered = $('.fade-outs').is(":hover");
        if(isHovered == false) {
            $('.fade-outs').stop().fadeOut();
        }
    }
}, 1000);
$(document).bind('mousemove mousedown', function(e) {
    idleTime = 0;
    $('.fade-outs').fadeIn();
});

But, I am getting the following error with $('.fade-outs').is(":hover"); part:

Error: Syntax error, unrecognized expression: hover [http://localhost:5545/assets/js/jquery.min.js:3]

Does anyone know why I am getting this error?

4

2 回答 2

3

I would try something like this instead:

var idleTimer;

function startTimer() {
    stopTimer();

    idleTimer = setInterval(function() {
        $('.fade-outs').stop().fadeOut();
    }, 1000);
}

function stopTimer() {
    idleTimer && clearInterval(idleTimer);
}

$(document).on('mousemove', startTimer);

$('.fade-outs').on({
    mouseleave: startTimer,
    mouseenter: stopTimer,
    mousemove: function(e) {
        e.stopPropagation();
    }
});​

Demo: http://jsfiddle.net/Blender/urzug/4/

于 2012-09-30T06:07:16.083 回答
1

Try changing it to this:

idleTime = 0;
var idleInterval = setInterval(function() { 
idleTime++;
if (idleTime > 1) {
    var isHovered = $('.fade-outs').is(".hover");
    if(isHovered == false) {
        $('.fade-outs').stop().fadeOut();
    }
}
}, 1000);
$(document).bind('mousemove mousedown', function(e){
  idleTime = 0;
  $('.fade-outs').fadeIn();
});
$('.fade-outs').hover(funciton() { $(this).toggleClass('hover') });
于 2012-09-30T05:52:49.307 回答