无法移除图片横向滚动条,求助替换为箭头按钮的解决方案
解决横向图片滚动区替换为箭头按钮的问题
一、彻底移除滚动条
你之前的滚动条隐藏写法兼容性不足,需要针对不同浏览器补充规则,才能彻底隐藏滚动条同时保留滚动功能:
/* Chrome、Safari、Opera 内核浏览器 */ .scroll-images::-webkit-scrollbar { display: none; } /* IE 和 Edge */ .scroll-images { -ms-overflow-style: none; } /* Firefox */ .scroll-images { scrollbar-width: none; }
二、实现箭头按钮的滚动控制
原代码缺少按钮的滚动逻辑,同时按钮图标类名重复,以下是完整修正方案:
1. 修正按钮图标
把两个按钮的图标分别改为左/右箭头(用Unicode字符无需额外图标库):
- 左按钮:
<i>←</i> - 右按钮:
<i>→</i>
2. 添加滚动控制逻辑
在</body>标签前插入JS代码,实现点击按钮滚动图片:
const scrollContainer = document.querySelector('.scroll-images'); const leftBtn = document.querySelector('.main-scroll-div button:first-of-type'); const rightBtn = document.querySelector('.main-scroll-div button:last-of-type'); // 点击左箭头向左滚动 leftBtn.addEventListener('click', () => { scrollContainer.scrollBy({ left: -620, // 对应图片项的min-width+左右margin总和:600+10*2=620 behavior: 'smooth' }); }); // 点击右箭头向右滚动 rightBtn.addEventListener('click', () => { scrollContainer.scrollBy({ left: 620, behavior: 'smooth' }); });
三、完整修正后的代码
整合所有修改后的完整代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Horizontal Scroll Bar</title> <style> *{ margin: 0px; padding: 0px; box-sizing: border-box; background-color: black; } body { display: flex; align-items: center; justify-content: center; height: 100vh; width: 100%; background-color: black; } .main-scroll-div{ width: 90%; height: 100%; display: flex; align-items: center; justify-content: space-between; border: 2px solid red; } .cover{ position: relative; width: 90%; height: 50%; } .scroll-images{ width: 100%; height: auto; display: flex; justify-content: left; align-items: center; overflow: auto; position: relative; scroll-behavior: smooth; /* 隐藏滚动条 */ -ms-overflow-style: none; scrollbar-width: none; } /* 隐藏webkit内核浏览器滚动条 */ .scroll-images::-webkit-scrollbar{ display: none; } .child{ min-width: 600px; height: 450px; margin: 1px 10px; cursor: pointer; border: 1px solid white; overflow: hidden; } .child-img{ width: 100%; height: 100%; object-fit: cover; /* 可选:防止图片拉伸变形 */ } .icon{ color: green; background-color: black; font-size: 50px; outline: none; border: none; padding: 0px 20px; /* 修正原代码的opx拼写错误 */ cursor: pointer; } </style> </head> <body> <div class="main-scroll-div"> <div> <button class="icon"> <i>←</i></button> </div> <div class="cover"> <div class="scroll-images"> <div class="child"><img class="child-img" src="imges/1.jpg" alt="image"></div> <div class="child"><img class="child-img" src="imges/2.jpg" alt="image"></div> <div class="child"><img class="child-img" src="imges/3.jpg" alt="image"></div> <div class="child"><img class="child-img" src="imges/4.jpg" alt="image"></div> </div> </div> <div> <button class="icon"> <i>→</i></button> </div> </div> <script> const scrollContainer = document.querySelector('.scroll-images'); const leftBtn = document.querySelector('.main-scroll-div button:first-of-type'); const rightBtn = document.querySelector('.main-scroll-div button:last-of-type'); leftBtn.addEventListener('click', () => { scrollContainer.scrollBy({ left: -620, behavior: 'smooth' }); }); rightBtn.addEventListener('click', () => { scrollContainer.scrollBy({ left: 620, behavior: 'smooth' }); }); </script> </body> </html>
关键修正说明
- 滚动条隐藏:补充跨浏览器规则,确保所有主流浏览器都能隐藏滚动条
- 按钮逻辑:通过
scrollBy方法实现平滑滚动,滚动距离匹配图片项的总宽度 - 细节修复:修正了原代码中
padding: opx的拼写错误,添加object-fit: cover优化图片显示
内容的提问来源于stack exchange,提问作者JustaNobody
相关产品推荐
相关产品推荐

