zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    序列——堆排序-大根堆(堆大顶)
    3 分钟的高速体验 Apache Spark SQL
    UVa 1401 Remember the Word
    leetcode
    oracle看到用户的所有表名、表睐、字段名称、现场的目光、是空的、字段类型
    cocos2d-x物业现场
    Unable to start MySQL service. Another MySQL daemon is already running with the same UNIX socket
    ThreadPoolExecutor详解
    Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
    A First Exploration Of SolrCloud
  • 原文地址:https://www.cnblogs.com/520yh/p/12883150.html
Copyright © 2011-2022 走看看