简体   繁体   English

图像不适合滑块

[英]Images do not fit inside slider

I've created an image slider using HTML and JS.我使用 HTML 和 JS 创建了一个图像滑块。 One problem I've come across that I cant quite work myself around is how the image slider transmits multiple images for each slide when the dimensions of the image does not perfectly fit the box.我遇到的一个我无法完全解决的问题是,当图像的尺寸不完全适合盒子时,图像滑块如何为每张幻灯片传输多个图像。 I would like to change this code so that there is only one image displayed at a single time + the image is centered to the box.我想更改此代码,以便一次只显示一个图像+图像以框为中心。 Does anyone have any suggestions on to how I would edit this?有人对我将如何编辑它有任何建议吗? Please let me know if any additional information is needed.如果需要任何其他信息,请告诉我。

 const carouselSlide = document.querySelector('.carousel-slide'); const carouselImages = document.querySelectorAll('.carousel-slide img'); const prevBtn = document.querySelector('#prevBtn'); const nextBtn = document.querySelector('#nextBtn'); let counter = 1; const size = carouselImages[0].clientWidth; carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)'; //Button Listeners nextBtn.addEventListener('click', () => { if (counter >= carouselImages.length - 1) { return; } carouselSlide.style.tranisition = "transform 0.4s ease-in-out" counter++; carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)'; }); prevBtn.addEventListener('click', () => { if (counter <= 0) { return; } carouselSlide.style.tranisition = "transform 0.4s ease-in-out" counter--; carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)'; }); carouselSlide.addEventListener('transitonend', () => { if (carouselImages[counter].id === 'lastclone') { carouselSlide.style.transition = "none"; counter = carouselImage.length - 2; carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)'; } if (carouselImages[counter].id === 'firstclone') { carouselSlide.style.transition = "none"; counter = carouselImage.length - counter; carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)'; } });
 <div class="carousel-container"> <i class="fa fa-arrow-left" id="prevBtn" aria-hidden="true"></i> <i class="fa fa-arrow-right" id="nextBtn" aria-hidden="true"></i> <div class="carousel-slide"> <img src="public/images/homme.jpeg" id="lastClone" alt=""> <img src="public/images/homme.jpeg" alt=""> <img src="public/images/homme.jpeg" alt=""> <img src="public/images/homme.jpeg" alt=""> <img src="public/images/homme.jpeg" id="firstClone" alt=""> </div> </div> </div>

One option would be to place each image in the center of its own container div that has the size of the largest image, line up those divs, and have your carouselSlide slide over those divs, so that it only shows one div at a time.一种选择是将每个图像放置在其自己的具有最大图像大小的容器 div 的中心,排列这些 div,然后让 carouselSlide 在这些 div 上滑动,以便一次只显示一个 div。

This may produce margins for images that are smaller than the largest image.这可能会为小于最大图像的图像产生边距。 One way to solve that is to use CSS to set the images as background images of the divs, and use CSS to center the images and fill the div.解决这个问题的一种方法是使用 CSS 将图像设置为 div 的背景图像,并使用 CSS 将图像居中并填充 div。

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

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