Automatic change pictures in slideshow with jQuery

2019-07-27 22:38发布

I use a working picture slider with jQuery UI. I can change the pictures with the slider. Now, I like to change the pictures automatic additionally in a period (for example 3 seconds).

$(function() {
  var myImages = [
    "img/january/tweet_january_1.svg",
    "img/january/tweet_january_2.svg",
    "img/january/tweet_january_3.svg",
    "img/january/tweet_january_4.svg",
    "img/january/tweet_january_5.svg"
  ];
  $("#start_slider").slider({
    min: 0,
    max: (myImages.length - 1),
    step: 1,
    change: function(event, ui) {
      $('#flask1').attr('src', myImages[ui.value]);
    }
  });
});
.start_heading {
    padding: 2em;
    margin-top: 10em;
    text-align: center;
    font-family: 'Roboto';
    font-size: 1em;
}

#start_slider {
    align-content: center;
    margin-left: 15em;
    margin-right: 15em;
    margin-top: 3em;
    margin-bottom: -10em;
}
<div class="start_heading">
  <h2> Headline</h2>
</div>

<div class="tweets_january">
  <img src="img/january/tweet_january_1.svg" height="" width="" id="flask1" />
  <div id="start_slider"></div>
</div>

1条回答
姐就是有狂的资本
2楼-- · 2019-07-27 23:01

You can use any JQuery plugin for Carousel from here and can also do following

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <style>
            .carousel-inner > .item > img,
            .carousel-inner > .item > a > img {
                width: 70%;
                margin: auto;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <br>
            <div id="myCarousel" class="carousel slide" data-ride="carousel">
                <!-- Indicators -->
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#myCarousel" data-slide-to="1"></li>
                    <li data-target="#myCarousel" data-slide-to="2"></li>
                    <li data-target="#myCarousel" data-slide-to="3"></li>
                </ol>
                <!-- Wrapper for slides -->
                <div class="carousel-inner" role="listbox">
                    <div class="item active">
                        <img src="img_chania.jpg" alt="Chania" width="460" height="345">
                        <div class="carousel-caption">
                            <h3>Chania</h3>
                            <p>The atmosphere in Chania has a touch of Florence and Venice.</p>
                        </div>
                    </div>

                    <div class="item">
                        <img src="img_chania2.jpg" alt="Chania" width="460" height="345">
                        <div class="carousel-caption">
                            <h3>Chania</h3>
                            <p>The atmosphere in Chania has a touch of Florence and Venice.</p>
                        </div>
                    </div>

                    <div class="item">
                        <img src="img_flower.jpg" alt="Flower" width="460" height="345">
                        <div class="carousel-caption">
                            <h3>Flowers</h3>
                            <p>Beautiful flowers in Kolymbari, Crete.</p>
                        </div>
                    </div>

                    <div class="item">
                        <img src="img_flower2.jpg" alt="Flower" width="460" height="345">
                        <div class="carousel-caption">
                            <h3>Flowers</h3>
                            <p>Beautiful flowers in Kolymbari, Crete.</p>
                        </div>
                    </div>

                </div>

                <!-- Left and right controls -->
                <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>
    </body>
</html>
查看更多
登录 后发表回答