龙哥最近一直用AI做网站或者修改网站,经常让AI添加侧边返回顶部和底部的按钮,今天把鸡巴付(jibafu.com)改版了一下,感觉这个按钮的代码不错的,于是要AI专门整理出了完整的代码,所以放在博客收藏下,以后就直接拿来使用,免得浪费tokens。
1. HTML 结构(放在 前任意位置)
- <!-- 滚动按钮容器 -->
- <div class="scroll-buttons">
- <button class="scroll-btn scroll-top" id="scrollTopBtn" title="返回顶部">
- <!-- 您可以使用 Font Awesome 图标,或替换为 ↑ 字符 -->
- <i class="fas fa-arrow-up"></i>
- <!-- 如果不用 Font Awesome,可改为: ↑ -->
- </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;
- 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: #ffffff;
- box-shadow: 0 4px 16px rgba(0, 0, 0, 0.10);
- color: #4a5568;
- font-size: 20px;
- cursor: pointer;
- /* 核心过渡效果,可根据喜好调整时长或属性 */
- transition: opacity 0.4s ease-in-out, transform 0.4s ease-in-out, background 0.25s, box-shadow 0.25s;
- display: flex;
- align-items: center;
- justify-content: center;
- backdrop-filter: blur(4px);
- border: 1px solid rgba(255, 255, 255, 0.6);
- /* 默认隐藏(透明且不可点击) */
- opacity: 0;
- transform: translateY(10px);
- pointer-events: none;
- }
- /* 显示状态 */
- .scroll-btn.show {
- opacity: 1;
- transform: translateY(0);
- pointer-events: auto;
- }
- /* 悬停效果(可自定义颜色) */
- .scroll-btn:hover {
- background: #ff6b35; /* 您可改为喜欢的颜色 */
- color: #fff;
- transform: scale(1.05) translateY(0);
- box-shadow: 0 6px 20px rgba(255, 107, 53, 0.25);
- }
3. JavaScript 逻辑(放入脚本文件或内联)
- /**
- * 滚动按钮控制(独立可用)
- * 功能:滚动超过 200px 显示“返回顶部”;滚动到底部时隐藏“返回底部”
- */
- (function() {
- var topBtn = document.getElementById('scrollTopBtn');
- var bottomBtn = document.getElementById('scrollBottomBtn');
- function updateScrollButtons() {
- var scrollY = window.scrollY || window.pageYOffset || 0;
- var docHeight = document.documentElement.scrollHeight;
- var winHeight = window.innerHeight;
- var bottomReached = (winHeight + scrollY) >= (docHeight - 80); // 距离底部 80px 以内视为到底
- // 控制顶部按钮
- if (scrollY > 200) {
- topBtn.classList.add('show');
- } else {
- topBtn.classList.remove('show');
- }
- // 控制底部按钮
- if (bottomReached) {
- bottomBtn.classList.remove('show');
- } else {
- bottomBtn.classList.add('show');
- }
- }
- if (topBtn && bottomBtn) {
- var timer = null;
- // 滚动事件(带节流)
- window.addEventListener('scroll', function() {
- if (timer) return;
- timer = setTimeout(function() {
- updateScrollButtons();
- timer = null;
- }, 50);
- });
- // 窗口大小变化时更新
- window.addEventListener('resize', updateScrollButtons);
- // 初始化
- setTimeout(updateScrollButtons, 100);
- // 点击事件
- topBtn.addEventListener('click', function() {
- window.scrollTo({ top: 0, behavior: 'smooth' });
- });
- bottomBtn.addEventListener('click', function() {
- window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
- });
- }
- })();
使用说明:
复制 HTML 到您需要放置按钮的位置(通常放在 < /body> 前)。
复制 CSS 到您的样式文件中(或 < style> 标签内)。
复制 JavaScript 到您的脚本文件中(或 < script> 标签内)。
调整样式(可选):
修改 right、bottom 调整位置。
修改 background、color 等匹配您的主题色。
修改 transition 时长或缓动函数。
图标:如果您没有 Font Awesome,将 < i> 替换为普通字符(如 ↑ ↓)或 SVG 图标。
注意事项:
该组件不依赖任何第三方库,纯原生实现。
已做滚动节流优化,性能良好。
兼容所有现代浏览器(Chrome、Firefox、Safari、Edge)。
如果您的项目已使用 jQuery,可替换为 jQuery 语法,但非必要。