为什么图像轮播上不会立即弹出图像?

时间:2017-08-16 03:13:06

标签: javascript html css image carousel

我正在为一家关于飞机的虚构公司制作一个图像轮播。我稍后会做更多的样式,所以请原谅代码的极端平淡。我从互联网上提取了一些库存图片。我真的很困惑,因为我不知道我做错了什么。感谢您花时间阅读我的帖子,这真的意味着很多。为什么不弹出图像?这是我的代码:

HTML:

            <div class="bulkOfPage">
                <!-- Image Carousel Goes Here -->
                <!-- Work More on This -->
                <div class="slideshow-container">
                  <div class="mySlides fade">
                    <div class="numbertext">1 / 3</div>
                    <img src="img/airplanes-work-1.jpg" style="width:100%">
                    <div class="text">Our fleet is the most updated in the business, with our planes being decked out in the latest equipment</div>
                  </div>

                  <div class="mySlides fade">
                    <div class="numbertext">2 / 3</div>
                    <img src="img/landing1015-airplane.jpg" style="width:100%">
                    <div class="text">We have the most trained pilots in the industry, and this gives the smoothest landings you will find.</div>
                  </div>

                  <div class="mySlides fade">
                    <div class="numbertext">3 / 3</div>
                    <img src="img/download.jpeg" style="width:100%">
                    <div class="text">We have landing rights at almost all of the major airports around the world.</div>
                  </div>

                  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
                  <a class="next" onclick="plusSlides(1)">&#10095;</a>
                </div>
                <br>

                <div style="text-align:center">
                  <span class="dot" onclick="currentSlide(1)"></span> 
                  <span class="dot" onclick="currentSlide(2)"></span> 
                  <span class="dot" onclick="currentSlide(3)"></span> 
                </div>

我的CSS:

* {box-sizing:border-box}

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

.mySlides {
    display: none;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

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

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* 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}
}

最后,JavaScript:

$(document).ready(function(){
    var slideIndex = 0;
    showSlides();

    function showSlides() {
        var i;
        var slides = document.getElementsByClassName("mySlides");
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none"; 
        }
        slideIndex++;
        if (slideIndex> slides.length) {slideIndex = 1} 
        slides[slideIndex-1].style.display = "block"; 
        setTimeout(showSlides, 2000); // Change image every 2 seconds
    }
});

非常感谢您阅读我的帖子,这对我来说意义重大。谢谢!祝你度过愉快的一天!

1 个答案:

答案 0 :(得分:0)

这应该有效。我为你的下一个/上一个按钮和点按钮添加了功能。

注意,由于范围的原因,使用div onclick =“plusSlides(1)”等触发器无法访问document.ready中发生的事情。因此要么定义document.ready之外的函数,要么使用jQuery方式代替onclick =“”,例如:$('。dot')。点击

就个人而言,我不喜欢你使用基于1的索引。我认为这增加了混乱;它增加-1,+ 1,......我建议您将来使用0基础索引。不要试图让第一个元素作为索引。

<body>
<div class="bulkOfPage">
    <!-- Image Carousel Goes Here -->
    <!-- Work More on This -->
    <div class="slideshow-container">
        <div class="mySlides fade">
            <div class="numbertext">1 / 3</div>
            <img src="https://www.w3schools.com/css/img_fjords.jpg" style="width:100%">
            <div class="text">Our fleet is the most updated in the business, with our planes being decked out in the latest equipment</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">2 / 3</div>
            <img src="https://www.w3schools.com/css/img_forest.jpg" style="width:100%">
            <div class="text">We have the most trained pilots in the industry, and this gives the smoothest landings you will find.</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">3 / 3</div>
            <img src="https://www.w3schools.com/css/img_lights.jpg" style="width:100%">
            <div class="text">We have landing rights at almost all of the major airports around the world.</div>
        </div>

        <a class="prev">&#10094;</a>
        <a class="next">&#10095;</a>
    </div>
    <br>
    <div style="text-align:center">
        <span class="dot" data-index="1"></span>
        <span class="dot" data-index="2"></span>
        <span class="dot" data-index="3"></span>
    </div>
    <style>
        * {
            box-sizing: border-box
        }
        /* Slideshow container */

        .slideshow-container {
            max-width: 1000px;
            position: relative;
            margin: auto;
        }

        .mySlides {
            display: none;
        }
        /* Next & previous buttons */

        .prev,
        .next {
            cursor: pointer;
            position: absolute;
            top: 50%;
            width: auto;
            margin-top: -22px;
            padding: 16px;
            color: white;
            font-weight: bold;
            font-size: 18px;
            transition: 0.6s ease;
            border-radius: 0 3px 3px 0;
        }
        /* Position the "next button" to the right */

        .next {
            right: 0;
            border-radius: 3px 0 0 3px;
        }
        /* On hover, add a black background color with a little bit see-through */

        .prev:hover,
        .next:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }
        /* Caption text */

        .text {
            color: #f2f2f2;
            font-size: 15px;
            padding: 8px 12px;
            position: absolute;
            bottom: 8px;
            width: 100%;
            text-align: center;
        }
        /* Number text (1/3 etc) */

        .numbertext {
            color: #f2f2f2;
            font-size: 12px;
            padding: 8px 12px;
            position: absolute;
            top: 0;
        }
        /* 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
            }
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            var timer;
            var slideIndex = 0;
            showSlides();
            function showSlides() {
                var i;
                var slides = document.getElementsByClassName("mySlides");
                for (i = 0; i < slides.length; i++) {
                    slides[i].style.display = "none";
                }
                slideIndex++;
                if (slideIndex > slides.length) {
                    slideIndex = 1
                }
                slides[slideIndex - 1].style.display = "block";
                timer = setTimeout(showSlides, 4000); // Change image every 4 seconds
            }

            // click on dot.  Takes you straight to that picture
            $('.dot').click(function() {
               slideIndex = $(this).data('index') -1;  // this reads the value of <... data-index="..." >
               clearTimeout(timer);
               showSlides(); 
            })
            // next / prev buttons
            function next() {
                clearTimeout(timer);
                var slides = document.getElementsByClassName("mySlides");
                slideIndex++;
                if (slideIndex > slides.length) {
                    slideIndex = 1
                }
                showSlides();
            }
            $('.next').click(function() {
               next();
            })
            function prev() {
                clearTimeout(timer);
                var slides = document.getElementsByClassName("mySlides");
                slideIndex -=2;
                if (slideIndex < 0) {
                    slideIndex = slides.length -1;
                }
                showSlides();
            }
            $('.prev').click(function() {
               prev();
            })
        });

    </script>
</body>