js控制元素旋转速度

本来是项目中需要画一个叉叉,在一个单独的html页面中利用四个三角形挡住父元素的背景颜色的方法画出来之后,一时兴起用js让它转了起来,最后写了两个按钮控制了速度。
在这里插入图片描述

源码:

<!-- /* 
* @Author: Chauncey Yuan  
* @Date: 2019-10-20 20:46:37  
* @Last Modified by: Chauncey Yuan  
* @Last Modified time: 2019-10-19 22:46:37 
*/ -->
<!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>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        .main {
            width: 300px;
            height: 300px;
            background-color: #ab15fd;
            margin: 0 auto;
            margin-top: 100px;
            position: relative;
            border-radius: 20px;
            transform: rotate(0deg);
        }
        
        .top {
            width: 0px;
            height: 0px;
            border-top: 120px solid #fff;
            border-left: 120px solid transparent;
            border-right: 120px solid transparent;
            position: absolute;
            top: 0;
            left: 30px;
        }
        
        .left {
            width: 0px;
            height: 0px;
            border-left: 120px solid #fff;
            border-top: 120px solid transparent;
            border-bottom: 120px solid transparent;
            position: absolute;
            left: 0;
            top: 30px;
        }
        
        .right {
            width: 0px;
            height: 0px;
            border-right: 120px solid #fff;
            border-top: 120px solid transparent;
            border-bottom: 120px solid transparent;
            position: absolute;
            right: 0;
            top: 30px;
        }
        
        .bottom {
            width: 0px;
            height: 0px;
            border-bottom: 120px solid #fff;
            border-left: 120px solid transparent;
            border-right: 120px solid transparent;
            position: absolute;
            bottom: 0;
            left: 30px;
        }
        
        .btn {
            width: 20px;
            height: 20px;
            border-radius: 10px;
            background-color: #f00;
            position: absolute;
            top: 140px;
            left: 140px;
        }
        
        .speed {
            width: 300px;
            height: 50px;
            margin: 0 auto;
            margin-top: 150px;
            display: flex;
        }
        
        .down,
        .num,
        .up {
            /* display: block; */
            width: 120px;
            height: 50px;
            background-color: #ab15fd;
            text-align: center;
            line-height: 50px;
            font-size: 25px;
            color: #fff;
        }
        
        .down:hover,
        .up:hover {
            cursor: pointer;
        }
        
        .num {
            background-color: #fff;
            color: #ab15fd;
            flex: 1;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="top"></div>
        <div class="left"></div>
        <div class="right"></div>
        <div class="bottom"></div>
        <div class="btn"></div>
    </div>
    <div class="speed">
        <div class="down">减速</div>
        <div class="num"></div>
        <div class="up">加速</div>
    </div>
    <script>
        // 定义旋转角度
        var i = 0;
        // 定义初始速度
        var speed = 1;
        // 获取速度显示区域
        var num = document.getElementsByClassName("num")[0];
        // 写入初始速度
        num.innerHTML = speed;
        // 为减速按钮添加单击事件
        document.getElementsByClassName("down")[0].onclick = function() {
            // 如果速度大于0
            if (speed > 0) {
                // 速度减一
                speed--;
            }
            // 写入速度到页面
            num.innerHTML = speed;
        }
        为加速按钮添加单击事件
        document.getElementsByClassName("up")[0].onclick = function() {
            // 如果速度大于45
            if (speed < 45) {
                // 速度加一
                speed++;
                // 写入速度到页面
                num.innerHTML = speed;
            }
        }

        // 定义定时器
        var timer = setInterval(function() {
            // 设置旋转角度
            document.getElementsByClassName("main")[0].style.transform = "rotate(" + i + "deg)";
            // 旋转角度逐渐变化
            i += speed;
            // 防止角度无线变大
            if (i > 20000) {
                i = 0;
            }
        }, 20);
    </script>
</body>

</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值