JavaScript - 延时提示框 -

image

开启定时器

  • setInterval 【间隔性】一旦启动就不会停下来
  • setTimeout【延时性】只执行一次

停止定时器

  • clearInterval
  • clearTimeout
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style media="screen">
        #div1 {
            width: 200px;
            height: 30px;
            background-color: red;
        }
        #div2 {
            width: 150px;
            height: 200px;
            background-color: gray;
            margin: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div id="div1"></div>
    <div id="div2"></div>
    <script type="text/javascript">
        window.onload = function () {
            var oDiv1 = document.getElementById('div1');
            var oDiv2 = document.getElementById('div2');
            var timer = null;

            oDiv1.onmouseover = function () { 
                oDiv2.style.display = 'block';   //步骤1、鼠标移入div1.显示div2
                clearTimeout(timer);          //步骤6、
            }
            oDiv1.onmouseout = function () {
                timer = setTimeout(function () { //步骤2、鼠标移除div1.延时 0.3s 消失div2
                    oDiv2.style.display = 'none';
                },300);
            }
            oDiv2.onmouseover = function () {
                oDiv2.style.display = 'block';   //步骤3、鼠标移入div2.显示Div2
                clearTimeout(timer);             //步骤4、清除步骤2.样式
            }
            oDiv2.onmouseout = function () {    
                timer = setTimeout(function () { //步骤5、鼠标移除div2.延时 0.3s 消失div2
                    oDiv2.style.display = 'none';
                },300);
            }
        }
    </script>
</body>
</html>

js部分最终简化代码为

    <script >
        window.onload = function () {
            var oDiv1 = document.getElementById('div1');
            var oDiv2 = document.getElementById('div2');
            var timer = null;

            oDiv1.onmouseover = oDiv2.onmouseover = function () {
                oDiv2.style.display = 'block';
                clearTimeout(timer);
            }
            oDiv1.onmouseout = oDiv2.onmouseout = function () {
                timer = setTimeout(function () {
                    oDiv2.style.display = 'none';
                },500);
            }
        }
    </script>

jQuery写法

$CodeBox1.hover(function() {
    $erwCode1.show(0)
    $erwCode2.hide(0).stop(false,false);
}, function() {
    $erwCode1.delay(500).hide(0)
});

$CodeBox2.hover(function() {
    $erwCode2.show(0)
    $erwCode1.hide(0).stop(false,false);
}, function() {
    $erwCode2.delay(500).hide(0)
});

$erwCode1.hover(function() {
    $erwCode1.show(0)
    $erwCode2.hide(0).stop(false,false);
}, function() {
    $erwCode1.hide(0)
});

$erwCode2.hover(function() {
    $erwCode2.show(0)
    $erwCode1.hide(0).stop(false,false);
}, function() {
    $erwCode2.hide(0)
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInt...
    晚晴幽草阅读 5,589评论 1 18
  • JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInt...
    许先生__阅读 3,725评论 0 1
  • 0.前言 有一段时间没有写博客了,不过令我高兴地是写了这么长时间的文章,终于收到了关注和评价,在首页上通过了文章,...
    紫荆峰阅读 4,852评论 3 6
  • 配置PHP开发环境的时候,当进行到在Apache的httpd.conf文件中配置加载PHP模块时发生如下错误 ht...
    itbsl阅读 6,243评论 2 1
  • 今天是你订婚的日子,不舍开心难过幸福!以后不能再和你肆无忌惮的玩闹,不会在我心情不好的时候陪我聊天,不会在缠着...
    sweet12阅读 1,603评论 0 0

友情链接更多精彩内容