Jquery: how to make something fade out when mouse is idle. When mouse moves again, It fadesIn!

后端 未结 1 1391
眼角桃花
眼角桃花 2020-12-13 22:35

I have a div called \"#top\". I would like it to fade out when the mouse is idle for 3 seconds. When the mouse moves again, make it appear (fade, of course

相关标签:
1条回答
  • 2020-12-13 23:01

    Use setTimeout, saving the return value somewhere (to cancel it with clearTimeout when the mouse moves again):

    var timer;
    $(document).mousemove(function() {
        if (timer) {
            clearTimeout(timer);
            timer = 0;
        }
    
        $('#top:visible').fadeIn();
        timer = setTimeout(function() {
            $('#top').fadeOut()
        }, 3000)
    })
    

    You'll want this inside $(document).ready() or the like.

    0 讨论(0)
提交回复
热议问题