使用jQuery自动更改幻灯片中的图片

时间:2018-08-22 14:41:41

标签: javascript jquery html css

我使用带有jQuery UI的工作图片滑块。我可以使用滑块更改图片。现在,我想在一段时间(例如3秒)内自动更改图片。

$(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 个答案:

答案 0 :(得分:0)

您可以使用here中的任何用于Carousel的JQuery插件,也可以执行以下操作

<!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>