js控制动画

效果如下:
在这里插入图片描述

代码如下(可以直接复制在本地观看效果):

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <style type="text/css">
        .box {
            margin-top: 200px;
            width: 228px;
            height: 156px;
            position: relative;
            background-color: pink;
            animation-name: test, tomove;
            animation-duration: 1s;
            animation-timing-function: linear;
            /* 匀速 */
        }
        
        .con {
            width: 50px;
            height: 156px;
            background-color: black;
            color: white;
            text-align: center;
        }
        
        .first,
        .second,
        .third {
            margin-top: 200px;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: red;
            position: absolute;
            transform: translate(-50%, -50%);
        }
        
        .first {
            left: 900px;
            top: 0;
        }
        
        .second {
            left: 600px;
            top: 300px;
        }
        
        .third {
            left: 300px;
            top: 0px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="con">头部</div>
    </div>
    <div class="first"></div>
    <div class="second"></div>
    <div class="third"></div>

    <script type="text/javascript">
        var newx = 600
        var oldx = 900
        var newy = 300
        var oldy = 0
        var i = 0


        carmove(i)
        setTimeout(
            () => {
                newx = 300
                oldx = 600
                newy = 0
                oldy = 300
                i = i + 2
                $('.box').css({
                    "animation-duration": i + "s", //1s后上个动画执行完毕,让新的动画时间为2-1=1s
                    "animation-fill-mode": "forwards", // "animation-fill-mode": "forwards", 最后一帧结束后停止
                })
                carmove(i)
            },
            1000

        );

        function getTanDeg(y) {
            return Math.atan(y) * 180 / Math.PI
        }

        function carmove(i) {
            var deg = getTanDeg((oldx - newx) / (oldy - newy)) //计算两次坐标偏转的角度,自己可以画图看下
            console.log(deg)
            $(".box").css("transform", "translate(-50%, -50%) rotate(" + deg + "deg)"); //translate(-50%,-50%)让盒子位于自己中心
            var style = document.styleSheets[0] //获取html中第1个样式表中的第一条规则
            console.log(style.cssRules)
            style.insertRule("@keyframes test{from{ left: " + oldx + "px; }to{ left: " + newx + "px;}}", i); //js写入样式
            style.insertRule("@keyframes tomove{from{ top: " + oldy + "px; }to{ top: " + newy + "px;}}", 1 + i);
        }
    </script>
</body>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值