利用BootStrap的Carousel.js實現輪播圖動畫效果_javascript技巧

來源:互聯網
上載者:User

前期準備:

1.jquery.js。

2.bootstrap的carousel.js。

3.bootstrap.css。

一起來看代碼吧:

頁面比較醜,希望大家不要介意哦嘻嘻

效果圖:

html+js:

<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>my love-首頁</title> <link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/index.css"> <link href="favicon.ico" rel="shortcut icon" /> <script src="js/jquery.js" type="text/javascript"></script> <script src="js/carousel.js" type="text/javascript"></script></head><body> <div class="index-content">  <div class="index-header">   <nav class="navbar navbar-default">    <div class="container-fluid">     <div class="navbar-header">      <a class="navbar-brand" href="javascript:void(0)">首頁</a>     </div>     <div>      <ul class="nav navbar-nav">       <li class="active"><a href="#section1" class="con">向日葵花</a></li>       <li><a href="#section2" class="con">萌萌噠的狗狗</a></li>       <li><a href="#section3" class="con">好吃噠</a></li>      </ul>     </div>    </div>   </nav>  </div>  <div class="index-body">   <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">    <!-- Indicators -->    <ol class="carousel-indicators">     <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>     <li data-target="#carousel-example-generic" data-slide-to="1"></li>     <li data-target="#carousel-example-generic" data-slide-to="2"></li>    </ol>    <!-- Wrapper for slides -->    <div class="carousel-inner" role="listbox">     <div class="item active">      <img src="image/indexOne.jpg" alt="向日葵" class="images">     </div>     <div class="item">      <img src="image/indexTwo.jpg" alt="萌萌噠狗狗" class="images">     </div>     <div class="item">      <img src="image/indexThree.jpg" alt="好吃噠" class="images">     </div>    </div>    <!-- Controls -->    <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">     <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>     <span class="sr-only">Next</span>    </a>   </div>  </div>  <div class="index-footer">   <div class="footer-centent">    Copyright ©2016 愛喝優酪乳的吃貨   </div>  </div> </div> <script type="text/javascript">  $(function() {   $(".navbar-nav li").each(function(index) {    $(this).click(function() {     $("li.active").removeClass("active"); //注意這裡     $(this).addClass("active"); //注意這裡     $(".carousel-inner div.active").removeClass("active");     $(".carousel-inner div").eq(index).addClass("active");    });   });   window.setInterval(function() {    $(".carousel-inner div").each(function(index) {     if ($(this).hasClass("active")) {      $(".navbar-nav li.active").removeClass("active");      $(".navbar-nav li").eq(index).addClass("active");     }    });   }, 100);  }); </script></body></html>

index.css

.container-fluid { padding-left: 200px;}.navbar-default { background-color: #308dca;}.navbar-brand { font-size: 34px; height: 70px;}.navbar-nav>li>a { font-size: 19px;}.navbar-default .navbar-brand, .navbar-default .navbar-nav>li>a, .navbar-default .navbar-nav>li>a:hover, .navbar-default .navbar-nav>li>a:focus { color: #fff;}.navbar-brand, .navbar-nav>li>a { line-height: 40px;}.navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:hover, .navbar-default .navbar-nav>.active>a:focus { color: #fff; background-color: #2276bf;}.index-body { margin: 0 auto;}.carousel-inner { height: 490px;}.index-body, .images { width: 900px;}.index-footer { margin-top: 20px; color: #fff; background-color: #2276bf;}.footer-centent { width: 300px; font-size: 20px; line-height: 55px; margin: 0 auto; height: 60px;}

大家在自己設計demo的時候注意下,class為index-body的div和img的寬度要設定一樣,否則會出現以下情況:

以上所述是小編給大家介紹的利用BootStrap的Carousel.js實現輪播圖動畫效果,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.