漂亮的侧边返回顶部和底部代码

2026年7 月4日 / 网站源码 / 2条 / 744次

龙哥最近一直用AI做网站或者修改网站,经常让AI添加侧边返回顶部和底部的按钮,今天把鸡巴付(jibafu.com)改版了一下,感觉这个按钮的代码不错的,于是要AI专门整理出了完整的代码,所以放在博客收藏下,以后就直接拿来使用,免得浪费tokens。

1. HTML 结构(放在 前任意位置)

  1. <!-- 滚动按钮容器 -->
  2. <div class="scroll-buttons">
  3.     <button class="scroll-btn scroll-top" id="scrollTopBtn" title="返回顶部">
  4.         <!-- 使用 Unicode 箭头字符,不依赖 Font Awesome -->
  5.         ↑
  6.         <!-- 如果您使用了 Font Awesome,可将上面一行替换为:<i class="fas fa-arrow-up"></i> -->
  7.     </button>
  8.     <button class="scroll-btn scroll-bottom" id="scrollBottomBtn" title="返回底部">
  9.         ↓
  10.         <!-- 同理,可替换为:<i class="fas fa-arrow-down"></i> -->
  11.     </button>
  12. </div>

提示:如果您的项目未引入 Font Awesome,请将 替换为 ↑ 或使用 SVG 图标。

2. CSS 样式(放入您的样式表)

  1. /* 滚动按钮容器 - 固定于右侧 */
  2. .scroll-buttons {
  3.     positionfixed;
  4.     rightright24px;
  5.     bottombottom100px;          /* 距离底部位置,可调整 */
  6.     display: flex;
  7.     flex-direction: column;
  8.     gap: 12px;
  9.     z-index: 999;
  10. }
  11. /* 单个按钮样式 */
  12. .scroll-btn {
  13.     width48px;
  14.     height48px;
  15.     border-radius: 50%;
  16.     bordernone;
  17.     background#ffffff;
  18.     box-shadow: 0 4px 16px rgba(0, 0, 0, 0.10);
  19.     color#4a5568;
  20.     font-size20px;
  21.     cursorpointer;
  22.     display: flex;
  23.     align-items: center;
  24.     justify-contentcenter;
  25.     transition: opacity 0.4s ease, transform 0.4s ease, background 0.25s, box-shadow 0.25s;
  26.     backdrop-filter: blur(4px);
  27.     border1px solid rgba(255, 255, 255, 0.6);
  28.     /* 默认隐藏(不可见、不可点击) */
  29.     opacity: 0;
  30.     transform: translateY(10px);
  31.     pointer-events: none;
  32. }
  33. /* 显示状态 - 通过 .show 类控制 */
  34. .scroll-btn.show {
  35.     opacity: 1;
  36.     transform: translateY(0);
  37.     pointer-events: auto;
  38. }
  39. /* 悬停效果(可自定义颜色) */
  40. .scroll-btn:hover {
  41.     background#ff6b35;
  42.     color#fff;
  43.     transform: scale(1.05);
  44.     box-shadow: 0 6px 20px rgba(255, 107, 53, 0.25);
  45. }
  46. /* 移动端适配(可选) */
  47. @media (max-width480px) {
  48.     .scroll-buttons {
  49.         rightright16px;
  50.         bottombottom80px;
  51.         gap: 10px;
  52.     }
  53.     .scroll-btn {
  54.         width42px;
  55.         height42px;
  56.         font-size16px;
  57.     }
  58. }

