Javascript图像滑块高度问题

时间:2017-05-15 16:46:06

标签: javascript css html5

我一直致力于制作我的on image滑块(基于w3schools滑块),但我希望它有点不同。现在我用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
    }

1 个答案:

答案 0 :(得分:1)

您可以使用此技巧来实现目标:

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