zoukankan      html  css  js  c++  java
  • 点滴积累【JS】---JS小功能(JS实现侧悬浮浮动)

    效果:

    思路:

    首先,加载onscroll控制滚动条。然后写缓存运动的方法,缓冲运动的方法是先计算出DIV缓冲的速度,并且将其取整,再进行运动判断什么时候到达终点。最后将其参数返回。再在onscroll里面调用此方法,并且将终点计算出来赋予此方法的参数。

    代码:

     1 <head runat="server">
     2     <title></title>
     3     <style type="text/css">
     4         #div1
     5         {
     6             width: 200px;
     7             height: 200px;
     8             background: #0000FF;
     9             position: absolute;
    10             right: 0;
    11             bottom: 0;
    12         }
    13     </style>
    14     <script type="text/javascript">
    15         window.onscroll = function () {
    16             var oDiv = document.getElementById('div1');
    17             var DivScroll = document.documentElement.scrollTop || document.body.scrollTop;      //获取移动高度
    18             //                        oDiv.style.top = (document.documentElement.clientHeight - oDiv.offsetHeight)/2 + DivScroll + 'px';
    19             move(parseInt((document.documentElement.clientHeight - oDiv.offsetHeight) / 2 + DivScroll));    //调用传参,其中里面的参数是DIV要走的终点。也就是(可视高度-DIV高度)/2+移动高度
    20         };
    21 
    22         var timer = null;
    23         function move(end) {
    24             clearInterval(timer);       //首先,先关闭之前如果有开启的setInterval;
    25             timer = setInterval(function () {       
    26                 var oDiv = document.getElementById('div1');
    27                 var speed = (end - oDiv.offsetTop) / 5;     //计算DIV要走的速度,DIV要走的速度就等于(终点-offsetTop高度)/缩放系数
    28                 speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);       //为了避免小数,将其取整
    29                 if (oDiv.offsetTop == end) {        //当DIV到达终点,则关闭setInterval;
    30                     clearInterval(timer);
    31                 }
    32                 else {
    33                     oDiv.style.top = oDiv.offsetTop + speed + 'px';     //移动div
    34                 }
    35             }, 30);
    36         }
    37     </script>
    38 </head>
    39 <body style="height: 1000px;">
    40     <div id="div1">
    41     </div>
    42 </body>
  • 相关阅读:
    Java中Runnable和Thread的区别
    git 代理设置
    Android的bitmap和优化
    String、StringBuffer与StringBuilder之间区别
    工作流的一些记录
    UIAutomation调用计算器模拟自动执行
    从客户端(Content="<EM ><STRONG ><U >这是测试这...")中检测到有潜在危险的Request.Form 值。
    泛型
    基础加强
    数据库和ado
  • 原文地址:https://www.cnblogs.com/xinchun/p/3449057.html
Copyright © 2011-2022 走看看