使用JS實(shí)現(xiàn)圖片輪播的實(shí)例(前后首尾相接)
最近各種跑面試,終于還是被問(wèn)到這個(gè),一腦子漿糊,當(dāng)時(shí)沒(méi)想出來(lái)首尾相接怎么搞,回來(lái)之后研究了一波,終于搞出來(lái)了,不多說(shuō),直接看代碼
代碼參考了一位已經(jīng)寫(xiě)好了圖片輪播功能的(再次表示感謝),但是沒(méi)有首尾相接的功能,本人在此基礎(chǔ)上增加了首尾相接功能。
效果如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>圖片輪播</title>
<style type="text/css">
body,div,ul,li,a,img{margin: 0;padding: 0;}
ul,li{list-style: none;}
a{text-decoration: none;}
#wrapper{
position: relative;
margin: 30px auto; /* 上下邊距30px,水平居中 */
width: 400px;
height: 200px;
}
#banner{
position:relative;
width: 400px;
height: 200px;
overflow: hidden;
}
.imgList{
position:relative;
width:2000px;
height:200px;
z-index: 10;
overflow: hidden;
}
.imgList li{float:left;display: inline;}
#prev,
#next{
position: absolute;
top:80px;
z-index: 20;
cursor: pointer;
opacity: 0.2;
filter:alpha(opacity=20);
}
#prev{left: 10px;}
#next{right: 10px;}
#prev:hover,
#next:hover{opacity: 0.5;filter:alpha(opacity=50);}
</style>
</head>
<body>
<div id="wrapper"><!-- 最外層部分 -->
<div id="banner"><!-- 輪播部分 -->
<ul class="imgList"><!-- 圖片部分 -->
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/1.jpg" width="400px" height="200px" alt="1"></a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/2.jpg" width="400px" height="200px" alt="2"></a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/3.jpg" width="400px" height="200px" alt="3"></a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/4.jpg" width="400px" height="200px" alt="4"></a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/5.jpg" width="400px" height="200px" alt="5"></a></li>
</ul>
<img src="./img/prev.png" width="40px" height="40px" id="prev">
<img src="./img/next.png" width="40px" height="40px" id="next">
</div>
</div>
<script type="text/javascript" src="./js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var curIndex = 0, //當(dāng)前index
imgLen = $(".imgList li").length; //圖片總數(shù)
$(".imgList").css("width", (imgLen+1)*400+"px");
// 定時(shí)器自動(dòng)變換3秒每次
var autoChange = setInterval(function(){
if(curIndex < imgLen-1){
curIndex ++;
}else{
curIndex = 0;
}
//調(diào)用變換處理函數(shù)
changeTo(curIndex);
},3000);
//左箭頭滑入滑出事件處理
$("#prev").hover(function(){
//滑入清除定時(shí)器
clearInterval(autoChange);
}, function(){
//滑出則重置定時(shí)器
autoChangeAgain();
});
//左箭頭點(diǎn)擊處理
$("#prev").click(function(){
//根據(jù)curIndex進(jìn)行上一個(gè)圖片處理
// curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);
if (curIndex == 0) {
var element = document.createElement("li");
element.innerHTML = $(".imgList li")[imgLen - 1].innerHTML;
// $(".imgList li")[imgLen - 1].remove();
$(".imgList").prepend(element);
$(".imgList").css("left", -1*400+"px");
changeTo(curIndex);
curIndex = -1;
} else if (curIndex == -1) {
$(".imgList").css("left", -(imgLen-1)*400+"px");
curIndex = imgLen-2;
$(".imgList li")[0].remove();
changeTo(curIndex);
} else {
--curIndex;
changeTo(curIndex);
}
});
//右箭頭滑入滑出事件處理
$("#next").hover(function(){
//滑入清除定時(shí)器
clearInterval(autoChange);
}, function(){
//滑出則重置定時(shí)器
autoChangeAgain();
});
//右箭頭點(diǎn)擊處理
$("#next").click(function(){
// curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;
console.log(imgLen);
if (curIndex == imgLen-1) {
var element = document.createElement("li");
element.innerHTML = $(".imgList li")[0].innerHTML;
// $(".imgList li")[0].remove();
$(".imgList").append(element);
++curIndex;
} else if (curIndex == imgLen) {
curIndex = 0;
$(".imgList").css("left", "0px");
$(".imgList li")[imgLen].remove();
curIndex++;
} else {
++curIndex;
}
changeTo(curIndex);
});
//清除定時(shí)器時(shí)候的重置定時(shí)器--封裝
function autoChangeAgain(){
autoChange = setInterval(function(){
if(curIndex < imgLen-1){
curIndex ++;
}else{
curIndex = 0;
}
//調(diào)用變換處理函數(shù)
changeTo(curIndex);
},3000);
}
function changeTo(num){
var goLeft = num * 400;
$(".imgList").animate({left: "-" + goLeft + "px"},500);
}
</script>
</body>
</html>
以上這篇使用JS實(shí)現(xiàn)圖片輪播的實(shí)例(前后首尾相接)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript控制輸入框中只能輸入中文、數(shù)字和英文的方法【基于正則實(shí)現(xiàn)】
這篇文章主要介紹了JavaScript控制輸入框中只能輸入中文、數(shù)字和英文的方法,基于正則驗(yàn)證實(shí)現(xiàn)字符輸入限制功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
詳解JavaScript中Math內(nèi)置對(duì)象基本方法的使用
Math?是javaScript的內(nèi)置對(duì)象,包含了部分?jǐn)?shù)學(xué)常數(shù)屬性和數(shù)學(xué)函數(shù)方法。本文將詳細(xì)講解Math基本方法的使用,感興趣的小伙伴可以學(xué)習(xí)一下2022-04-04
js實(shí)現(xiàn)省市級(jí)聯(lián)效果分享
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)省市級(jí)聯(lián)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
JavaScript判斷兩個(gè)對(duì)象是否相等的方法總結(jié)
判斷兩個(gè)對(duì)象是否相等是js中的一個(gè)很常見(jiàn)的內(nèi)容,不同的編程語(yǔ)言和環(huán)境可能會(huì)有不同的方式來(lái)實(shí)現(xiàn)這一目標(biāo),在 JavaScript 中,判斷兩個(gè)對(duì)象是否相等主要有以下幾種方法,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-08-08
JS實(shí)現(xiàn)網(wǎng)頁(yè)端猜數(shù)字小游戲
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)網(wǎng)頁(yè)端猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03

