Javascript 图像 slider 高度问题

标签 javascript css html

我一直致力于制作我的图像 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/

相关文章:

php - 从html到mysql数据库的基本输入

javascript - 尝试向各个元素添加单击事件,但希望它们一次执行一个

javascript - 在JS中创建 Canvas 并绘制文本时,创建更多 Canvas 时文本会被覆盖

javascript - 如何用 jquery 中使用的另一个字符替换 'dollar' 符号?

css - Wordpress 子主题样式表似乎不起作用

javascript - Wordpress 定制器、FS 和 :after CSS

javascript - Jquery可拖动图像拖放到左上角所在的div

javascript - 使用 RequireJS 和 Jasmine 监视由 jQuery 'change' 调用的函数

php - 首次加载 Minify 时出现错误 330 net::ERR_CONTENT_DECODING_FAILED

javascript - 交换文本标签以选择值