zoukankan      html  css  js  c++  java
  • JS实现右侧悬浮框随着页面的上下轮动而移动

    <!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{
                 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)/6;
                    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>
    

      

  • 相关阅读:
    【转】awk内置变量
    【转】awk数组操作
    【转】awk 数组用法【精华贴】
    【转】linux shell 逻辑运算符、逻辑表达式
    指挥作战
    人脸相关
    TD
    后台
    前台 html 空格
    linux
  • 原文地址:https://www.cnblogs.com/520yh/p/13790535.html
Copyright © 2011-2022 走看看