js實現圖片自動輪播
阿新 • • 發佈:2018-12-24
今天有人問我這個問題,我就順便把這個記下來,分享給大夥。
如圖,就是圖片自己輪播,並且圖中中下方的白點也發生變化,圖片到哪,白點就到哪,就直接上程式碼了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <!--輪播--> <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> </head> <body> <!--輪播--> <div id="myCarousel" class="carousel slide" style="width:100%;"> <!-- 輪播(Carousel)指標 --> <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> <li data-target="#myCarousel" data-slide-to="4"></li> </ol> <!-- 輪播(Carousel)專案 --> <div class="carousel-inner"> <div id="pic0" class="item active"> <img src="1.jpg" alt="First slide"> </div> <div id="pic1" class="item"> <img src="2.jpg" alt="Second slide"> </div> <div id="pic2" class="item"> <img src="3.jpg" alt="Third slide"> </div> <div id="pic3" class="item"> <img src="4.jpg" alt="fourth slide"> </div> <div id="pic4" class="item"> <img src="f.jpg" alt="fifth slide"> </div> </div> </div> <script> var i=0; var c = null; c = setTimeout(carousel,1000);//開始執行 function carousel() { clearTimeout(c);//清除定時器 $("#pic"+i).removeClass("active"); $("#pic"+(i+1)).addClass("active"); i++; $("ol li").removeClass("active"); $("ol li:eq("+i+")").addClass("active"); if(i>4){ $("#pic"+(i-1)).removeClass("active"); $("#pic0").addClass("active"); i=0; $("ol li:eq("+i+")").addClass("active"); } c = setTimeout(carousel,1000); //設定定時器,迴圈執行 } </script> </body> </html>
希望對大家有用