3. JavaScript 逻辑(放入脚本文件或内联)

  1. (function() {
  2.     'use strict';
  3.     var topBtn = document.getElementById('scrollTopBtn');
  4.     var bottomBtn = document.getElementById('scrollBottomBtn');
  5.     // 如果元素不存在则退出
  6.     if (!topBtn || !bottomBtn) return;
  7.     // 更新按钮显示状态
  8.     function updateButtons() {
  9.         var scrollY = window.scrollY || window.pageYOffset || 0;
  10.         var docHeight = document.documentElement.scrollHeight;
  11.         var winHeight = window.innerHeight;
  12.         // 距离底部 80px 以内视为“到达底部”
  13.         var bottomReached = (winHeight + scrollY) >= (docHeight - 80);
  14.         // 顶部按钮:滚动超过 200px 时显示
  15.         if (scrollY > 200) {
  16.             topBtn.classList.add('show');
  17.         } else {
  18.             topBtn.classList.remove('show');
  19.         }
  20.         // 底部按钮:未到达底部时显示
  21.         if (bottomReached) {
  22.             bottomBtn.classList.remove('show');
  23.         } else {
  24.             bottomBtn.classList.add('show');
  25.         }
  26.     }
  27.     // 滚动节流(避免频繁触发)
  28.     var timer = null;
  29.     function throttledUpdate() {
  30.         if (timer) return;
  31.         timer = setTimeout(function() {
  32.             updateButtons();
  33.             timer = null;
  34.         }, 50);
  35.     }
  36.     // 监听滚动和窗口大小变化
  37.     window.addEventListener('scroll', throttledUpdate, { passive: true });
  38.     window.addEventListener('resize', updateButtons);
  39.     // 点击事件:平滑滚动
  40.     topBtn.addEventListener('click', function() {
  41.         window.scrollTo({ top: 0, behavior: 'smooth' });
  42.     });
  43.     bottomBtn.addEventListener('click', function() {
  44.         window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
  45.     });
  46.     // 页面加载完成后初始化一次
  47.     if (document.readyState === 'complete') {
  48.         updateButtons();
  49.     } else {
  50.         window.addEventListener('load', updateButtons);
  51.     }
  52. })();

使用说明

复制 HTML 到您页面的任意位置(建议放在 < body> 结束前)。

复制 CSS 到您的样式文件中,或放在 < style> 标签内。

复制 JavaScript 到您的脚本文件中,或放在 < script> 标签内(确保在 DOM 加载后执行,或放在页面底部)。

不依赖任何外部库:按钮使用 Unicode 箭头(↑ / ↓),若您已引入 Font Awesome,可自行替换为图标标签。

引入Font Awesome图标添加css库:

  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" />  

可调参数:

滚动阈值(scrollY > 200)——可改为其他数值。

底部判定距离(docHeight - 80)——可调整 80 这个值。

这样您就获得了一个轻量、独立、即插即用的滚动辅助组件。

※实现效果完整代码整合

