當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JS回到顶部
第一種:錨點
使用錨點鏈接是一種簡單的返回頂部的功能實現。該實現主要在頁面頂部放置一個指定名稱的錨點鏈接,然后在頁面下方放置一個返回到該錨點的鏈接,用戶點擊該鏈接即可返回到該錨點所在的頂部位置
<body style="height:2000px;"><div id="topAnchor"></div><a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到頂部</a> </body>?
第二種:scrollTop
scrollTop屬性表示被隱藏在內容區域上方的像素數。元素未滾動時,scrollTop的值為0,如果元素被垂直滾動了,scrollTop的值大于0,且表示元素上方不可見內容的像素寬度
<body style="height:2000px;"><button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button><script>test.onclick = function(){document.body.scrollTop = document.documentElement.scrollTop = 0;}</script> </body>?
第三種:scrollTo()
scrollTo(x,y)方法滾動當前window中顯示的文檔,讓文檔中由坐標x和y指定的點位于顯示區域的左上角,設置scrollTo(0,0)可以實現回到頂部的效果
<body style="height:2000px;"><button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button><script>test.onclick = function(){scrollTo(0,0);}</script> </body>?
轉載于:https://www.cnblogs.com/wangyongx/p/9915608.html
總結
- 上一篇: 织梦电脑站手机站伪静态+全套伪静态规则-
- 下一篇: JavaScript高级篇之Functi