<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>缓冲运动2</title>
        <style type="text/css">
        body{
            height: 2000px;
        }
            .div {
                width: 100px;
                height: 100px;
                background-color: darkslateblue;
                position: absolute;
                right: 0;
                bottom: 0;
                cursor: pointer;
            }
        </style>
        <script type="text/javascript">
        window.onscroll= function(){
            //1,给浏览器的滚动添加事件,onscollTop事件
            var oDiv = document.getElementById('div');
            var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
//            oDiv.style.top = document.documentElement.clientHeight-oDiv.offsetHeight+scrollTop+'px';
            //可视区域的高度减掉物体高度再加上向上滚动的高度
            starMove(document.documentElement.clientHeight-oDiv.offsetHeight+scrollTop)
        };
        var timer = null;
        function starMove(iTarget){
            var oDiv = document.getElementById('div');
            clearInterval(timer);
            timer = setInterval(function(){
                var speed = (iTarget-oDiv.offsetTop)/4;
                speed=speed>0?Math.ceil(speed):Math.floor(speed);
                if(oDiv.offsetTop==iTarget){
                    clearInterval(timer)
                }else{
                    oDiv.style.top = oDiv.offsetTop+speed+'px';
                }
            },30)
        }
        
        </script>
    </head>

    <body>
        <div class="div" id="div"></div>
    
    </body>

</html>

课程链接:智能社的开发教程:https://ke.qq.com/webcourse/index.html#course_id=152997&term_id=100174752&taid=766913655494053&vid=v14127nxshc

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2021-11-02
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-27
  • 2021-10-09
  • 2021-06-04
  • 2021-12-15
  • 2022-12-23
  • 2021-06-08
  • 2021-10-12
相关资源
相似解决方案