-
主要就是使用 position: fixed; 固定到指定位置。
-
demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>回到顶部</title> </head> <style> #fixed{ position: fixed; top: 80%; right: 10%; color: blue; font-size: 50px; font-weight: 700; } .lb{ line-height: 12.5rem; font-size: 100px; } </style> <body> <button id="fixed" onclick="toTop()">回到顶部</button> <p class="lb">列表</p> <p class="lb">列表</p> <p class="lb">列表</p> <p class="lb">列表</p> <p class="lb">列表</p> <p class="lb">列表</p> <p class="lb">列表</p> <p class="lb">列表</p> <p class="lb">列表</p> </body> <script> function toTop(){ scrollTo(0,0); } </script> </html>
-
效果