zoukankan      html  css  js  c++  java
  • Javascript笔记----实现Page页面右下角置顶按钮.

      从用博客开始,发现博客园中很多博友的博客中在Page右下角都有个图标,不论屏幕怎么拉伸,都始终停留在右下角。点击后页面置顶。后面想想写一个Demo来实现这种效果吧。

         一. 图标右下角固定.

        1.SS 里面提供了4中布局方式. 其中fixed表示绝对定位元素。所以我们选择使用fixed来实现图标固定.

     

    absolute

    生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

    元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

    fixed

    生成绝对定位的元素,相对于浏览器窗口进行定位。

    元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

    relative

    生成相对定位的元素,相对于其正常位置进行定位。

    因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

    static 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。
    inherit 规定应该从父元素继承 position 属性的值。

            2.定代码如下。Button按钮将始终置于屏幕右下角。不论是拖动上下精度条还是拉伸浏览器窗口大小.

    #FourLeafCloverZCTopBtn{
            bottom: 5px;
            right: 5px;
            position:fixed;
        }

          二. 实现点击后回到页面最上角.
              1. 要想回到屏幕最上角就得小了解如何通过JavaScript还操作拖动条的上下移动.JavaScript提供了scrollby和scroll方法.

    window.scrollBy(0,-30)  //屏幕上移30像素点
    window.scroll(0,0) // 屏幕回到最上角

              2. 上面已经提到了如何移动拖动条,那么如何实现按照一定的速度移动到page页顶部呢。那么就要借助setInterval和clearInterval方法. 实现没10毫秒屏幕上移30个像素点。 

    <button id="FourLeafCloverZCTopBtn" onclick="FourLeafCloverZCTopFunc()">TOP</button>

     

    <script type="text/javascript">
         var FourLeafCloverZCVar;
    
        function FourLeafCloverZCTopFunc(){
            FourLeafCloverZCVar=setInterval(FourLeafCloverZCEachScrollBy,10);    
        }
    
        function FourLeafCloverZCEachScrollBy(eachHeight){
            if(document.documentElement && document.documentElement.scrollTop)  //IE
            {
                if(document.documentElement.scrollTop<=0){
                    clearInterval(FourLeafCloverZCVar);
                }else{
                    window.scrollBy(0,-30);
                }
            }else{          //Chrome不支持documentElement.scrollTop
                if(document.body.scrollTop<=0){
                    clearInterval(FourLeafCloverZCVar);
                }else{
                    window.scrollBy(0,-30);
                }
            }
        }
     </script>

            三. 扩展
                 实现了置顶按钮。那么我们如何实现点击按钮屏幕置底呢.其实原理差不多,这里就不写demo了。给大家提供一些属性做参考.

          网页可见区域宽:document.body.clientWidth
      网页可见区域高:document.body.clientHeight
      网页可见区域宽:document.body.offsetWidth (包括边线的宽)
      网页可见区域高:document.body.offsetHeight (包括边线的宽)
      网页正文全文宽:document.body.scrollWidth
      网页正文全文高:document.body.scrollHeight
      网页被卷去的高:document.body.scrollTop
      网页被卷去的左:document.body.scrollLeft
      网页正文部分上:window.screenTop
      网页正文部分左:window.screenLeft
      屏幕分辨率的高:window.screen.height
      屏幕分辨率的宽:window.screen.width
      屏幕可用工作区高度:window.screen.availHeight
      屏幕可用工作区宽度:window.screen.availWidth

            四. Demo  http://files.cnblogs.com/files/FourLeafCloverZc/%E5%AF%BC%E8%88%AA.zip 

         

        

  • 相关阅读:
    LeetCode——Generate Parentheses
    LeetCode——Best Time to Buy and Sell Stock IV
    LeetCode——Best Time to Buy and Sell Stock III
    LeetCode——Best Time to Buy and Sell Stock
    LeetCode——Find Minimum in Rotated Sorted Array
    Mahout实现基于用户的协同过滤算法
    使用Java对文件进行解压缩
    LeetCode——Convert Sorted Array to Binary Search Tree
    LeetCode——Missing Number
    LeetCode——Integer to Roman
  • 原文地址:https://www.cnblogs.com/FourLeafCloverZc/p/4296412.html
Copyright © 2011-2022 走看看