图片隐藏,鼠标移动时淡入、淡出并再次淡入

4

我试图在鼠标没有动作3秒钟后淡出图像,然后当鼠标再次移动时淡入。

如果有人告诉我如何让图像隐藏直到鼠标移动,我将不胜感激。因此,当页面加载时,您看不到图像,直到鼠标移动。

这是我目前的代码...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
    <html>
        <head>
        <title></title>
        <script type="text/javascript">
            var timer;
            $(document).mousemove(function() {
            if (timer) {
            clearTimeout(timer);
            timer = 0;
            }
            $('#top:visible').fadeIn();
            timer = setTimeout(function() {
            $('#top').fadeOut()
            }, 3000)
            })
        </script>
        </head>
        <body>
            <div id="top">
                <img src="graphics/logo/logo.psd" title="" alt="">
            </div>
        </body>
    </html>

非常感谢您的帮助!
2个回答

2

我已经更新了我的回答,提供了一个全面的页面。希望这样做能让事情更清晰明了。最好将JavaScript放在自己的文件中,但这将让您开始运行。

请确保这一行:

<script type="text/javascript" src="jquery-1.4.1.min.js"></script>

请确保您的jQuery文件位置和名称正确。

让我知道进展如何。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>tester page</title>  

<style> 
    <!--

    -->
 </style>

<script type="text/javascript" src="jquery-1.4.1.min.js"></script>

<script type="text/javascript"> 

    $(document).ready(function() {
    var $top = $('#top');
    var $document = $(document);
    var timer = null;
    var timerIsRunning = false;

    $top.hide();

    $('#menu').mousemove(function(e){
        e.stopPropagation();
    });
    setTimeout(function() {
                        $document.mousemove(function(e) {
                                if($top.is(':hidden')) {
                                    $top.fadeIn();
                                } else {
                                    if(!timerIsRunning) {
                                        timerIsRunning = true;
                                        clearTimeout(timer);
                                        timer = setTimeout(function() { $top.fadeOut();  }, 5000);
                                        setTimeout(function() {timerIsRunning = false;}, 2000);
                                    }
                                }
                        });
                }, 500);

});

</script>
</head>
<body>
<div id="top">
   <img src="graphics/logo/logo.psd" title="" alt="">
</div>
</body>
</html>

谢谢你的帮助,Patrick,但我似乎无法让它工作 :( - Lozano
抱歉,我没有仔细关注你代码的其余部分。你的HTML文件中是否已经导入了jQuery?我将在一分钟内更新我的答案,提供更全面的解决方案。 - user113716
没问题。看起来即使没有移动,mousemove事件在页面加载时也会被触发。所以我所做的是更新我的答案,使用另一个setTimeout延迟约半秒钟分配'mousemove'事件处理程序。这样,当文档触发其初始的'mousemove'时,处理程序就不会接收到它。 - user113716
第二部分:该网站非常简单。有两个div部分,其中一个是标题。当您查看第二个div中的图像时,我不想显示标题。当用户感到困惑时,他/她会开始移动鼠标,然后标题将出现并带有简短的描述。因此,在用户处理页面主要部分的div时,标题保持空白,但当他们感到困惑并开始移动时,标题就会出现。 - Lozano
好的,我明白了。我编辑了我的答案。我在“#menu”(名称可能不正确)上添加了一个“mousemove”事件处理程序,然后引用了该事件并调用了stopPropagation()。由于jQuery中的事件从最内部元素向上冒泡到顶部(文档),因此您可以使用stopPropagation()停止冒泡。这样,您的菜单将捕获'mousemove'事件,并阻止其进一步传播。我还添加了一行代码,使“#top”立即淡出。 - user113716
显示剩余16条评论

1

试试这个:

$(document).ready(function() {
    var timer;
    // hide initially
    $('#top').hide();

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

谢谢Ben,看起来好像不起作用。你能给我展示一下你如何在脚本中布局来确保这不是因为我太菜鸟了吗? - Lozano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接