js原生实现楼梯滚动效果

在开发过程中可能会遇到这种场景 有点类似经京东楼梯的效果那样,点击按钮然后跳转到对应区域,还记得当初学习js的时候遇到这种东西,完全不知道怎么做?用定位吗显然不是,后来知道 仅仅三行到四行代码就解决了,

一般来说有两种方法 一个是用插件,一个是原生实现,如果使用vue开发,会用到 better-scrooll插件

github地址:https://github.com/ustbhuangyi/better-scroll  

参考链接:http://www.cnblogs.com/chenlw/p/9720208.html |||https://blog.csdn.net/guochao8/article/details/85225080

第二种方法是用原生实现

let current = document.getElementsByClassName("current").offsetTop-50;//偏移量是50
                /**
                 *  document.body.scrollTop:网页被卷去的高(也就是滚动到哪里的位置)
                 *  window.scrollY:返回文档当前垂直滚动的像素数
                 *  document.documentElement.scrollTop:获取当前页面的滚动条纵坐标位置
                 * @type {number}
                 */
                document.body.scrollTop = window.scrollY=document.documentElement.scrollTop=current;

猜你喜欢

转载自blog.csdn.net/aoshiyunlong/article/details/89554893