jQuery實(shí)現(xiàn)輪播圖及其原理詳解
本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)輪播圖及其原理的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>JQuery輪播圖</title>
<style>
*{
padding:0;
margin:0;
}
.container{
width:600px;
height:400px;
overflow: hidden;
position:relative;
margin:0 auto;
}
.list{
width:3000px;
height:400px;
position:absolute;
}
.list>img{
float:left;
width:600px;
height:400px;
}
.pointer{
position:absolute;
width:100px;
bottom:20px;
left:250px;
}
.pointer>span{
cursor:pointer;
display:inline-block;
width:10px;
height:10px;
background: #7b7d80;
border-radius:50%;
border:1px solid #fff;
}
.pointer .on{
background: #28a4c9;
}
.arrow{
position:absolute;
text-decoration:none;
width:40px;
height:40px;
background: #727d8f;
color:#fff;
font-weight: bold;
line-height:40px;
text-align:center;
top:180px;
display:none;
}
.arrow:hover{
background: #0f0f0f;
}
.left{
left:0;
}
.right{
right:0;
}
.container:hover .arrow{
display:block;
}
</style>
</head>
<body>
<div class="container">
<div class="list" style="left:0px;">
<!--<img src="../static/image/photo1.jpg" alt="5"/>-->
<img src="../static/image/banner.jpg" alt="1"/>
<img src="../static/image/slide1.jpg" alt="2"/>
<img src="../static/image/slide1.jpg" alt="3"/>
<img src="../static/image/slide1.jpg" alt="4"/>
<img src="../static/image/photo1.jpg" alt="5"/>
<!--<img src="../static/image/banner.jpg" alt="1"/>-->
</div>
<div class="pointer">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="#" rel="external nofollow" rel="external nofollow" class="arrow left">></a>
<a href="#" rel="external nofollow" rel="external nofollow" class="arrow right"><</a>
</div>
<script src="../static/js/jquery-3.2.1.min.js"></script>
<script>
var imgCount = 5;
var index = 1;
var intervalId;
var buttonSpan = $('.pointer')[0].children;//htmlCollection 集合
//自動(dòng)輪播功能 使用定時(shí)器
autoNextPage();
function autoNextPage(){
intervalId = setInterval(function(){
nextPage(true);
},3000);
}
//當(dāng)鼠標(biāo)移入 停止輪播
$('.container').mouseover(function(){
console.log('hah');
clearInterval(intervalId);
});
// 當(dāng)鼠標(biāo)移出,開始輪播
$('.container').mouseout(function(){
autoNextPage();
});
//點(diǎn)擊下一頁 上一頁的功能
$('.left').click(function(){
nextPage(true);
});
$('.right').click(function(){
nextPage(false);
});
//小圓點(diǎn)的相應(yīng)功能 事件委托
clickButtons();
function clickButtons(){
var length = buttonSpan.length;
for(var i=0;i<length;i++){
buttonSpan[i].onclick = function(){
$(buttonSpan[index-1]).removeClass('on');
if($(this).attr('index')==1){
index = 5;
}else{
index = $(this).attr('index')-1;
}
nextPage(true);
};
}
}
function nextPage(next){
var targetLeft = 0;
//當(dāng)前的圓點(diǎn)去掉on樣式
$(buttonSpan[index-1]).removeClass('on');
if(next){//往后走
if(index == 5){//到最后一張,直接跳到第一張
targetLeft = 0;
index = 1;
}else{
index++;
targetLeft = -600*(index-1);
}
}else{//往前走
if(index == 1){//在第一張,直接跳到第五張
index = 5;
targetLeft = -600*(imgCount-1);
}else{
index--;
targetLeft = -600*(index-1);
}
}
$('.list').animate({left:targetLeft+'px'});
//更新后的圓點(diǎn)加上樣式
$(buttonSpan[index-1]).addClass('on');
}
</script>
</body>
</html>
效果圖:

原理:
頁面結(jié)構(gòu)方面:
將輪播圖容器設(shè)置成相對(duì)定位,寬度設(shè)置成圖片的寬度;容器中分為四部分:輪播的圖片;點(diǎn)擊的小按鈕;前一張;后一張
樣式方面:
- 輪播圖容器為相對(duì)定位,寬度/高度設(shè)置成圖片的寬度/高度,設(shè)置overflow為hidden;
- 容器中的每一部分都設(shè)置成絕對(duì)定位,放到相應(yīng)的位置;
- 輪播圖片的容器寬度設(shè)置成所有圖片的寬度和,left開始先設(shè)置為0;
- 每張圖片寬度一樣,都設(shè)成左浮動(dòng),左右圖片都在一行排列,所以輪播圖的實(shí)質(zhì)是裝有圖片的容器的移動(dòng),每次移動(dòng)的距離為一張圖片的寬度,這樣每次就只顯示一張圖片;
- 前一張/后一張?jiān)O(shè)置成display為none,當(dāng)鼠標(biāo)移入總?cè)萜鲿r(shí),再設(shè)置成display為block
功能方面:
自動(dòng)輪播為一個(gè)定時(shí)循環(huán)機(jī)制setInteval,鼠標(biāo)移入,循環(huán)停止,移除循環(huán)繼續(xù);
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jquery 實(shí)現(xiàn)輪播圖詳解及實(shí)例代碼
- 利用jquery寫的左右輪播圖特效
- jquery實(shí)現(xiàn)左右無縫輪播圖
- jQuery插件slides實(shí)現(xiàn)無縫輪播圖特效
- jquery實(shí)現(xiàn)左右滑動(dòng)式輪播圖
- jQuery自適應(yīng)輪播圖插件Swiper用法示例
- JQuery和html+css實(shí)現(xiàn)帶小圓點(diǎn)和左右按鈕的輪播圖實(shí)例
- jquery實(shí)現(xiàn)輪播圖特效
- jquery實(shí)現(xiàn)的簡(jiǎn)單輪播圖功能【適合新手】
- jquery實(shí)現(xiàn)無縫輪播圖
相關(guān)文章
Java框架SSH結(jié)合Easyui控件實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)示例解析
這篇文章主要為大家詳細(xì)介紹了Java框架SSH結(jié)合Easyui控件實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
jquery-easyui關(guān)閉tab自動(dòng)切換到前一個(gè)tab
考慮到用戶體驗(yàn),當(dāng)用戶關(guān)閉tab時(shí),自動(dòng)切換到前一個(gè)tab不至于讓用戶看到空的界面。2010-07-07
jquery數(shù)組之存放checkbox全選值示例代碼
使用jquery數(shù)組可以存放checkbox全選值,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-12-12
利用jquery正則表達(dá)式在頁面驗(yàn)證url網(wǎng)址輸入是否正確
這篇文章主要介紹了關(guān)于利用jquery正則表達(dá)式在頁面驗(yàn)證url網(wǎng)址輸入是否正確的相關(guān)資料,文中給出了完整的示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04
手寫簡(jiǎn)單的jQuery雪花飄落效果實(shí)例
這篇文章主要給大家介紹了關(guān)于手寫簡(jiǎn)單的jQuery雪花飄落的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
Jquery UI震動(dòng)效果實(shí)現(xiàn)原理及步驟
如果你想你的博客頁面某些部分引起讀者的注意,你可以使這些部分震動(dòng),如廣告等等,今天這篇文章將介紹怎樣使你的頁面中的元素震動(dòng)起來,感興趣的你可不要錯(cuò)過了哦,或許對(duì)你學(xué)習(xí)jquery ui 有所幫助2013-02-02

