HTML 之 定时器

定时器

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>定时器</title>
        <style type="text/css">
            #first{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                left: 0px;
            }
        </style>
    </head>
    <body>
        <div id="first"></div>
    </body>
    <script type="text/javascript">
        // 一次性定时器/延时器
        // 第一种写法
        setTimeout(function(){

        }, 1000);
        // 第二种写法
        setTimeout(f, 1000);
        function f(){
            console.log("执行 f");
        }

        // 循环定时器
        var timer = setInterval(function(){
            console.log("循环定时器");
        }, 2000);

        setTimeout(function(){
            // 清除定时器
            clearInterval(timer);
            // clearTimeout()
        }, 8000);

        var first = document.getElementById("first");
        first.onclick = function(){
            var move = setInterval(function(){
                first.style.left = first.offsetLeft + 5 + "px";
                if (first.offsetLeft >= 500) {
                    clearInterval(move);
                }
            }, 20)
        }
    </script>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>轮播图</title>
        <style type="text/css">
            .box{
                margin: 50px auto;
                height: 400px;
                width: 600px;
                border: 5px green solid;
                overflow: hidden;
                position: relative;
            }
            .hidebox{
                position: absolute;
                width: 3600px;
                height: 400px;
                z-index: 1;
            }
            .hidebox div{
                height: 400px;
                width: 600px;
                float: left;
                font-size: 50px;
                line-height: 400px;
                text-align: center;
                background-size:600px 400px;
            }
            .one{
                background: url("images/8.png");
            }
            .two{
                background: url("images/5.jpg");
            }
            .three{
                background: url("images/4.jpg");
            }
            .four{
                background: url("images/3.jpg");
            }
            input{
                /*-webkit-appearance: button;*/
                position: relative;
                z-index: 100;
                top: 185px;
                width: 30px;
                height: 30px;
                background-color: #03030355;
                border: 0;
                color: white;
                display: none;
            }
            .box:hover input{
                display: inline-block;
            }
            .left{
                border-radius: 0 15px 15px 0;
                margin-left: -5px;
                float: left;
            }
            .left:hover, .right:hover{
                background-color: #03030399;
            }
            .right{
                border-radius: 15px 0 0 15px;
                margin-right: -5px;
                float: right;
            }
            .list{
                height:20px;
                width:100px;
                position: absolute;
                left:200px;
                bottom: 0px;
                opacity: 0.8;
                filter: alpha(opacity=80); 
                z-index: 100;
            }
            .list li{
                height:20px;
                width:20px;
                background: #ccc;
                border-radius: 50%;
                float: left;
                margin-right:5px;
                cursor: pointer;
                list-style: none;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <input type="button" value="&lt" class="left">
            <div class="hidebox">
                <div class="four"></div>
                <div class="one"></div>
                <div class="two"></div>
                <div class="three"></div>
                <div class="four"></div>
                <div class="one"></div>
            </div>
            <input type="button" value="&gt" class="right">
            <ul class="list">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </body>
    <script type="text/javascript">
        var hidebox = document.getElementsByClassName("hidebox")[0];
        var left = document.getElementsByClassName("left")[0];
        var right = document.getElementsByClassName("right")[0];
        var box = document.getElementsByClassName("box")[0];
        var list = document.getElementsByTagName("li");
        hidebox.style.left = hidebox.offsetLeft - 600 + "px";
        var temptimer;
        function play(){
            temptimer = setInterval(function(){
                var timer = setInterval(function(){
                    hidebox.style.left = hidebox.offsetLeft - 50 + "px";
                }, 10);
                setTimeout(function(){
                    clearInterval(timer);
                }, 120);
                if (hidebox.offsetLeft <= -2400) {
                    hidebox.style.left = 0;
                }
                for (var i = 0; i < list.length; i++) {
                    list[i].style.backgroundColor = "grey";
                }
                var num = hidebox.offsetLeft / -600;
                list[num].style.backgroundColor = "black";
            }, 2000);
        }
        play();
        function stop(){
            setTimeout(function(){
                clearInterval(temptimer);
            });
        }
        box.onmouseover = stop;
        box.onmouseout = play;
        list[0].style.backgroundColor = "black";
        for (var i = 0; i < list.length; i++) {
            list[i].temp = i;
            var width = -600;
            list[i].onmouseover = function(){
                hidebox.style.left = width * (this.temp + 1) + "px";
                for (var i = 0; i < list.length; i++) {
                    list[i].style.backgroundColor = "grey";
                }
                list[this.temp].style.backgroundColor = "black";
            }
        }

        left.onclick = function(){
            var timers = setInterval(function(){
                hidebox.style.left = hidebox.offsetLeft + 50 + "px";
            }, 10);
            setTimeout(function(){
                clearInterval(timers);
            }, 120);
            if (hidebox.offsetLeft == -600) {
                hidebox.style.left = -3000 + "px";
            }
            for (var i = 0; i < list.length; i++) {
                    list[i].style.backgroundColor = "grey";
            }
            var num = hidebox.offsetLeft / -600 - 2;
            list[num].style.backgroundColor = "black";
        };

        right.onclick = function(){
            var timers = setInterval(function(){
                hidebox.style.left = hidebox.offsetLeft - 50 + "px";
            }, 10);
            setTimeout(function(){
                clearInterval(timers);
            }, 120);
            if (hidebox.offsetLeft == -2400) {
                hidebox.style.left = 0 + "px";
            }
            for (var i = 0; i < list.length; i++) {
                    list[i].style.backgroundColor = "grey";
            }
            var num = hidebox.offsetLeft / -600;
            list[num].style.backgroundColor = "black";
        };
    </script>
</html>

这里写图片描述

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
            .item{
                width: 60px;
                border-radius: 50px;
                position: absolute;
                left: 10px;
                top: 0
            }
        </style>
    </head>
    <body>
        <input type="button" value="开始" id="start">
        <input type="button" value="停止" id="stop">
    </body>
    <script type="text/javascript">
        var start = document.getElementById("start");
        var stop = document.getElementById("stop");
        var createTimer;
        stop.onclick = function(){
            clearInterval(createTimer);
        };
        start.onclick = function(){
            createTimer = setInterval(function(){
                var gold = document.createElement("img");
                gold.src = "images/金币.jpg";
                gold.className = "item";
                gold.style.left = Math.random() * 960 + 100 + "px";
                document.getElementsByTagName("body")[0].appendChild(gold);
                var downTimer = setInterval(function(){
                    gold.style.top = gold.offsetTop + 5 + "px";
                    if (gold.offsetTop >= 500) {
                        gold.remove();
                        clearInterval(downTimer);
                    }
                }, 20);
            }, 200);
        };
    </script>
</html>

这里写图片描述

http://blog.csdn.net/huzongnan/article/list

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值