精华内容
下载资源
问答
  • js回到顶部

    2017-04-06 21:46:40
    js回到顶部
  • JS回到顶部

    2019-06-09 18:50:32
    JS回到顶部 今天我就来简单的说说用JS实现回到顶部 HTML: Hello ... 回到顶部 CSS: .container{ border: 1px solid black; } #top{ position: fixed; padding: 10px; ...

    JS回到顶部
    今天我就来简单的说说用JS实现回到顶部
    HTML:

    Hello

    ...
    回到顶部
    CSS: .container{ border: 1px solid black; } #top{ position: fixed; padding: 10px; width: 20px; border: 1px solid black; box-shadow: 0 0 2px #333; right: 20px; bottom: 20px; } #top:hover{ cursor: pointer; } JS代码: 首先创建一个变量 var scroll_Top = document.getElementById('top'); 用最常用的scrollTop属性实现 var timer = null; function scrollTop() { 取消一个通过调用requestAnimationFrame()方法添加到计划中的动画帧请求. cancelAnimationFrame(timer); requestAnimationFrame 会把每一帧中的所有DOM操作集中起来,在一次重绘或回流中就完成,并且重绘或回流的时间间隔紧紧跟随浏览器的刷新频率一般来说,这个频率为每秒60帧 timer = requestAnimationFrame(function sTop() { var top = document.body.scrollTop || document.documentElement.scrollTop; if (top > 0) { 使用定时器,将scrollTop的值每次减少20(自行设置),直到减少到0,则滚动完毕 document.body.scrollTop = document.documentElement.scrollTop = top - 20; timer = requestAnimationFrame(sTop); } else { cancelAnimationFrame(timer); } }); } scroll_Top.addEventListener('click', scrollTop, false);
    展开全文
  • JS 回到顶部

    2019-11-23 08:40:31
    对于内容超出屏幕很多的内容,有个回到顶部的按钮还是很人性化的 HTML <header></header> <div></div> 回到顶部</span> JS...

    对于内容超出屏幕很多的内容,有个回到顶部的按钮还是很人性化的

     

    HTML

    <header></header>
    <div></div>
    <span id="go">回到顶部</span>

     

    JS

    var goTop =document.getElementById("go");
        window.onscroll =function(){
            var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
            if(scrolltop >300){
                goTop.style.display ="block";
            }else{
                goTop.style.display ="none";
            }
        }
        goTop.onclick =function(){
            // 第一种方法:直接改变滚动条的位置
            document.documentElement.scrollTop = document.body.scrollTop =0;
    
            // 使用scrollTo方法,可把内容滚动到指定的坐标  谷歌不兼容
            // var win =document.documentElement || document.body;
            // win.scrollTo(0,0);
        }

     

    CSS自定义。

    知识点:

    * scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置。

    * scrollTo() 方法可把内容滚动到指定的坐标。

    谷歌浏览器不识别 document.documentElement.scrollTop 所以写document.documentElement.scrollTop = document.body.scrollTop都为0,这样在IE,谷歌,火狐都兼容了

    另外header标签在IE8是不能被识别的,解决办法是在head标签里面引入<script src="http://cdn.bootcss.com/html5shiv/r29/html5.min.js"></script>,这样就行了

     

    展开全文
  • js 回到顶部

    千次阅读 2012-02-17 09:24:54
    js回到顶部 function goTop(acceleration, time) { acceleration = acceleration || 0.1; time = time || 16; var x1 = 0; var y1 = 0; var x2 = 0; var y2 = 0; var x3 = 0; var y3 = 0; ...
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js回到顶部</title>
    <script type="text/javascript" language="javascript">
    function goTop(acceleration, time) {
    	acceleration = acceleration || 0.1;
    	time = time || 16;
     
    	var x1 = 0;
    	var y1 = 0;
    	var x2 = 0;
    	var y2 = 0;
    	var x3 = 0;
    	var y3 = 0;
     
    	if (document.documentElement) {
    		x1 = document.documentElement.scrollLeft || 0;
    		y1 = document.documentElement.scrollTop || 0;
    	}
    	if (document.body) {
    		x2 = document.body.scrollLeft || 0;
    		y2 = document.body.scrollTop || 0;
    	}
    	var x3 = window.scrollX || 0;
    	var y3 = window.scrollY || 0;
     
    	// 滚动条到页面顶部的水平距离
    	var x = Math.max(x1, Math.max(x2, x3));
    	// 滚动条到页面顶部的垂直距离
    	var y = Math.max(y1, Math.max(y2, y3));
     
    	// 滚动距离 = 目前距离 / 速度, 因为距离原来越小, 速度是大于 1 的数, 所以滚动距离会越来越小
    	var speed = 1 + acceleration;
    	window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
     
    	// 如果距离不为零, 继续调用迭代本函数
    	if(x > 0 || y > 0) {
    		var invokeFunction = "goTop(" + acceleration + ", " + time + ")";
    		window.setTimeout(invokeFunction, time);
    	}
    }
    
    
    
    </script>
    </head>
    
    <body>
    <br />Top
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <a href="#" οnclick="goTop();return false;">TOP</a>
    </body>
    </html>
    

    展开全文
  • 详解原生JS回到顶部

    2020-10-17 06:29:41
    主要介绍了JS回到顶部,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
  • js回到顶部动画

    2019-10-03 08:24:37
    现在很多网页都是宽屏,并且只有一些,很长很长,这时候一个“回到顶部”的按钮是显得很重要了。写了一个小demo,很轻松的搞定这个功能。  -----------------------------------------------正文开始-----------...

      现在很多网页都是宽屏,并且只有一些,很长很长,这时候一个“回到顶部”的按钮是显得很重要了。写了一个小demo,很轻松的搞定这个功能。

     

      -----------------------------------------------正文开始-----------------------------------------------------------

      先完成html把基本信息写出来

      给很多<div></div>占据很长很长的页面,最后给一个<span id="goTop"></span>作为“回到顶部”的按钮。

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>js回到顶部</title>
     6 </head>
     7 <body>
     8     <div>I want to go back to the top.</div><br/>
     9     <div>I want to go back to the top.</div><br/>
    10     <div>I want to go back to the top.</div><br/>
    11     <div>I want to go back to the top.</div><br/>
    12     <div>I want to go back to the top.</div><br/>
    13     <div>I want to go back to the top.</div><br/>
    14     <div>I want to go back to the top.</div><br/>
    15     <div>I want to go back to the top.</div><br/>
    16     <div>I want to go back to the top.</div><br/>
    17     <div>I want to go back to the top.</div><br/>
    18     <div>I want to go back to the top.</div><br/>
    19     <div>I want to go back to the top.</div><br/>
    20     <div>I want to go back to the top.</div><br/>
    21     <div>I want to go back to the top.</div><br/>
    22     <div>I want to go back to the top.</div><br/>
    23     <div>I want to go back to the top.</div><br/>
    24     <div>I want to go back to the top.</div><br/>
    25     <div>I want to go back to the top.</div><br/>
    26     <div>I want to go back to the top.</div><br/>
    27     <div>I want to go back to the top.</div><br/>
    28     <div>I want to go back to the top.</div><br/>
    29     <div>I want to go back to the top.</div><br/>
    30     <div>I want to go back to the top.</div><br/>
    31     <div>I want to go back to the top.</div><br/>
    32     <div>I want to go back to the top.</div><br/>
    33     <div>I want to go back to the top.</div><br/>
    34     <div>I want to go back to the top.</div><br/>
    35     <div>I want to go back to the top.</div><br/>
    36     <div>I want to go back to the top.</div><br/>
    37     <div>I want to go back to the top.</div><br/>
    38     <div>I want to go back to the top.</div><br/>
    39     <div>I want to go back to the top.</div><br/>
    40     <div>I want to go back to the top.</div><br/>
    41     <div>I want to go back to the top.</div><br/>
    42     <div>I want to go back to the top.</div><br/>
    43     <div>I want to go back to the top.</div><br/>
    44     <div>I want to go back to the top.</div><br/>
    45     <div>I want to go back to the top.</div><br/>
    46     <div>I want to go back to the top.</div><br/>
    47     <div>I want to go back to the top.</div><br/>
    48     <div>I want to go back to the top.</div><br/>
    49     <div>I want to go back to the top.</div><br/>
    50     <div>I want to go back to the top.</div><br/>
    51     <div>I want to go back to the top.</div><br/>
    52     <div>I want to go back to the top.</div><br/>
    53     <div>I want to go back to the top.</div><br/>
    54     <span id="goTop"></span>
    55 </body>
    56 </html>

       

      接下来给按钮一个css样式吧。

      

     1     <style>
     2         #goTop{
     3             display:none;
     4             position:fixed;
     5             bottom:20px;
     6             background:url(http://cdn.w3cfuns.com/resource/images/scrolltop.png) no-repeat 0 0;
     7             width:34px;
     8             height:34px;
     9             cursor:pointer;
    10             right:20px;
    11         }
    12     </style>

     

      看,以下这个图图就是准备回到顶部的按钮。

           

      css中  display:none;已经把这个隐藏了,但别担心,在js中我们实现了 : 当鼠标滚轮下滑时它才会出现。

     

      ------------------------------------------现在开始JS文件啦--------------------------------------------

      现在先框一个JS的框框。

    1 <script>
    2    window.onload=function(){   };
    3 </script>

      再贴上js里的所有代码:

     1     <script>
     2         window.onload=function(){
     3             var timer=null;  
     4             var goTop=document.getElementById("goTop"); //获取“回到顶部”的按钮
     5             goTop.onclick=function(){ //点击按钮之后触发的函数
     6                 timer=setInterval(function(){
     7                     var top=document.body.scrollTop+document.documentElement.scrollTop;//获取当前位置离顶部的距离
     8                     var speed=top/5;//设置回到顶部的速度
     9                     if(document.body.scrollTop){ 
    10                         document.body.scrollTop-=speed;
    11                     }
    12                     else{
    13                         document.documentElement.scrollTop-=speed;
    14                     }
    15                     if(top==0){
    16                         clearInterval(timer);
    17                     }
    18                 },30);
    19             };
    20             window.onscroll=function(){ //当距离大于500时才让按钮出现,否则就不显示
    21                 if(document.body.scrollTop+document.documentElement.scrollTop>=500){
    22                     goTop.style.display="block";
    23                 }
    24                 else{
    25                     goTop.style.display="none";
    26                 }
    27             };
    28         window.onmousewheel=function(e){//当滚轮是向下滚时,就将向上滚的函数停止执行
    29             if(e.wheelDelta<0) clearInterval(timer);
    30         };
    31         };
    32     </script>

     

      完毕了。可以实现了。以下贴上整个页面的代码。

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>js回到顶部</title>
     6         <script>
     7         window.onload=function(){
     8             var timer=null;  
     9             var goTop=document.getElementById("goTop"); //获取“回到顶部”的按钮
    10             goTop.onclick=function(){ //点击按钮之后触发的函数
    11                 timer=setInterval(function(){
    12                     var top=document.body.scrollTop+document.documentElement.scrollTop;//获取当前位置离顶部的距离
    13                     var speed=top/5;//设置回到顶部的速度
    14                     if(document.body.scrollTop){ 
    15                         document.body.scrollTop-=speed;
    16                     }
    17                     else{
    18                         document.documentElement.scrollTop-=speed;
    19                     }
    20                     if(top==0){
    21                         clearInterval(timer);
    22                     }
    23                 },30);
    24             };
    25             window.onscroll=function(){ //当距离大于500时才让按钮出现,否则就不显示
    26                 if(document.body.scrollTop+document.documentElement.scrollTop>=500){
    27                     goTop.style.display="block";
    28                 }
    29                 else{
    30                     goTop.style.display="none";
    31                 }
    32             };
    33         window.onmousewheel=function(e){//当滚轮是向下滚时,就将向上滚的函数停止执行
    34             if(e.wheelDelta<0) clearInterval(timer);
    35         };
    36         };
    37     </script>
    38 </head>
    39 <body>
    40     <div>I want to go back to the top.</div><br/>
    41     <div>I want to go back to the top.</div><br/>
    42     <div>I want to go back to the top.</div><br/>
    43     <div>I want to go back to the top.</div><br/>
    44     <div>I want to go back to the top.</div><br/>
    45     <div>I want to go back to the top.</div><br/>
    46     <div>I want to go back to the top.</div><br/>
    47     <div>I want to go back to the top.</div><br/>
    48     <div>I want to go back to the top.</div><br/>
    49     <div>I want to go back to the top.</div><br/>
    50     <div>I want to go back to the top.</div><br/>
    51     <div>I want to go back to the top.</div><br/>
    52     <div>I want to go back to the top.</div><br/>
    53     <div>I want to go back to the top.</div><br/>
    54     <div>I want to go back to the top.</div><br/>
    55     <div>I want to go back to the top.</div><br/>
    56     <div>I want to go back to the top.</div><br/>
    57     <div>I want to go back to the top.</div><br/>
    58     <div>I want to go back to the top.</div><br/>
    59     <div>I want to go back to the top.</div><br/>
    60     <div>I want to go back to the top.</div><br/>
    61     <div>I want to go back to the top.</div><br/>
    62     <div>I want to go back to the top.</div><br/>
    63     <div>I want to go back to the top.</div><br/>
    64     <div>I want to go back to the top.</div><br/>
    65     <div>I want to go back to the top.</div><br/>
    66     <div>I want to go back to the top.</div><br/>
    67     <div>I want to go back to the top.</div><br/>
    68     <div>I want to go back to the top.</div><br/>
    69     <div>I want to go back to the top.</div><br/>
    70     <div>I want to go back to the top.</div><br/>
    71     <div>I want to go back to the top.</div><br/>
    72     <div>I want to go back to the top.</div><br/>
    73     <div>I want to go back to the top.</div><br/>
    74     <div>I want to go back to the top.</div><br/>
    75     <div>I want to go back to the top.</div><br/>
    76     <div>I want to go back to the top.</div><br/>
    77     <div>I want to go back to the top.</div><br/>
    78     <div>I want to go back to the top.</div><br/>
    79     <div>I want to go back to the top.</div><br/>
    80     <div>I want to go back to the top.</div><br/>
    81     <div>I want to go back to the top.</div><br/>
    82     <div>I want to go back to the top.</div><br/>
    83     <div>I want to go back to the top.</div><br/>
    84     <div>I want to go back to the top.</div><br/>
    85     <div>I want to go back to the top.</div><br/>
    86     <span id="goTop"></span>
    87 </body>
    88 </html>

     

    转载于:https://www.cnblogs.com/abigale/p/4855327.html

    展开全文
  • 原生JS回到顶部

    2019-04-04 08:58:37
    HTML代码: <div class="container"> <p>你好哇</p> ... ...回到顶部</div> CSS代码: .container{ border: 1px solid black; } #top{ position: fixed; padding: ...
  • js回到顶部代码

    2020-12-09 14:05:30
    // if(osTop >= clientHeight){ //如果滚动高度大于可视区域高度,则显示回到顶部按钮 if (osTop >= 100) { //如果滚动高度大于可视区域高度,则显示回到顶部按钮 obtn.style.display = 'block'; } else { //否则...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,051
精华内容 420
关键字:

js回到顶部