如果懒得分开,直接将以下全部代码放在< /body>前面即可:

  1. <!-- 滚动按钮容器 -->  
  2. <div class="scroll-buttons">  
  3.     <button class="scroll-btn scroll-top" id="scrollTopBtn" title="返回顶部">  
  4.         <!-- 使用 Unicode 箭头字符,不依赖 Font Awesome -->  
  5.         ↑  
  6.         <!-- 如果您使用了 Font Awesome,可将上面一行替换为:<i class="fas fa-arrow-up"></i> -->  
  7.     </button>  
  8.     <button class="scroll-btn scroll-bottom" id="scrollBottomBtn" title="返回底部">  
  9.         ↓  
  10.         <!-- 同理,可替换为:<i class="fas fa-arrow-down"></i> -->  
  11.     </button>  
  12. </div>  
  13. <style>  
  14. /* 滚动按钮容器 - 固定于右侧 */  
  15. .scroll-buttons {  
  16.     position: fixed;  
  17.     right: 24px;  
  18.     bottom: 100px;          /* 距离底部位置,可调整 */  
  19.     display: flex;  
  20.     flex-direction: column;  
  21.     gap: 12px;  
  22.     z-index: 999;  
  23. }  
  24.   
  25. /* 单个按钮样式 */  
  26. .scroll-btn {  
  27.     width: 48px;  
  28.     height: 48px;  
  29.     border-radius: 50%;  
  30.     border: none;  
  31.     background: #58b325;  
  32.     box-shadow: 0 4px 16px rgba(0000.10);  
  33.     color: #ffffff;  
  34.     font-size: 20px;  
  35.     cursor: pointer;  
  36.     display: flex;  
  37.     align-items: center;  
  38.     justify-content: center;  
  39.     transition: opacity 0.4s ease, transform 0.4s ease, background 0.25s, box-shadow 0.25s;  
  40.     backdrop-filter: blur(4px);  
  41.     border: 1px solid #DDD;  
  42.   
  43.     /* 默认隐藏(不可见、不可点击) */  
  44.     opacity: 0;  
  45.     transform: translateY(10px);  
  46.     pointer-events: none;  
  47. }  
  48.   
  49. /* 显示状态 - 通过 .show 类控制 */  
  50. .scroll-btn.show {  
  51.     opacity: .8;  
  52.     transform: translateY(0);  
  53.     pointer-events: auto;  
  54. }  
  55.   
  56. /* 悬停效果(可自定义颜色) */  
  57. .scroll-btn:hover {  
  58.     background: #ff6b35;  
  59.     color: #fff;  
  60.     transform: scale(1.05);  
  61.     box-shadow: 0 6px 20px rgba(255107530.25);  
  62. }  
  63.   
  64. /* 移动端适配(可选) */  
  65. @media (max-width: 480px) {  
  66.     .scroll-buttons {  
  67.         right: 16px;  
  68.         bottom: 80px;  
  69.         gap: 10px;  
  70.     }  
  71.     .scroll-btn {  
  72.         width: 42px;  
  73.         height: 42px;  
  74.         font-size: 16px;  
  75.     }  
  76. }  
  77. </style>  
  78. <script>  
  79. (function() {  
  80.     'use strict';  
  81.   
  82.     var topBtn = document.getElementById('scrollTopBtn');  
  83.     var bottomBtn = document.getElementById('scrollBottomBtn');  
  84.   
  85.     // 如果元素不存在则退出  
  86.     if (!topBtn || !bottomBtn) return;  
  87.   
  88.     // 更新按钮显示状态  
  89.     function updateButtons() {  
  90.         var scrollY = window.scrollY || window.pageYOffset || 0;  
  91.         var docHeight = document.documentElement.scrollHeight;  
  92.         var winHeight = window.innerHeight;  
  93.   
  94.         // 距离底部 80px 以内视为“到达底部”  
  95.         var bottomReached = (winHeight + scrollY) >= (docHeight - 80);  
  96.   
  97.         // 顶部按钮:滚动超过 200px 时显示  
  98.         if (scrollY > 200) {  
  99.             topBtn.classList.add('show');  
  100.         } else {  
  101.             topBtn.classList.remove('show');  
  102.         }  
  103.   
  104.         // 底部按钮:未到达底部时显示  
  105.         if (bottomReached) {  
  106.             bottomBtn.classList.remove('show');  
  107.         } else {  
  108.             bottomBtn.classList.add('show');  
  109.         }  
  110.     }  
  111.   
  112.     // 滚动节流(避免频繁触发)  
  113.     var timer = null;  
  114.     function throttledUpdate() {  
  115.         if (timer) return;  
  116.         timer = setTimeout(function() {  
  117.             updateButtons();  
  118.             timer = null;  
  119.         }, 50);  
  120.     }  
  121.   
  122.     // 监听滚动和窗口大小变化  
  123.     window.addEventListener('scroll', throttledUpdate, { passive: true });  
  124.     window.addEventListener('resize', updateButtons);  
  125.   
  126.     // 点击事件:平滑滚动  
  127.     topBtn.addEventListener('click', function() {  
  128.         window.scrollTo({ top: 0, behavior: 'smooth' });  
  129.     });  
  130.     bottomBtn.addEventListener('click', function() {  
  131.         window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });  
  132.     });  
  133.   
  134.     // 页面加载完成后初始化一次  
  135.     if (document.readyState === 'complete') {  
  136.         updateButtons();  
  137.     } else {  
  138.         window.addEventListener('load', updateButtons);  
  139.     }  
  140. })();  
  141. </script>  

《漂亮的侧边返回顶部和底部代码》有2 条评论

  1. 感谢分享,谢谢站长!!已收藏


提交评论

请拖动滑块对齐白圈完成验证