(function() { // 1. 注入样式 const style = document.createElement('style'); style.textContent = ` .scroll-btn { position: fixed; right: 20px; width: 40px; height: 40px; background-color: rgba(0, 0, 0, 0.7); color: white; border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.9); /* 增加白色边框 */ display: flex; justify-content: center; align-items: center; cursor: pointer; z-index: 9999; box-shadow: 0 4px 12px rgba(0,0,0,0.5); /* 加重阴影 */ transition: all 0.3s; user-select: none; -webkit-tap-highlight-color: transparent; } .scroll-btn:hover { background-color: rgba(0, 0, 0, 0.9); border-color: #fff; transform: scale(1.05); box-shadow: 0 6px 16px rgba(0,0,0,0.6); } .scroll-btn:active { transform: scale(0.95); } /* 箭头基础样式 */ .scroll-btn::after { content: ''; width: 10px; height: 10px; border-right: 2px solid white; border-bottom: 2px solid white; } /* 下一页按钮位置和箭头 */ .next-btn { bottom: 20%; } .next-btn::after { transform: rotate(45deg); margin-top: -4px; } /* 上一页按钮位置和箭头 */ .prev-btn { bottom: calc(20% + 55px); /* 40px height + 15px gap */ } .prev-btn::after { transform: rotate(-135deg); /* 向上箭头 */ margin-top: 4px; } /* 回到顶部按钮 */ .top-btn { bottom: calc(20% + 110px); /* 40px * 2 + 15px * 2 */ opacity: 0; pointer-events: none; transition: opacity 0.3s, background-color 0.3s, transform 0.2s; } .top-btn.visible { opacity: 1; pointer-events: auto; } /* 利用 before 画横线,after 画箭头 */ .top-btn::before { content: ''; position: absolute; width: 12px; height: 2px; background-color: white; top: 12px; /* 调整横线位置 */ } .top-btn::after { transform: rotate(-135deg); margin-top: 2px; /* 调整箭头位置,使其位于横线下方 */ } /* 移动端适配 */ @media (max-width: 768px) { .scroll-btn { width: 40px; height: 40px; right: 15px; background-color: rgba(0, 0, 0, 0.5); } .next-btn { bottom: 100px; } .prev-btn { bottom: 155px; /* 100px + 40px height + 15px gap */ } .top-btn { bottom: 218px; /* 163px + 40px height + 15px gap */ } .top-btn::before { top: 10px; /* 移动端按钮大一点,微调位置 */ } } `; document.head.appendChild(style); // 辅助函数:创建按钮 function createBtn(className, title) { const btn = document.createElement('div'); btn.className = `scroll-btn ${className}`; btn.title = title; document.body.appendChild(btn); return btn; } // 2. 创建按钮 const topBtn = createBtn('top-btn', '回到顶部'); const prevBtn = createBtn('prev-btn', '上一屏'); const nextBtn = createBtn('next-btn', '下一屏'); // 3. 滚动逻辑 function scrollByViewport(direction) { const viewportHeight = window.innerHeight; const currentScroll = window.scrollY || window.pageYOffset; const targetScroll = currentScroll + (direction * viewportHeight); window.scrollTo({ top: targetScroll, behavior: 'smooth' }); } // 4. 绑定事件 nextBtn.addEventListener('click', function() { scrollByViewport(1); }); prevBtn.addEventListener('click', function() { scrollByViewport(-1); }); topBtn.addEventListener('click', function() { window.scrollTo({ top: 0, behavior: 'smooth' }); }); // 5. 监听滚动,控制回到顶部按钮显隐 function handleScroll() { const viewportHeight = window.innerHeight; const currentScroll = window.scrollY || window.pageYOffset; // 当滚动超过一屏时显示 if (currentScroll >= viewportHeight) { topBtn.classList.add('visible'); } else { topBtn.classList.remove('visible'); } } // 初始检查一次 handleScroll(); // 添加监听,使用 passive 提高性能 window.addEventListener('scroll', handleScroll, { passive: true }); })();