gpt4 book ai didi

Javascript 图像 slider 高度问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:42:57 24 4
gpt4 key购买 nike

我一直致力于制作我的图像 slider (基于 w3schools slider ),但我希望它有点不同。现在我对 html 和 css 还不是很好,我似乎无法找到为什么叠加层(见图片)比 img 稍微大一点,我找不到匹配 img 高度的解决方案。

以为是图像改变了几次,但似乎并没有解决问题。

还包括代码

Overlay

HTML

<div class="slideshow-container">
<div class="mySlides fade">
<img src="img/img1.jpg" style="width:100%">
</div>

<div class="mySlides fade">
<img src="img/img2.jpg" style="width:100%">
</div>

<div class="mySlides fade">
<img src="img/img3.jpg" style="width:100%">
</div>
<div class="prev" onclick="plusSlides(-1)">
<a class="arrows" onclick="plusSlides(-1)">&#10094;</a>
</div>
<div class="next" onclick="plusSlides(1)">
<a class="arrows" onclick="plusSlides(1)">&#10095;</a>
</div>
<div class="dot-container">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
</div>

CSS

/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
overflow: hidden;
}

/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 0;
height: 100%;
padding-left: 5%;
padding-right: 5%;
transition: 0.6s ease;
}

.arrows{
position: absolute;
top: 50%;
left: 40%;
color: white;
font-weight: bold;
font-size: 18px;
}

/* Position the "next button" to the right */
.next {
right: 0;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.4);
}

/* Caption text */
.dot-container {
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}

/* The dots/bullets/indicators */
.dot {
cursor:pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}

.active, .dot:hover {
background-color: #717171;
}

/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}

@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

@media only screen and (max-width: 500px) {
.slideshow-container {
max-width: 80%;
}
}

Javascript

 var slideIndex = 0;
var clicked;
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
slide();

function slide()
{
if(!clicked){
autoSlide();
}else{
showSlides(slideIndex);
}
}

function plusSlides(n) {
showSlides(slideIndex += n);
clicked = true;
}

function currentSlide(n) {
showSlides(slideIndex = n);
clicked = true;
}

function showSlides(n) {
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "";
dots[slideIndex-1].className += " active";
}

function autoSlide()
{
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slideIndex++;
if (slideIndex> slides.length) {slideIndex = 1}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
setTimeout(slide, 5000); // Change image every 5 seconds
}

最佳答案

您可以使用这个技巧来实现您的目标:

.slideshow-container {
font-size: 0;
}

关于Javascript 图像 slider 高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43984839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com