大家好,又见面了,我是你们的朋友全栈君。
1.锚点方式:
1 <body style="height:2000px;"> 2 <div id="topAnchor"></div> 3 <a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到顶部</a> 4 </body>
2.scrollTop:scrollTop属性表示被隐藏在内容区域上方的像素数。元素未滚动时,scrollTop的值为0,如果元素被垂直滚动了,scrollTop的值大于0,且表示元素上方不可见内容的像素宽度
1 <body style="height:2000px;"> 2 <button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button> 3 <script> 4 test.onclick = function(){ 5 document.body.scrollTop = document.documentElement.scrollTop = 0; 6 } 7 </script> 8 </body>
3.scrollTo:scrollTo(x,y)方法滚动当前window中显示的文档,让文档中由坐标x和y指定的点位于显示区域的左上角,设置scrollTo(0,0)可以实现回到顶部的效果
1 <body style="height:2000px;"> 2 <button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button> 3 <script> 4 test.onclick = function(){ 5 scrollTo(0,0); 6 } 7 </script> 8 </body>
4.scrollBy():scrollBy(x,y)方法滚动当前window中显示的文档,x和y指定滚动的相对量,只要把当前页面的滚动长度作为参数,逆向滚动,则可以实现回到顶部的效果
1 <body style="height:2000px;"> 2 <button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button> 3 <script> 4 test.onclick = function(){ 5 var top = document.body.scrollTop || document.documentElement.scrollTop 6 scrollBy(0,-top); 7 } 8 </script> 9 </body>
5.scrolltoView()Element.scrollIntoView方法滚动当前元素,进入浏览器的可见区域,该方法可以接受一个布尔值作为参数。如果为true,表示元素的顶部与当前区域的可见部分的顶部对齐(前提是当前区域可滚动);如果为false,表示元素的底部与当前区域的可见部分的尾部对齐(前提是当前区域可滚动)。如果没有提供该参数,默认为true,使用该方法的原理与使用锚点的原理类似,在页面最上方设置目标元素,当页面滚动时,目标元素被滚动到页面区域以外,点击回到顶部按钮,使目标元素重新回到原来位置,则达到预期效果
1 <body style="height:2000px;"> 2 <div id="target"></div> 3 <button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button> 4 <script> 5 test.onclick = function(){ 6 target.scrollIntoView(); 7 } 8 </script> 9 </body>
- <style>
- .box{
- position:fixed;
- right:10px;
- bottom: 10px;
- height:30px;
- width: 50px;
- text–align:center;
- padding–top:20px;
- background–color: lightblue;
- border–radius: 20%;
- overflow: hidden;
- }
- .box:hover:before{
- top:50%
- }
- .box:hover .box–in{
- visibility: hidden;
- }
- .box:before{
- position: absolute;
- top: –50%;
- left: 50%;
- transform: translate(-50%,-50%);
- content:‘回到顶部’;
- width: 40px;
- color:peru;
- font–weight:bold;
- }
- .box–in{
- visibility: visible;
- display:inline–block;
- height:20px;
- width: 20px;
- border: 3px solid black;
- border–color: white transparent transparent white;
- transform:rotate(45deg);
- }
- </style>
- <body style=“height:2000px;“>
- <div id=“box” class=“box”>
- <div class=“box-in”></div>
- </div>
- </body>
- <script>
- var timer = null;
- box.onclick = function(){
- cancelAnimationFrame(timer);
- timer = requestAnimationFrame(function fn(){
- var oTop = document.body.scrollTop || document.documentElement.scrollTop;
- if(oTop > 0){
- document.body.scrollTop = document.documentElement.scrollTop = oTop – 50;
- timer = requestAnimationFrame(fn);
- }else{
- cancelAnimationFrame(timer);
- }
- });
- }
- </script>
- <script>
- var timer = null;
- box.onclick = function(){
- cancelAnimationFrame(timer);
- timer = requestAnimationFrame(function fn(){
- var oTop = document.body.scrollTop || document.documentElement.scrollTop;
- if(oTop > 0){
- scrollTo(0,oTop–50);
- timer = requestAnimationFrame(fn);
- }else{
- cancelAnimationFrame(timer);
- }
- });
- }
- </script>
- <script>
- var timer = null;
- box.onclick = function(){
- cancelAnimationFrame(timer);
- timer = requestAnimationFrame(function fn(){
- var oTop = document.body.scrollTop || document.documentElement.scrollTop;
- if(oTop > 0){
- scrollBy(0,-50);
- timer = requestAnimationFrame(fn);
- }else{
- cancelAnimationFrame(timer);
- }
- });
- }
- </script>
- <style>
- .box{
- position:fixed;
- right:10px;
- bottom: 10px;
- height:30px;
- width: 50px;
- text–align:center;
- padding–top:20px;
- background–color: lightblue;
- border–radius: 20%;
- overflow: hidden;
- }
- .box:hover:before{
- top:50%
- }
- .box:hover .box–in{
- visibility: hidden;
- }
- .box:before{
- position: absolute;
- top: –50%;
- left: 50%;
- transform: translate(-50%,-50%);
- content:‘回到顶部’;
- width: 40px;
- color:peru;
- font–weight:bold;
- }
- .box–in{
- visibility: visible;
- display:inline–block;
- height:20px;
- width: 20px;
- border: 3px solid black;
- border–color: white transparent transparent white;
- transform:rotate(45deg);
- }
- </style>
- <body style=“height:2000px;“>
- <div id=“box” class=“box”>
- <div class=“box-in”></div>
- </div>
- </body>
- <script>
- var timer = null;
- box.onclick = function(){
- cancelAnimationFrame(timer);
- timer = requestAnimationFrame(function fn(){
- var oTop = document.body.scrollTop || document.documentElement.scrollTop;
- if(oTop > 0){
- document.body.scrollTop = document.documentElement.scrollTop = oTop – 50;
- timer = requestAnimationFrame(fn);
- }else{
- cancelAnimationFrame(timer);
- }
- });
- }
- </script>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155722.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...