jQuery實(shí)現(xiàn)的超簡(jiǎn)單輪播圖功能示例【代碼解釋】
先看效果
鼠標(biāo)懸浮時(shí)停止輪播,離開(kāi)時(shí)自動(dòng)輪播,點(diǎn)下一張小圓點(diǎn)會(huì)隨著動(dòng)

直接上代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery輪播圖</title>
<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
<style>
.wrapper {
width: 600px;
height: 350px;
border: 1px solid red;
position: relative;
}
/* 5張圖片疊加到一塊 */
.wrapper img {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
display: none;
}
.wrapper img:nth-of-type(1) {
display: block;
}
/* 小圓點(diǎn) */
.btn {
width: 150px;
display: flex;
justify-content: space-around;
position: absolute;
left: 225px;
bottom: 10px;
z-index: 100
}
.btn span {
display: block;
width: 15px;
height: 15px;
border: 3px solid white;
border-radius: 50%;
}
/* 左右箭頭 */
.wrapper a {
text-decoration: none;
font-size: 50px;
color: red;
position: absolute;
top: 35%;
}
.wrapper a:nth-of-type(1) {
left: 10px;
}
.wrapper a:nth-of-type(2) {
right: 10px;
}
.active {
background-color: red;
}
</style>
</head>
<body>
<div>
<div>
<img src="./imgs/1.png" alt="">
<img src="./imgs/2.png" alt="">
<img src="./imgs/3.png" alt="">
<img src="./imgs/4.png" alt="">
<img src="./imgs/5.png" alt="">
</div>
<div>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" ><</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" >">></a>
</div>
<script src="js/jquery-3.6.0.js"></script>
<script>
var index=0;
// 點(diǎn)擊上一張
$("a:first").click(function(){
prev_pic();
})
// 點(diǎn)擊下一張
$("a:last").click(function(){
next_pic();
})
// 懸浮停止
$(".wrapper").mouseover(function(){
clearInterval(id);
});
$(".wrapper").mouseout(function(){
autoplay();
})
// 下一張
function next_pic(){
index++;
if(index>4){
index=0;
}
addStyle();
}
// 上一張
function prev_pic(){
index--;
if(index<0){
index=4;
}
addStyle();
}
// 控制圖片顯示隱藏,小圓點(diǎn)背景色
function addStyle(){
$("img").eq(index).fadeIn();
$("img").eq(index).siblings().fadeOut();
$("span").eq(index).addClass("active");
$("span").eq(index).siblings().removeClass("active");
}
// 自動(dòng)輪播
var id;
autoplay();
function autoplay(){
id=setInterval(function(){
next_pic();
},1000)
}
</script>
</body>
</html>這里只要把圖片地址更換一下 ,引用jquery文件就可以了
另外,很多UI前端框架也有現(xiàn)成的輪播圖效果代碼,如:bootstrap、layui、amazeui等等。感興趣的朋友可以到對(duì)應(yīng)的官網(wǎng)查看示例代碼。
相關(guān)文章
jquery操作select option 的代碼小結(jié)
jquery操作select option 的代碼小結(jié),需要的朋友可以參考下。2011-06-06
學(xué)習(xí)從實(shí)踐開(kāi)始之jQuery插件開(kāi)發(fā) 對(duì)話框插件開(kāi)發(fā)
之所以寫(xiě)下這篇文章,是想將我的想法分享給大家;對(duì)于初學(xué)者,我希望他能從這篇文章中獲取對(duì)他有用的東西,對(duì)于經(jīng)驗(yàn)豐富的開(kāi)發(fā)者,我希望他能指出我的不足,給我更多的意見(jiàn)和建議;目的就是共同進(jìn)步2012-04-04
flag和jq on 的綁定多個(gè)對(duì)象和方法(必看)
下面小編就為大家?guī)?lái)一篇flag和jq on 的綁定多個(gè)對(duì)象和方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
IE下支持文本框和密碼框placeholder效果的JQuery插件分享
這篇文章主要介紹了IE下支持文本框和密碼框placeholder效果的JQuery插件分享,本文給出插件源碼和使用示例,需要的朋友可以參考下2015-01-01
jQuery效果 slideToggle() 方法(在隱藏和顯示之間切換)
slideToggle() 方法通過(guò)使用滑動(dòng)效果(高度變化)來(lái)切換元素的可見(jiàn)狀態(tài)。2011-06-06
使用Ajax和Jquery配合數(shù)據(jù)庫(kù)實(shí)現(xiàn)下拉框的二級(jí)聯(lián)動(dòng)的示例
下面小編就為大家分享一篇使用Ajax和Jquery配合數(shù)據(jù)庫(kù)實(shí)現(xiàn)下拉框的二級(jí)聯(lián)動(dòng)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

