• JS运动基础


    让Div运动起来

    运动中的bug:

    1.不会停止

    2.速度取某些值会无法停止

    3.到达位置后在点击还会运动

    4.重复点击速度加快

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #div1 {
                width: 200px;
                height: 200px;
                background: red;
                position: absolute;
                top: 50px;
                left: 0px;
            }
        </style>
        <script>
            window.onload = function () {
                var oBtn = document.getElementById('btn');var oDiv = document.getElementById('div1');
                var time = null;
                var spead = 1;
                oBtn.onclick = function () {
                    clearInterval(time);//解决:重复点击速度加快
                    time = setInterval(function () {
                        if (oDiv.offsetLeft >= 300) {//解决:spead取值----速度取某些值会无法停止
                            clearInterval(time)
                        }else{//到达位置后在点击还会运动
                            oDiv.style.left = oDiv.offsetLeft + spead + 'px'
                        }
                       
                    }, 10)
                }
            }
    
        </script>
    </head>
    
    <body>
        <input id="btn" type="button" value="开始运动">
        <div id="div1"></div>
    </body>
    
    </html>

    运动框架及应用

    运动框架

    1.在开始运动时,关闭已有定时器

    2.把运动和停止隔开(if/else)

    缓冲运动

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #div {
                width: 200px;
                height: 200px;
                background: red;
                position: absolute;
                top: 50px;
                left: 0;
            }
        </style>
        <script>
            window.onload = function () {
                var oBtn = document.getElementById('btn');
                var oDiv = document.getElementById('div');
    
                oBtn.onclick = function () {
                    setInterval(function () {
                        //Math.ceil()向上取整
                        //Math.floor()向下取整
                        var speed = (300 - oDiv.offsetLeft) / 10;
                        speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)
                        oDiv.style.left = oDiv.offsetLeft + speed + 'px'
                        document.title = oDiv.offsetLeft + speed + 'px'
                    }, 30)
                }
    
            }
        </script>
    </head>
    
    <body>
        <input id="btn" type="button" value="缓冲运动">
        <div id="div"></div>
    </body>
    
    </html>

    右侧悬浮框

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #div{
                width: 200px;
                height: 100px;
                background: red;
                position: absolute;
                right: 0;
                bottom: 40%;
            }
        </style>
        <script>
            window.onscroll = function () {
                var oDiv = document.getElementById('div');
                var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
                //oDiv.style.top = (document.documentElement.clientHeight-oDiv.offsetHeight)/2+scrollTop+'px';
                startMove(parseInt((document.documentElement.clientHeight-oDiv.offsetHeight)/2+scrollTop));
            };
            var time = null;
            function startMove(iTarget){
                var oDiv = document.getElementById('div');
                //先停止计时器
                clearInterval(time);
                time = setInterval(function(){
                    var speed = (iTarget-oDiv.offsetTop)/8;
                    //判断speed的值上下取整
                    speed = speed>0?Math.ceil(speed):Math.floor(speed);
                    if(oDiv.offsetTop == iTarget){
                        clearInterval(time)
                    }else{
                        oDiv.style.top = oDiv.offsetTop+speed+'px';
                    }
                },30)
            }
        </script>
    </head>
    <body style="height: 2000px;">
        <div id="div"></div>
    </body>
    </html>
  • 相关阅读:
    CF995A Tesla
    CF961D Pair Of Lines
    P1186 玛丽卡
    CF986B Petr and Permutations
    hdu6331 Problem M. Walking Plan
    Edison UVALive3488
    Be a Smart Raftsman SGU475
    100198H Royal Federation
    100197G Robbers
    Evil Book -- CodeChef
  • 原文地址:https://www.cnblogs.com/520yh/p/12883150.html
Copyright © 2020-2023  润新知