zoukankan      html  css  js  c++  java
  • 返回顶部

    <!DOCTYPE html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>Javascript 返回顶部</title>
    <style type="text/css">
    #btn {40px; height:40px; position:fixed; right:65px;display: none; bottom:10px; background:url(img/top_bg.png) no-repeat left top;}
    #btn:hover {background:url(img/top_bg.png) no-repeat 0 -39px;}
    .bg {1190px; margin:0 auto;}
    </style>

    </head>
    <body>
    <a href="javascript:;" id="btn" title="回到顶部"></a>
    <div class="bg">
    <img src="img/tb_bg.jpg" alt="" />
    </div>
    </body>
    <script type="text/javascript">
    window.onload=function(){

    var obtn=document.getElementById("btn");
    var timer=null;
    var isTop=true;
    var clientHeight=document.documentElement.clientHeight || document.body.clientHeight;//可视区域高度
    //窗口滚动事件
    window.onscroll=function(){
    var osTop=document.documentElement.scrollTop || document.body.scrollTop;//滚动条距离顶部的距离
    if(osTop>=clientHeight){
    obtn.style.display="block"; //返回顶部按钮的显示和隐藏
    }
    else{
    obtn.style.display="none";
    }
    if(!isTop){
    clearInterval(timer); //滚动停止事件
    }
    isTop=false;

    }
    obtn.onclick=function(){
    //设置定时器
    timer=setInterval(function(){
    //获取滚动条距离顶部的高度
    var osTop=document.documentElement.scrollTop || document.body.scrollTop;
    var ispeed=Math.floor(-osTop/5);
    document.documentElement.scrollTop=document.body.scrollTop=osTop+ispeed; //滚动条加速效果
    isTop=true;
    if(osTop==0){
    clearInterval(timer);
    }
    },30);
    }
    }
    </script>
    </html>

  • 相关阅读:
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列(important!)
    从上往下打印二叉树
    最小的k个数(important!)
    扑克牌顺子
    栈的压入、弹出序列(important!)
    和为s的连续正数序列(important!)
    数组中只出现一次的数字
    fgets()函数以及fputs()函数
    C语言中的指针
  • 原文地址:https://www.cnblogs.com/luoguixin/p/6385865.html
Copyright © 2011-2022 走看看