javascript - JS怎么让绕着圆运动的物体,速度慢慢减小,最后停下来?
黄舟
黄舟 2017-04-11 09:46:18
[JavaScript讨论组]

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .js-circle-wrap{
            position: absolute;
            top: 100px;
            left: 600px;
            width: 400px;height: 400px;
            border: 1px solid red;
            border-radius: 50%;
        }
        .circle{
            width: 50px;
            height: 50px;
            border-radius:50%;
            position: absolute;
            left: 0;top: 0;
            background-color: lightblue;
        }
    </style>
</head>

<body>
    <p class="js-circle-wrap">
        <p class="circle"></p>
    </p>
</body>
<script src="./jquery-3.1.0.js"></script>
<script>
let wrap = document.querySelector('.js-circle-wrap');
let origX = parseInt(window.getComputedStyle(wrap,null).width)/2;
let origY = parseInt(window.getComputedStyle(wrap,null).height)/2;
let r = origX;

let circle = document.querySelector('.circle');
let circleR = parseInt(window.getComputedStyle(circle,null).width)/2;
let x,y;
let ang = 0;
setInterval(function(){
    if (ang < 360){
        ang += 0.1;
    }else{
        ang = 360;
    }
    x = parseInt(origX + r*Math.cos(ang) - circleR);
    y = parseInt(origY + r*Math.sin(ang) -circleR);
    circle.style.left = x + "px";
    circle.style.top = y + "px";
},100);

</script>
</html>
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
黄舟
var speed = 0.1;//增加一个speed变量
setInterval(function(){
    speed = speed - 0.001;//让速度减小
    if (speed > 0){
        ang += speed;
    }else{
        clearInterval();//清除定时器
    }
    x = parseInt(origX + r*Math.cos(ang) - circleR);
    y = parseInt(origY + r*Math.sin(ang) -circleR);
    circle.style.left = x + "px";
    circle.style.top = y + "px";
},100);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号