JS实现回到页面顶部的五种写法(从实现到增强)

您所在的位置:网站首页 vim怎么返回顶部 JS实现回到页面顶部的五种写法(从实现到增强)

JS实现回到页面顶部的五种写法(从实现到增强)

2024-01-10 02:49| 来源: 网络整理| 查看: 265

【1】锚点

  使用锚点链接是一种简单的返回顶部的功能实现。该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置

回到顶部

【2】scrollTop

  scrollTop属性表示被隐藏在内容区域上方的像素数。元素未滚动时,scrollTop的值为0,如果元素被垂直滚动了,scrollTop的值大于0,且表示元素上方不可见内容的像素宽度

  由于scrollTop是可写的,可以利用scrollTop来实现回到顶部的功能

回到顶部 test.onclick = function(){ document.body.scrollTop = document.documentElement.scrollTop = 0; }

【3】scrollTo()

  scrollTo(x,y)方法滚动当前window中显示的文档,让文档中由坐标x和y指定的点位于显示区域的左上角

  设置scrollTo(0,0)可以实现回到顶部的效果

回到顶部 test.onclick = function(){ scrollTo(0,0); }

【4】scrollBy()

  scrollBy(x,y)方法滚动当前window中显示的文档,x和y指定滚动的相对量

  只要把当前页面的滚动长度作为参数,逆向滚动,则可以实现回到顶部的效果

回到顶部 test.onclick = function(){ var top = document.body.scrollTop || document.documentElement.scrollTop scrollBy(0,-top); }

【5】scrollIntoView()

  Element.scrollIntoView方法滚动当前元素,进入浏览器的可见区域 

  该方法可以接受一个布尔值作为参数。如果为true,表示元素的顶部与当前区域的可见部分的顶部对齐(前提是当前区域可滚动);如果为false,表示元素的底部与当前区域的可见部分的尾部对齐(前提是当前区域可滚动)。如果没有提供该参数,默认为true

  使用该方法的原理与使用锚点的原理类似,在页面最上方设置目标元素,当页面滚动时,目标元素被滚动到页面区域以外,点击回到顶部按钮,使目标元素重新回到原来位置,则达到预期效果

回到顶部 test.onclick = function(){ target.scrollIntoView(); } scrollIntoView()实现平滑移动锚点效果:

  scrollIntoView()有三个参数:

behavior 表示滚动方式。auto 表示使用当前元素的 scroll-behavior 样式。instant 和 smooth表示 直接滚到底 和 使用平滑滚动。 block 表示块级元素排列方向要滚动到的位置。对于默认的 writing-mode: horizontal-tb来说,就是竖直方向。start 表示将视口的顶部和元素顶部对齐;center 表示将视口的中间和元素的中间对齐;end表示将视口的底部和元素底部对齐;nearest 表示就近对齐。 inline 表示行内元素排列方向要滚动到的位置。对于默认的 writing-mode: horizontal-tb来说,就是水平方向。其值与 block 类似 { behavior: "auto" | "instant" | "smooth", // 默认 auto block: "start" | "center" | "end" | "nearest", // 默认 center inline: "start" | "center" | "end" | "nearest", // 默认 nearest }

 

增强

  下面对回到顶部的功能进行增强

【1】显示增强

  使用CSS画图,将“回到顶部”变成可视化的图形(如果兼容IE8-浏览器,则用图片代替)

  使用CSS伪元素及伪类hover效果,当鼠标移动到该元素上时,显示回到顶部的文字,移出时不显示

.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); }

【2】动画增强

  为回到顶部增加动画效果,滚动条以一定的速度回滚到顶部

  动画有两种:一种是CSS动画,需要有样式变化配合transition;一种是javascript动画,使用定时器来实现  

  在上面的5种实现中,scrollTop、scrollTo()和scrollBy()方法可以增加动画,且由于无样式变化,只能增加javascript动画

  定时器又有setInterval、setTimeout和requestAnimationFrame这三种可以使用,下面使用性能最好的定时器requestAnimationFrame来实现

  [注意]IE9-浏览器不支持该方法,可以使用setTimeout来兼容

  1、增加scrollTop的动画效果

  使用定时器,将scrollTop的值每次减少50,直到减少到0,则动画完毕

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); } }); }

  2、增加scrollTo()动画效果

  将scrollTo(x,y)中的y参数通过scrollTop值获取,每次减少50,直到减少到0,则动画完毕

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); } }); }

3、增加scrollBy()动画效果

  将scrollBy(x,y)中的y参数设置为-50,直到scrollTop为0,则回滚停止

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); } }); }

实现

  由于scrollTop、scrollBy()和scrollTo()方法,都以scrollTop值是否减少为0作为动画停止的参照,且三个动画的原理和实现都基本相似,性能也相似。最终,以最常用的scrollTop属性实现动画增强效果

  当然,如果觉得50的速度不合适,可以根据实际情况进行调整

.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); } 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); } }); }

 

 

总结自:

1、https://blog.csdn.net/qq_39864544/article/details/89919519

2、https://www.jb51.net/article/91824.htm



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3