輪播圖的四種方法


1.swiper插件實現輪播圖

swiper是一個實現輪播圖很強大,上手也容易。並且也是現在app,網址等用的最多的,
官方網址:http://www.swiper.com.cn/
下載插件導入,按照教程即可實現精美效果

代碼部分

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" type="text/css" href="swiper-3.4.1.min.css"> <script type="text/javascript" src="jquery/jquery-3.0.0.min.js"></script> <script type="text/javascript" src="swiper-3.4.1.jquery.min.js"></script> <style type="text/css"> .swiper-container{ width: 790px; height: 340px; } </style> </head> <!-- 結構以及class的類名不允許更改 --> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide">![]((1).jpg)</div> <div class="swiper-slide">![]((2).jpg)</div> <div class="swiper-slide">![]((3).jpg)</div> <div class="swiper-slide">![]((4).jpg)</div> <div class="swiper-slide">![]((5).jpg)</div> <div class="swiper-slide">![]((6).jpg)</div> <div class="swiper-slide">![]((7).jpg)</div> <div class="swiper-slide">![]((8).jpg)</div> </div> <!-- 如果需要分頁器 --> <div class="swiper-pagination"></div> <!-- 如果需要導航按鈕 --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> <!-- 如果需要滾動條 --> <!-- <div class="swiper-scrollbar"></div> --> </div> <script type="text/javascript"> var mySwiper = new Swiper ('.swiper-container', { // 滾動方向 horizontal/vertical direction: 'horizontal', // 自動播放 autoplay:2000, // 是否循環播放 loop: true, // 如果需要分頁器(小圓點) // 是否需要分頁器 pagination: '.swiper-pagination', // 點擊分頁器是否切換到對應的圖片 是 true 否 false paginationClickable:true, // 如果需要前進后退按鈕 nextButton: '.swiper-button-next', prevButton: '.swiper-button-prev', // 用戶操作swiper之后,是否禁止autoplay。默認為true:停止。 // 如果設置為false,用戶操作swiper之后自動切換不會停止,每次都會重新啟動autoplay。 // 操作包括觸碰,拖動,點擊pagination等。 autoplayDisableOnInteraction:false, }) </script> </body> </body> </html>

2.JS實現輪播圖

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>輪播圖</title> <style> #loopDiv{ width: 790px; height: 340px; margin: 0 auto; position: relative; } #list{ list-style: none; position: absolute; bottom: 10px; left: 250px; } #list li{ float: left; width: 20px; height: 20px; line-height: 20px; text-align: center; border-radius: 50%; background: #aaa; margin-right: 10px; } .chooseBut{ width: 50px; height: 80px; background-color: rgba(0,0,0,0.2); color: #fff; font-size: 30px; line-height: 80px; text-align: center; display: none; } #left{ position: absolute; left: 0px; top: 130px; } #right{ position: absolute; right: 0px; top: 130px; } </style> </head> <body> <div id="loopDiv"> ![](img/0.jpg) <ul id="list"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> </ul> <div id="left" class="chooseBut"><</div> <div id="right" class="chooseBut">></div> </div> <script type="text/javascript"> var jsDivBox = document.getElementById("loopDiv"); //圖片節點 var jsImg = document.getElementById("pic"); //左右按鈕節點 var jsLeft = document.getElementById("left"); var jsRight = document.getElementById("right"); //獲取所有的li var jsUl = document.getElementById("list"); var jsLis = jsUl.getElementsByTagName("li"); //讓第一個小圓點變為紅色 jsLis[0].style.backgroundColor = "red"; //顯示當前的圖片下標 var currentPage = 0; //啟動定時器 var timer = setInterval(func, 1000); function func() { currentPage++; changePic(); } function changePic() { if (currentPage == 8) { currentPage = 0; } if (currentPage == -1) { currentPage = 7; } //更換圖片 //"img/1.jpg" jsImg.src = "img/" + currentPage + ".jpg"; //將所有的小圓點顏色清空 for (var i = 0; i < jsLis.length; i++) { jsLis[i].style.backgroundColor = "#aaa"; } //改變對應小圓點為紅色 jsLis[currentPage].style.backgroundColor = "red"; } //鼠標進入 jsDivBox.addEventListener("mouseover", func1, false); function func1() { //停止定時器 clearInterval(timer); //顯示左右按鈕 jsLeft.style.display = "block"; jsRight.style.display = "block"; } //鼠標移出 jsDivBox.addEventListener("mouseout", func2, false); function func2() { //重啟定時器 timer = setInterval(func, 1000); //隱藏左右按鈕 jsLeft.style.display = "none"; jsRight.style.display = "none"; } //點擊左右按鈕 jsLeft.addEventListener("click", func3, false); function func3() { currentPage--; changePic(); } jsLeft.onmouseover = function() { this.style.backgroundColor = "rgba(0,0,0,0.6)"; } jsLeft.onmouseout = function() { this.style.backgroundColor = "rgba(0,0,0,0.2)"; } jsRight.addEventListener("click", func4, false); function func4() { currentPage++; changePic(); } jsRight.onmouseover = function() { this.style.backgroundColor = "rgba(0,0,0,0.6)"; } jsRight.onmouseout = function() { this.style.backgroundColor = "rgba(0,0,0,0.2)"; } //進入小圓點 for (var j = 0; j < jsLis.length; j++) { jsLis[j].onmouseover = function() { currentPage = parseInt(this.innerHTML) - 1; changePic(); }; } </script> </body> </html>


3.jQuery實現輪播圖

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .pic{ width: 790px; height: 340px; margin: 10px auto; position: relative; overflow: hidden; } .content{ width: 99999px; height: 340px; position: absolute; left: 0px; top: 0px; } .content img{ float: left; } .index{ position: absolute; left: 300px; bottom: 5px; width: 200px; height: 20px; list-style: none; } .index li{ width: 10px; height: 10px; border-radius: 50%; float: left; margin-left: 10px; background-color: rgba(100,100,100,0.3); } .left{ width: 30px; height:50px; background-color:rgba(100,100,100,0.5); position: absolute; left: 0px; top: 150px; line-height: 50px; text-align: center; font-size: 20px; color: #fff; display: none; } .right{ width: 30px; height:50px; background-color:rgba(100,100,100,0.5); position: absolute; right: 0px; top: 150px; line-height: 50px; text-align: center; font-size: 20px; color: #fff; display: none; } .index .first{ background-color: red; } </style> <script type="text/javascript" src="jquery/jquery-3.0.0.min.js"></script> </head> <body> <div class="pic"> <div class="content"> ![](img/(1).jpg) ![](img/(2).jpg) ![](img/(3).jpg) ![](img/(4).jpg) ![](img/(5).jpg) ![](img/(6).jpg) ![](img/(7).jpg) ![](img/(8).jpg) </div> <ul class="index"> <li class="first"></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> <div class="right">></div> <div class="left"><</div> </div> <script type="text/javascript"> $(function(){ //每個固定的時間移動圖片 var timer = setInterval(picLoop,1000); var index = 0; function picLoop(){ index++; if (index==8) {index=0;} $(".content").animate({"left":-790*index},300); $("li").eq(index).css("background-color","red") .siblings().css("background-color","rgba(100,100,100,0.3)"); } //定時器的控制 $(".pic").hover(function(){ clearInterval(timer); $(".left").show(); $(".right").show(); },function(){ timer = setInterval(picLoop,1000); $(".left").hide(); $(".right").hide(); }) $("li").mouseover(function(){ $(this).css("background-color","red") .siblings().css("background-color","rgba(100,100,100,0.3)"); index = $(this).index(); $(".content").animate({"left":-790*index},300); }) $(".left").click(function(){ index--; if (index==-1) {index=7;} $(".content").animate({"left":-790*index},300); $("li").eq(index).css("background-color","red") .siblings().css("background-color","rgba(100,100,100,0.3)"); }) $(".right").click(function(){ index++; if (index==8) {index=0} $(".content").animate({"left":-790*index},300); $("li").eq(index).css("background-color","red") .siblings().css("background-color","rgba(100,100,100,0.3)"); }) }) </script> </body> </html>


4.css3實現輪播圖

css3的輪播實用性差,但是也可以使用,但是可以鍛煉我們的思維。

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> * { margin:0; padding:0; } ul { list-style:none; } .loop{ position: relative; margin:30px auto; width:600px; height:300px; } #wrap { position:relative; width:600px; height:300px; border:1px solid #9cc5ef; overflow:hidden; } #slider { width:300%; height:100%; font:100px/400px Microsoft Yahei; text-align:center; color:#fff; margin-left:0; -webkit-animation:slide1 3s ease-out infinite; } #slider li { float:left; width:600px; height:100%; } #slider img{ width:600px; height:100%; } /*創建三種動畫策略*/ @-webkit-keyframes slide1 { 0% { margin-left:0;} 23% { margin-left:0;} 33% { margin-left:-600px;} 56% { margin-left:-600px;} 66% { margin-left:-1200px;} 90% { margin-left:-1200px;} 100% {margin-left:0;} } @-webkit-keyframes slide2 { 0% { margin-left:-600px;} 23% { margin-left:-600px;} 33% { margin-left:-1200px;} 56% { margin-left:-1200px;} 66% { margin-left:0;} 90% { margin-left:0;} 100% {margin-left:-600px;} } @-webkit-keyframes slide3 { 0% { margin-left:-1200px;} 23% { margin-left:-1200px;} 33% { margin-left:0;} 56% { margin-left:0;} 66% { margin-left:-600px;} 90% { margin-left:-600px;} 100% {margin-left:-1200px;} } /*當我們點擊對應按鈕時顯示對應的動畫*/ #first:checked ~ #wrap #slider { -webkit-animation-name:slide1; } #second:checked ~ #wrap #slider { -webkit-animation-name:slide2; } #third:checked ~ #wrap #slider { -webkit-animation-name:slide3; } /*短暫地取消動畫名稱,模擬重啟動畫*/ #first:active ~ #wrap #slider { -webkit-animation-name:none; margin-left:0; } #second:active ~ #wrap #slider { -webkit-animation-name:none; margin-left:-600px; } #third:active ~ #wrap #slider { -webkit-animation-name:none; margin-left:-1200px; } #opts { width:600px; height:40px; margin:auto; color:#fff; text-align:center; font:16px/30px Microsoft Yahei; position: absolute; top: 260px; left: 250px; } #opts label { float:left; width:30px; height:30px; margin-right:10px; background:#cccccc; cursor:pointer; border-radius: 50%; } #opts label:hover { background:#405871; } /* 隱藏Input按鈕*/ #first, #second, #third { display:none; } </style> </head> <body> <div class="loop"> <input type="radio" name="slider" id="first"> <input type="radio" name="slider" id="second"> <input type="radio" name="slider" id="third"> <div id="wrap"> <ul id="slider"> <li>![](img/0.jpg)</li> <li>![](img/1.jpg)</li> <li>![](img/2.jpg)</li> </ul> </div> <div id="opts"> <label for="first">1</label> <label for="second">2</label> <label for="third">3</label> </div> </div> </body> </html>


用jquery寫的無縫左右滑動輪播圖

這里的圓點的html代碼是通過js實現的,全jquery實現無縫輪播
1、html代碼
<div class="banner">
<ul class="imgs">
<li><a href="#"><img src="/img/pic1.jpg" alt="第1張圖片"></a></li>
<li><a href="#"><img src="/img/pic2.jpg" alt="第2張圖片"></a></li>
<li><a href="#"><img src="/img/pic3.jpg" alt="第3張圖片"></a></li>
<li><a href="#"><img src="/img/pic4.jpg" alt="第4張圖片"></a></li>
<li><a href="#"><img src="/img/pic5.jpg" alt="第5張圖片"></a></li>
</ul>
<!-- 小圓點 -->
<ul class="nums"></ul>
<!-- <ul class="des">
<li>第一個</li>
<li>第二個</li>
<li>第三個</li>
<li>第四個</li>
<li>第五個</li>
<li>第一個</li>
</ul> -->
<div class="btns">
<span class="prev"><</span>
<span class="next">></span>
</div>
</div>

2、css代碼
div.banner{
display:block;
position: relative;
margin:0 auto;
width:1200px;
height:535px;
margin-top:30px;
overflow: hidden;
z-index: 1;
}
.imgs{
position: absolute;
top: 0;
left: 0;
}
.des{
position: absolute;
bottom: 0;
left: 0;
z-index: -2;
background: #ccc;
}
.des li{
float: left;
width: 1200px;
}
.imgs li{
float: left;
}
.nums{
position: absolute;
bottom: 10px;width: 100%;
text-align: center;
font-size: 0;
}
.nums li{
width: 10px;
height: 10px;
background:rgba(0,0,0,0.5);
display: block;
border-radius: 100%;
display: inline-block;
margin: 0 5px;
cursor: pointer;
}
.btns{
display: none;
}
.btns span{
display: block;
width: 50px;
height: 100px;
background: rgba(0,0,0,0.6);
color: #fff;
font-size: 40px;
line-height: 100px;
text-align: center;
cursor:pointer;
}
.btns .prev{
position: absolute;
left: 0;
top: 50%;
margin-top: -50px;
}
.btns .next{
position: absolute;
right: 0;
top: 50%;
margin-top: -50px;
}
.nums .active{
background-color: #fff;
}
.hide{
display: none;
}

3、全部jquery-js代碼

$(function(){
var i=0;
var timer=null;
for (var j = 0; j < $('.imgs li').length; j++) { //創建圓點
$('.nums').append('<li></li>')
}
$('.nums li').first().addClass('active'); //給第一個圓點添加樣式
var firstimg=$('.imgs li').first().clone(); //復制第一張圖片
$('.imgs').append(firstimg).width($('.imgs li').length*($('.imgs img').width()));
//第一張圖片放到最后一張圖片后,設置ul的寬度為圖片張數*圖片寬度
$('.des').width($('.imgs li').length*($('.imgs img').width()));
// 下一個按鈕
$('.next').click(function(){
i++;
if (i==$('.imgs li').length) {
i=1; //這里不是i=0
$('.imgs').css({left:0}); //保證無縫輪播,設置left值
};
$('.imgs').stop().animate({left:-i*1200},300);
if (i==$('.imgs li').length-1) { //設置小圓點指示
$('.nums li').eq(0).addClass('active').siblings().removeClass('active');
$('.des li').eq(0).removeClass('hide').siblings().addClass('hide');
}else{
$('.nums li').eq(i).addClass('active').siblings().removeClass('active');
$('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
}
})
// 上一個按鈕
$('.prev').click(function(){
i--;
if (i==-1) {
i=$('.imgs li').length-2;
$('.imgs').css({left:-($('.imgs li').length-1)*1200});
}
$('.imgs').stop().animate({left:-i*1200},300);
$('.nums li').eq(i).addClass('active').siblings().removeClass('active');
$('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
})
//設置按鈕的顯示和隱藏
$('.banner').hover(function(){
$('.btns').show();
},function(){
$('.btns').hide();
})
//鼠標划入圓點
$('.nums li').mouseover(function(){
var _index=$(this).index();
$('.imgs').stop().animate({left:-_index*1200},300);
$('.nums li').eq(_index).addClass('active').siblings().removeClass('active');
$('.des li').eq(_index).removeClass('hide').siblings().addClass('hide');
})
//定時器自動播放
timer=setInterval(function(){
i++;
if (i==$('.imgs li').length) {
i=1;
$('.imgs').css({left:0});
};
$('.imgs').stop().animate({left:-i*1200},300);
if (i==$('.img li').length-1) {
$('.nums li').eq(0).addClass('active').siblings().removeClass('active');
$('.des li').eq(0).removeClass('hide').siblings().addClass('hide');
}else{
$('.nums li').eq(i).addClass('active').siblings().removeClass('active');
$('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
}
},3000)
//鼠標移入,暫停自動播放,移出,開始自動播放
$('.banner').hover(function(){
clearInterval(timer);
},function(){
timer=setInterval(function(){
i++;
if (i==$('.imgs li').length) {
i=1;
$('.imgs').css({left:0});
};
$('.imgs').stop().animate({left:-i*1200},300);
if (i==$('.img li').length-1) {
$('.nums li').eq(0).addClass('active').siblings().removeClass('active');
$('.des li').eq(0).removeClass('hide').siblings().addClass('hide');
}else{
$('.nums li').eq(i).addClass('active').siblings().removeClass('active');
$('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
}
},3000)
})
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM