A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 不二晨 金牌黑马   /  2019-1-18 09:27  /  1277 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

定时器弹框

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>定时器弹框</title>
        <style type="text/css">
                .pop{
                        width: 400px;
                        height: 300px;
                        background-color: #fff;
                        border: 1px solid #000;
                        /*固定定位*/
                        position: fixed;
                        /*左上角位于页面中心*/
                        left: 50%;
                        top: 50%;
                        /*让div向左偏移半个宽度、向上偏移半个高度,使div位于页面中心*/
                        margin-left: -200px;
                        margin-top: -150px;
                        /*弹窗在最上面*/
                        z-index: 9999;
                }
                /*遮罩样式*/
                .mask{
                        position: fixed;
                        width: 100%;
                        height: 100%;
                        background-color: #000;
                        left: 0;
                        top: 0;
                        /*设置透明度30%*/
                        opacity: 0.3;
                        filter: alpha(opacity=30);/*兼容IE6、7、8*/
                        /*遮罩在弹窗的下面,在网页所有内容的上面*/
                        z-index: 9990;
                }
                .pop_con{
                        display: none;/*默认不显示,用定时器显示*/
                }
        </style>
        <script type="text/javascript">
                /*
                setTimeout                只执行一次的定时器
                clearTimeout        关闭只执行一次的定时器
                setInterval                反复执行的定时器
                clearInterval        关闭反复执行的定时器
                */

                window.onload = function(){
                        var oPop = document.getElementById('pop');
                        var oShut = document.getElementById('shutOff');

                        /*setTimeout(showPop, 3000);//开启定时器,3秒后调用函数showPop()弹框

                        function showPop(){
                                oPop.style.display = 'block';//显示弹框和遮罩
                        }*/
                        //开启定时器的简写方式:调用匿名函数
                        setTimeout(function(){
                                oPop.style.display = 'block';
                        }, 3000);

                        oShut.onclick = function(){
                                oPop.style.display = 'none';//关闭弹框和遮罩
                        }
                }
        </script>
</head>
<body>
        <h1>首页标题</h1>
        <p>页面内容</p>
        <a href="http://www.baidu.com">百度网</a>


        <div class="pop_con" id="pop">
                <div class="pop">
                        <h3>提示信息!</h3>
                        <a href="#" id="shutOff">关闭</a>
                </div>
                <div class="mask"></div>
        </div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
定时器的基本用法

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>定时器的基本用法</title>
        <script type="text/javascript">
                //单次定时器
                var timer = setTimeout(function(){
                        alert('hello!');
                }, 3000);

                //清除单次定时器
                clearTimeout(timer);

                //反复循环定时器
                var timer2 = setInterval(function(){
                        alert('hi~~~');
                }, 2000);

                //清除反复循环定时器
                clearInterval(timer2);
        </script>
</head>
<body>
       
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
定时器动画

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>定时器动画</title>
        <style type="text/css">
                .box{
                        width: 100px;
                        height: 100px;
                        background-color: gold;
                        position: fixed;
                        left: 20px;
                        top: 20px;
                }
        </style>
        <script type="text/javascript">
                window.onload = function(){
                        var oBox = document.getElementById('box');

                        var left = 20;
                        //反复循环定时器,每30毫秒修改一次盒子的left值
                        var timer = setInterval(function(){
                                left += 2;
                                oBox.style.left = left + 'px';

                                //当left值大于700时停止动画(清除定时器)
                                if(left > 700){
                                        clearInterval(timer);
                                }
                        },30);
                }
        </script>
</head>
<body>
        <div class="box" id="box"></div>
</body>
</html>
---------------------
【转载,仅作分享,侵删】
作者:YRyr.*
原文:https://blog.csdn.net/weixin_43152725/article/details/86171947


2 个回复

倒序浏览
奈斯,感谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马