前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JS-缓冲运动-对联型悬浮框

JS-缓冲运动-对联型悬浮框

作者头像
xing.org1^
发布2018-05-17 16:28:40
3K0
发布2018-05-17 16:28:40
举报
文章被收录于专栏:前端说吧前端说吧
代码语言:javascript
复制
<!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)/2+scrollTop+'px';
            //可视区域的高度减掉物体高度再加上向上滚动的高度
            starMove(parseInt((document.documentElement.clientHeight-oDiv.offsetHeight)/2+scrollTop));
            //除以2以后就一直抖,就用parseInt取整
        };
        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{
                    document.title = iTarget;
                    document.getElementById('btn').value=oDiv.offsetTop;
                    oDiv.style.top = oDiv.offsetTop+speed+'px';
                }
            },30)
        }
        
        </script>
    </head>

    <body>
        <div class="div" id="div"></div>
    <input type="text" value="" id="btn" style="top: 0;
        right: 0;
        position: fixed;"/>
    </body>

</html>

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档