js特效4(3个特效)

1.手表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>手表</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        #box{
            position: relative;/*相对定位*/
            width: 600px;
            height: 600px;
            background: url("image/clock.jpg") no-repeat;
            margin: 20px auto;
        }
        #hour, #min, #sec{
            position: absolute;
            left: 50%;
            top: 0;
            width: 30px;
            height: 600px;
            margin-left: -15px;
        }
        #hour{
            background: url("image/hour.png") no-repeat center center;

        }
        #min{
             background: url("image/minute.png") no-repeat center center;
         }
        #sec{
            background: url("image/second.png") no-repeat center center;
        }
    </style>
</head>
<body>
<div id="box">
    <div id="hour"></div>
    <div id="min"></div>
    <div id="sec"></div>
</div>
<script>
    window.onload = function (ev) {
        var hour = document.getElementById('hour');
        var min = document.getElementById('min');
        var sec = document.getElementById('sec');
        setInterval(function (ev1) {
            var data = new Date();
            var allmill = data.getMilliseconds();//当前的毫秒
            var se = data.getSeconds() + allmill / 1000;//秒+毫秒变成的秒
            var mi = data.getMinutes() + se / 60;
            var hou = data.getHours() + mi /60;
            //做旋转
            hour.style.transform = 'rotate(' + hou * 30 + 'deg)';//一小时30度
            min.style.transform = 'rotate(' + mi * 6 + 'deg)';//一分钟走6度
            sec.style.transform = 'rotate(' + se * 6 + 'deg)';//一秒走6度
        },10);
    }
</script>
</body>
</html>

2.定时器累加

用事件控制进入定时器时,可能会发生定时器累加,所以一定要遵循“先清,后设”。
先清除定时器再设置定时器。
清除定时器clearInterval(定时器名字);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>练习</title>
</head>
<body>
<div id="box" style="width: 100px; height: 100px; background-color: red"> </div>
<script>
    window.onload = function (ev) {
        var box = document.getElementById('box');
        var num = 0, ding = null;
        box.addEventListener('mouseover',function (ev1) {
            clearInterval(ding);
            ding = setInterval(function () {
                num ++;
                console.log(num);
            },1000)
        })
    }
</script>
</body>
</html

3.长图滚动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>长图滚动</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        body{
            background-color: black;
        }
        #box{
            width: 750px;
            height: 400px;
            border: 1px solid red;
            margin: 100px auto;
            overflow: hidden;
            position: relative;
        }
        #prc{
            position: absolute;
            left: 0;
            top: 0;
        }
        #top,#bottom{
            width: 100%;
            height: 50%;
            position: absolute;
            left: 0;
            cursor: pointer;
        }
        #top{
            top:0;
            /*background-color: #00CCFF;*/
        }
        #bottom{
            bottom: 0;
            /*background-color: #00FF33;*/
        }
    </style>
</head>
<body>
<div id="box">
    <img src="image/timg.jpg" id="prc">
    <span id="top"></span>
    <span id="bottom"></span>
</div>
<script>
    window.onload = function (ev) {
        var box = f('box');
        var prc = f('prc');
        var top = f('top');
        var bottom = f('bottom');
        var ding ,long = 0;//定时器和距离
        //鼠标进入
        //向上
        top.onmouseover = function (ev1) {
            clearInterval(ding);
            ding = setInterval(function () {
                long -=10;
                if (long > -2466){//图片长2866,box宽度是400
                    prc.style.top = long + 'px';
                }
                else {
                    clearInterval(ding);
                }

            },40)
        };
        //向下
        bottom.onmouseover = function (ev2) {
            clearInterval(ding);
            ding = setInterval(function () {
                long +=10;
                if (long < 0){//向下top最大为0
                    prc.style.top = long + 'px';
                }
                else {
                    clearInterval(ding);
                }

            },40)
        };
        //鼠标离开
        box.onmouseout = function () {
            clearInterval(ding);
        }
    };
    function f(id) {
        return document.getElementById(id);
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值