圆周运动案例

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <style>
        .r1{
            width: 500px;
            height: 500px;
            border: 1px solid red;
            border-radius: 50%;
            position: relative;
        }
        .r1>div{
            position: absolute;
        }
        .r2{
            width: 300px;
            height: 300px;
            border: 1px solid green;
            border-radius: 50%;
            top: 100px;
            left: 100px;
        }
        .b1{
            width: 100px;
            height: 100px;
            background-color: rgb(17, 135, 220);
            border-radius: 50%;
            top: 200px;
            left: 0px;
        }
        .b2{
            width: 100px;
            height: 100px;
            background-color:#FFF8DC;
            border-radius: 50%;
            top: 200px;
            left: 100px;
        }
        .r3{
            width: 100px;
            height: 100px;
            border: 1px solid blue;
            border-radius: 50%;
            top: 200px;
            left: 200px;
        }
    </style>
</head>
<body>
    <div class="r1">
        <div class="b1" id="b1"></div>
        <div class="b2"></div>
        <div class="r2"></div>
        <div class="r3"></div>
    </div>
    <button onclick="start1()">开始</button>
    
</body>
<script>
    //r表示运动轨迹的半径,x0,y0表示圆心的坐标
    let r=200,x0=250,y0=250;
    
    //获取b1蓝色小球
    let b1=document.getElementById('b1')
  
    // b1.style.top=200+'px'
    //定义x,y表示小球的运动轨迹坐标
    let x=50,y=50
    function start1(){
        // 上半⚪
        let sby=setInterval(()=>{
        x++
        y=-Math.sqrt(Math.pow(r,2)-Math.pow(x-x0,2))+y0
        b1.style.top=y-50 +'px'
        b1.style.left=x-50 +'px'
        if(x==450&&y==250){
            // alert('右顶点')
            //取消下半圆的定时器
            clearInterval(sby)
            let xbr=setInterval(()=>{
                x--
                y=Math.sqrt(Math.pow(r,2)-Math.pow(x-x0,2))+y0
                b1.style.top=y-50 +'px'
                b1.style.left=x-50 +'px'
                if(x==50&&y==250){
                    //取消下半圆的定时器
                    clearInterval(xbr)
                    //开启上半圆定时器
                    start1()//递归
                }
            })
        }

        },1)

}

</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二价亚铁.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值