龙哥最近一直用AI做网站或者修改网站,经常让AI添加侧边返回顶部和底部的按钮,今天把鸡巴付(jibafu.com)改版了一下,感觉这个按钮的代码不错的,于是要AI专门整理出了完整的代码,所以放在博客收藏下,以后就直接拿来使用,免得浪费tokens。
1. HTML 结构(放在 前任意位置)
- <!-- 滚动按钮容器 -->
- <div class="scroll-buttons">
- <button class="scroll-btn scroll-top" id="scrollTopBtn" title="返回顶部">
- <!-- 使用 Unicode 箭头字符,不依赖 Font Awesome -->
- ↑
- <!-- 如果您使用了 Font Awesome,可将上面一行替换为:<i class="fas fa-arrow-up"></i> -->
- </button>
- <button class="scroll-btn scroll-bottom" id="scrollBottomBtn" title="返回底部">
- ↓
- <!-- 同理,可替换为:<i class="fas fa-arrow-down"></i> -->
- </button>
- </div>
提示:如果您的项目未引入 Font Awesome,请将 替换为 ↑ 或使用 SVG 图标。
2. CSS 样式(放入您的样式表)
- /* 滚动按钮容器 - 固定于右侧 */
- .scroll-buttons {
- position: fixed;
- rightright: 24px;
- bottombottom: 100px; /* 距离底部位置,可调整 */
- display: flex;
- flex-direction: column;
- gap: 12px;
- z-index: 999;
- }
- /* 单个按钮样式 */
- .scroll-btn {
- width: 48px;
- height: 48px;
- border-radius: 50%;
- border: none;
- background: #ffffff;
- box-shadow: 0 4px 16px rgba(0, 0, 0, 0.10);
- color: #4a5568;
- font-size: 20px;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- transition: opacity 0.4s ease, transform 0.4s ease, background 0.25s, box-shadow 0.25s;
- backdrop-filter: blur(4px);
- border: 1px solid rgba(255, 255, 255, 0.6);
- /* 默认隐藏(不可见、不可点击) */
- opacity: 0;
- transform: translateY(10px);
- pointer-events: none;
- }
- /* 显示状态 - 通过 .show 类控制 */
- .scroll-btn.show {
- opacity: 1;
- transform: translateY(0);
- pointer-events: auto;
- }
- /* 悬停效果(可自定义颜色) */
- .scroll-btn:hover {
- background: #ff6b35;
- color: #fff;
- transform: scale(1.05);
- box-shadow: 0 6px 20px rgba(255, 107, 53, 0.25);
- }
- /* 移动端适配(可选) */
- @media (max-width: 480px) {
- .scroll-buttons {
- rightright: 16px;
- bottombottom: 80px;
- gap: 10px;
- }
- .scroll-btn {
- width: 42px;
- height: 42px;
- font-size: 16px;
- }
- }
3. JavaScript 逻辑(放入脚本文件或内联)
- (function() {
- 'use strict';
- var topBtn = document.getElementById('scrollTopBtn');
- var bottomBtn = document.getElementById('scrollBottomBtn');
- // 如果元素不存在则退出
- if (!topBtn || !bottomBtn) return;
- // 更新按钮显示状态
- function updateButtons() {
- var scrollY = window.scrollY || window.pageYOffset || 0;
- var docHeight = document.documentElement.scrollHeight;
- var winHeight = window.innerHeight;
- // 距离底部 80px 以内视为“到达底部”
- var bottomReached = (winHeight + scrollY) >= (docHeight - 80);
- // 顶部按钮:滚动超过 200px 时显示
- if (scrollY > 200) {
- topBtn.classList.add('show');
- } else {
- topBtn.classList.remove('show');
- }
- // 底部按钮:未到达底部时显示
- if (bottomReached) {
- bottomBtn.classList.remove('show');
- } else {
- bottomBtn.classList.add('show');
- }
- }
- // 滚动节流(避免频繁触发)
- var timer = null;
- function throttledUpdate() {
- if (timer) return;
- timer = setTimeout(function() {
- updateButtons();
- timer = null;
- }, 50);
- }
- // 监听滚动和窗口大小变化
- window.addEventListener('scroll', throttledUpdate, { passive: true });
- window.addEventListener('resize', updateButtons);
- // 点击事件:平滑滚动
- topBtn.addEventListener('click', function() {
- window.scrollTo({ top: 0, behavior: 'smooth' });
- });
- bottomBtn.addEventListener('click', function() {
- window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
- });
- // 页面加载完成后初始化一次
- if (document.readyState === 'complete') {
- updateButtons();
- } else {
- window.addEventListener('load', updateButtons);
- }
- })();
使用说明
复制 HTML 到您页面的任意位置(建议放在 < body> 结束前)。
复制 CSS 到您的样式文件中,或放在 < style> 标签内。
复制 JavaScript 到您的脚本文件中,或放在 < script> 标签内(确保在 DOM 加载后执行,或放在页面底部)。
不依赖任何外部库:按钮使用 Unicode 箭头(↑ / ↓),若您已引入 Font Awesome,可自行替换为图标标签。
引入Font Awesome图标添加css库:
- <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>前面即可:
- <!-- 滚动按钮容器 -->
- <div class="scroll-buttons">
- <button class="scroll-btn scroll-top" id="scrollTopBtn" title="返回顶部">
- <!-- 使用 Unicode 箭头字符,不依赖 Font Awesome -->
- ↑
- <!-- 如果您使用了 Font Awesome,可将上面一行替换为:<i class="fas fa-arrow-up"></i> -->
- </button>
- <button class="scroll-btn scroll-bottom" id="scrollBottomBtn" title="返回底部">
- ↓
- <!-- 同理,可替换为:<i class="fas fa-arrow-down"></i> -->
- </button>
- </div>
- <style>
- /* 滚动按钮容器 - 固定于右侧 */
- .scroll-buttons {
- position: fixed;
- right: 24px;
- bottom: 100px; /* 距离底部位置,可调整 */
- display: flex;
- flex-direction: column;
- gap: 12px;
- z-index: 999;
- }
- /* 单个按钮样式 */
- .scroll-btn {
- width: 48px;
- height: 48px;
- border-radius: 50%;
- border: none;
- background: #58b325;
- box-shadow: 0 4px 16px rgba(0, 0, 0, 0.10);
- color: #ffffff;
- font-size: 20px;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- transition: opacity 0.4s ease, transform 0.4s ease, background 0.25s, box-shadow 0.25s;
- backdrop-filter: blur(4px);
- border: 1px solid #DDD;
- /* 默认隐藏(不可见、不可点击) */
- opacity: 0;
- transform: translateY(10px);
- pointer-events: none;
- }
- /* 显示状态 - 通过 .show 类控制 */
- .scroll-btn.show {
- opacity: .8;
- transform: translateY(0);
- pointer-events: auto;
- }
- /* 悬停效果(可自定义颜色) */
- .scroll-btn:hover {
- background: #ff6b35;
- color: #fff;
- transform: scale(1.05);
- box-shadow: 0 6px 20px rgba(255, 107, 53, 0.25);
- }
- /* 移动端适配(可选) */
- @media (max-width: 480px) {
- .scroll-buttons {
- right: 16px;
- bottom: 80px;
- gap: 10px;
- }
- .scroll-btn {
- width: 42px;
- height: 42px;
- font-size: 16px;
- }
- }
- </style>
- <script>
- (function() {
- 'use strict';
- var topBtn = document.getElementById('scrollTopBtn');
- var bottomBtn = document.getElementById('scrollBottomBtn');
- // 如果元素不存在则退出
- if (!topBtn || !bottomBtn) return;
- // 更新按钮显示状态
- function updateButtons() {
- var scrollY = window.scrollY || window.pageYOffset || 0;
- var docHeight = document.documentElement.scrollHeight;
- var winHeight = window.innerHeight;
- // 距离底部 80px 以内视为“到达底部”
- var bottomReached = (winHeight + scrollY) >= (docHeight - 80);
- // 顶部按钮:滚动超过 200px 时显示
- if (scrollY > 200) {
- topBtn.classList.add('show');
- } else {
- topBtn.classList.remove('show');
- }
- // 底部按钮:未到达底部时显示
- if (bottomReached) {
- bottomBtn.classList.remove('show');
- } else {
- bottomBtn.classList.add('show');
- }
- }
- // 滚动节流(避免频繁触发)
- var timer = null;
- function throttledUpdate() {
- if (timer) return;
- timer = setTimeout(function() {
- updateButtons();
- timer = null;
- }, 50);
- }
- // 监听滚动和窗口大小变化
- window.addEventListener('scroll', throttledUpdate, { passive: true });
- window.addEventListener('resize', updateButtons);
- // 点击事件:平滑滚动
- topBtn.addEventListener('click', function() {
- window.scrollTo({ top: 0, behavior: 'smooth' });
- });
- bottomBtn.addEventListener('click', function() {
- window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
- });
- // 页面加载完成后初始化一次
- if (document.readyState === 'complete') {
- updateButtons();
- } else {
- window.addEventListener('load', updateButtons);
- }
- })();
- </script>
行,
感谢分享,谢谢站长!!已收藏