简体   繁体   English

CSS 图库 Slider 问题

[英]CSS Gallery Slider Issue

I have been trying to create a simple CSS and HTML only image gallery slider.我一直在尝试创建一个简单的 CSS 和 HTML 仅图像库 slider。 This is just for practice and it's really bugging me.这只是为了练习,它真的让我很烦。 When I click the arrow and the container slides over, it slides the full page including the navigation and footer to the left, is there any way I can improve this so that only the images slide over?当我单击箭头并且容器滑过时,它会将整个页面(包括导航和页脚)滑到左侧,有什么办法可以改进这一点,以便只有图像滑过? I've tried messing with the overflow etc.我试过弄乱溢出等。

 .galleryw { display: grid; grid-template-columns: repeat(3, 100%); scroll-behavior: smooth; margin-top: 5px; padding-bottom: 40px }.galleryw section { width: 100%; position: relative; display: grid; transition: transform.2s; grid-template-columns: repeat(6, auto); margin: 10px 0; }.galleryw section.item { padding: 0 2px; transition: 250ms all; }.galleryw section.item:hover { -ms-transform: scale(1.5); /* IE 9 */ -webkit-transform: scale(1.5); /* Safari 3-8 */ transform: scale(1.5); }.galleryw section a { position: absolute; color: #000; text-decoration: none; font-size: 6em; width: 40px; padding: 10px; text-align: center; z-index: 1; }.galleryw section a:nth-of-type(1) { top: 0; bottom: 0; left: 0; }.galleryw section a:nth-of-type(2) { top: 1; bottom: 0; right: 0; }
 <div class="titles"><h2>ANIMALS<span class="red">.</span></h2></div> <div class="galleryw"> <section id="section1"> <a href="#section2">‹</a> <div class="item"> <img src="img/zebs.png" /><br><h3>ZEBRA<span class="red">.</span></h3> </div> <div class="item"> <img src="img/toitles.png" /><br><h3>TURTLE<span class="red">.</span></h3> </div> <div class="item"> <img src="img/dolph.png" /> <br><h3>DOLPHIN<span class="red">.</span></h3> </div> <div class="item"> <img src="img/re.png" /><br><h3>CHAMELEON<span class="red">.</span></h3> </div> <div class="item"> <img src="img/king.png" /><br><h3>KINGFISHER</h3> </div> <div class="item"> <img src="img/arc.png" /><br><h3>SNOWFOX<span class="red">.</span></h3> </div> <a href="#section2">›</a> </section> <section id="section2"> <a href="#section1">‹</a> <div class="item"> <img src="img/king2.png" /><br><h3>KINGFISHER<span class="red">.</span></h3> </div> <div class="item"> <img src="img/ele2.png" /><br><h3>ELEPHANT<span class="red">.</span></h3> </div> <div class="item"> <img src="img/rac.png" /> <br><h3>RACCOON<span class="red">.</span></h3></div> <div class="item"> <img src="img/rab.png" /><br><h3>RABBIT<span class="red">.</span></h3> </div> <div class="item"> <img src="img/deer.png" /><br><h3>DEER<span class="red">.</span></h3> </div> <div class="item"> <img src="img/pea.png" /><br><h3>PEACOCK<span class="red">.</span></h3> </div> <a href="#section1">›</a> </section> </div>

You can try it here你可以在这里试试

http://kny.me/practice/gallery.html http://kny.me/practice/gallery.html

Set overflow-x to auto on the gallery.在画廊中将overflow-x设置为auto

 .galleryw { display: grid; grid-template-columns: repeat(3, 100%); scroll-behavior: smooth; margin-top: 5px; padding-bottom: 40px; overflow-x:auto; }.galleryw section { width: 100%; position: relative; display: grid; transition: transform.2s; grid-template-columns: repeat(6, auto); margin: 10px 0; }.galleryw section.item { padding: 0 2px; transition: 250ms all; }.galleryw section.item:hover { -ms-transform: scale(1.5); /* IE 9 */ -webkit-transform: scale(1.5); /* Safari 3-8 */ transform: scale(1.5); }.galleryw section a { position: absolute; color: #000; text-decoration: none; font-size: 6em; width: 40px; padding: 10px; text-align: center; z-index: 1; }.galleryw section a:nth-of-type(1) { top: 0; bottom: 0; left: 0; }.galleryw section a:nth-of-type(2) { top: 1; bottom: 0; right: 0; }
 <div class="titles"><h2>ANIMALS<span class="red">.</span></h2></div> <div class="galleryw"> <section id="section1"> <a href="#section2">‹</a> <div class="item"> <img src="img/zebs.png" /><br><h3>ZEBRA<span class="red">.</span></h3> </div> <div class="item"> <img src="img/toitles.png" /><br><h3>TURTLE<span class="red">.</span></h3> </div> <div class="item"> <img src="img/dolph.png" /> <br><h3>DOLPHIN<span class="red">.</span></h3> </div> <div class="item"> <img src="img/re.png" /><br><h3>CHAMELEON<span class="red">.</span></h3> </div> <div class="item"> <img src="img/king.png" /><br><h3>KINGFISHER</h3> </div> <div class="item"> <img src="img/arc.png" /><br><h3>SNOWFOX<span class="red">.</span></h3> </div> <a href="#section2">›</a> </section> <section id="section2"> <a href="#section1">‹</a> <div class="item"> <img src="img/king2.png" /><br><h3>KINGFISHER<span class="red">.</span></h3> </div> <div class="item"> <img src="img/ele2.png" /><br><h3>ELEPHANT<span class="red">.</span></h3> </div> <div class="item"> <img src="img/rac.png" /> <br><h3>RACCOON<span class="red">.</span></h3></div> <div class="item"> <img src="img/rab.png" /><br><h3>RABBIT<span class="red">.</span></h3> </div> <div class="item"> <img src="img/deer.png" /><br><h3>DEER<span class="red">.</span></h3> </div> <div class="item"> <img src="img/pea.png" /><br><h3>PEACOCK<span class="red">.</span></h3> </div> <a href="#section1">›</a> </section> </div>

In order to scroll just the galleryw element and not the whole page you need to add overflow-x: hidden or overflow-x: auto to your galleryw css class.为了只滚动galleryw元素而不是整个页面,您需要将overflow-x: hiddenoverflow-x: auto添加到您的galleryw css class。
The vertical jump is due to the implementation of the anchor links which will use the ScrollIntoView js function in order to get the section you want on the screen.垂直跳转是由于锚链接的实现,它将使用ScrollIntoView js function 以便在屏幕上获得您想要的部分。
By default, the alignment is to the top-left corner so you need either to re-arrange your html or to use some javascript with some minor html tweaks. By default, the alignment is to the top-left corner so you need either to re-arrange your html or to use some javascript with some minor html tweaks.
I'm presenting you the second solution since is way faster and doesn't need you to change the whole layout of your page:我正在向您展示第二种解决方案,因为它更快并且不需要您更改页面的整个布局:

 function init() { let galleryw = document.getElementsByClassName("galleryw")[0]; let toSection1 = document.getElementsByClassName("toSection1"); let toSection2 = document.getElementsByClassName("toSection2"); for(const button of toSection1) button.onclick = () => galleryw.scrollBy(-galleryw.offsetWidth, 0); for(const button of toSection2) button.onclick = () => galleryw.scrollBy(galleryw.offsetWidth, 0); }
 .galleryw { overflow-x: hidden; /* ADDED */ display: grid; grid-template-columns: repeat(3, 100%); scroll-behavior: smooth; margin-top: 5px; padding-bottom: 40px }.galleryw section { width: 100%; position: relative; display: grid; transition: transform.2s; grid-template-columns: repeat(6, auto); margin: 10px 0; }.galleryw section.item { padding: 0 2px; transition: 250ms all; }.galleryw section.item:hover { -ms-transform: scale(1.5); /* IE 9 */ -webkit-transform: scale(1.5); /* Safari 3-8 */ transform: scale(1.5); }.galleryw section a { position: absolute; color: #000; text-decoration: none; font-size: 6em; width: 40px; padding: 10px; text-align: center; z-index: 1; }.galleryw section a:nth-of-type(1) { top: 0; bottom: 0; left: 0; }.galleryw section a:nth-of-type(2) { top: 1; bottom: 0; right: 0; }.toSection1, .toSection2 { cursor:pointer;/* ADDED */ }
 <body onload = "init()"> <div class="titles"><h2>ANIMALS<span class="red">.</span></h2></div> <div class="galleryw"> <section id="section1"> <a class="toSection2">‹</a><.-- Changed --> <div class="item"> <img src="img/zebs.png" /><br><h3>ZEBRA<span class="red">.</span></h3> </div> <div class="item"> <img src="img/toitles.png" /><br><h3>TURTLE<span class="red">.</span></h3> </div> <div class="item"> <img src="img/dolph.png" /> <br><h3>DOLPHIN<span class="red">.</span></h3> </div> <div class="item"> <img src="img/re.png" /><br><h3>CHAMELEON<span class="red">.</span></h3> </div> <div class="item"> <img src="img/king.png" /><br><h3>KINGFISHER</h3> </div> <div class="item"> <img src="img/arc.png" /><br><h3>SNOWFOX<span class="red">.</span></h3> </div> <a class="toSection2">›</a><.-- Changed --> </section> <section id="section2"> <a class="toSection1">‹</a><.-- Changed --> <div class="item"> <img src="img/king2.png" /><br><h3>KINGFISHER<span class="red">.</span></h3> </div> <div class="item"> <img src="img/ele2.png" /><br><h3>ELEPHANT<span class="red">.</span></h3> </div> <div class="item"> <img src="img/rac.png" /> <br><h3>RACCOON<span class="red">.</span></h3></div> <div class="item"> <img src="img/rab.png" /><br><h3>RABBIT<span class="red">.</span></h3> </div> <div class="item"> <img src="img/deer.png" /><br><h3>DEER<span class="red">.</span></h3> </div> <div class="item"> <img src="img/pea.png" /><br><h3>PEACOCK<span class="red">.</span></h3> </div> <a class="toSection1">›</a> <!-- Changed --> </section> </div> </body>

I've just added the init function which get called onload and changed the anchors to normal <a> tags with a class = "toSection..." to recognize them.我刚刚添加了init function ,它被称为onload并将锚点更改为带有class = "toSection..."的普通<a>标记以识别它们。
The init function uses the scrollBy function on the galleryw so that when our "anchors" get clicked they just scroll the container and not the whole page. init function 在galleryw上使用scrollBy galleryw ,这样当我们的“锚点”被点击时,它们只会滚动容器而不是整个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM