jquery寫(xiě)出PC端輪播圖實(shí)例
最近其他項(xiàng)目不是很忙,被安排給公司的官網(wǎng)項(xiàng)目做一個(gè)新的頁(yè)面(之前沒(méi)接觸公司官網(wǎng)項(xiàng)目),其中有一個(gè)用到輪播圖的地方,最開(kāi)始想直接用swiper.js插件實(shí)現(xiàn)就好了,可是發(fā)現(xiàn)官網(wǎng)項(xiàng)目里之前都沒(méi)有引入過(guò)swiper.js,后來(lái)想了想,就不引入它了,免得又得增加依次一次網(wǎng)絡(luò)請(qǐng)求,項(xiàng)目里既然已經(jīng)用到了jQuery,那就索性用jQuery寫(xiě)一個(gè)輪播圖吧。
現(xiàn)在把自己寫(xiě)的輪播圖這塊代碼單獨(dú)拿出來(lái),做一個(gè)小demo寫(xiě)在這里記錄一下(demo中輪播圖的圖片網(wǎng)上隨意找的)
實(shí)現(xiàn)的效果:
1、自動(dòng)輪播(輪播時(shí)間間隔在js代碼中自定義)
2、點(diǎn)擊左右側(cè)按鈕,實(shí)現(xiàn)手動(dòng)切換
3、底部小圓點(diǎn)根據(jù)切換圖片的位置相應(yīng)的顯示active狀態(tài)
4、鼠標(biāo)經(jīng)過(guò)輪播圖區(qū)域,停止輪播,離開(kāi)輪播圖區(qū)域開(kāi)始輪播
代碼目錄結(jié)果如下:

一、index.html
注:這里以5張圖片為例,頁(yè)面上真正輪播展示給用戶(hù)看到的是5張不同的圖片,但是為了輪播效果的連貫性,所以在第一張圖片前面添加上第五張圖片,在第五張圖片后面加上了第一張圖片,所以demo結(jié)構(gòu)里是7張圖片,每張圖片的尺寸必須都是一樣的哦(這里寬高尺寸是720*350px)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PC-jquery版輪播圖</title>
<link rel="stylesheet" href="css/style.css" rel="external nofollow" >
</head>
<body>
<div class="layout">
<h2 style="text-align: center;">PC-jquery版輪播圖</h2>
<div class="slide" id="slide">
<div id="outer" class="outer">
<ul id="inner" class="inner">
<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-5</p><img src="images/slide-5.jpg"></a></li>
<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-1</p><img src="images/slide-1.jpg"></a></li>
<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-2</p><img src="images/slide-2.jpg"></a></li>
<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-3</p><img src="images/slide-3.jpg"></a></li>
<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-4</p><img src="images/slide-4.jpg"></a></li>
<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-5</p><img src="images/slide-5.jpg"></a></li>
<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-1</p><img src="images/slide-1.jpg"></a></li>
</ul> <!--底部小圓點(diǎn)-->
<ol class="dot" id="dot">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
<!--左右兩側(cè)的點(diǎn)擊切換按鈕-->
<div class="arrow-box">
<div class="arrow arrow-l" id="arrow_l">‹</div>
<div class="arrow arrow-r" id="arrow_r">›</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/index.js"></script>
</body>
</html>
二、style.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.layout {
width: 1000px;
margin: 30px auto;
}
ul,ol,li {
list-style: none;
}
.slide {
position: relative;
width: 900px;
margin:auto;
}
.slide .outer {
position: relative;
margin: 30px auto;
width: 720px;
height: 400px;
overflow: hidden;
}
.slide .outer .inner {
width: 5040px;
height: 350px;
position: absolute;
left: -720px;
top: 0;
}
.slide .outer .inner li {
float: left;
height: 350px;
}
.slide .outer .inner li a {
display: block;
position: relative;
width: 100%;
height: 100%;
}
.slide .outer .inner li a p {
position: absolute;
left: 0;
bottom: 0;
color: #fff;
font-size: 18px;
width: 720px;
height: 80px;
line-height: 80px;
padding-left: 50px;
background: linear-gradient(180deg,rgba(0,0,0,0), rgba(0,0,0,0.5));
}
.slide .outer .dot {
margin-top: 365px;
text-align: center;
}
.slide .outer .dot li {
height: 6px;
width: 6px;
border-radius: 3px;
background-color: #d2cbcb;
display: inline-block;
margin: 0 3px;
}
.slide .outer .dot li.active {
background-color: #6e5ca5;
}
.slide .arrow-box {
position: absolute;
width: 900px;
height: 60px;
top: 150px;
left: 0;
}
.slide .arrow-box .arrow {
width: 60px;
height: 60px;
line-height: 60px;
text-align: center;
border-radius: 30px;
background-color: #dde2e6;
font-size: 60px;
color: #999;
cursor: pointer;
}
.slide .arrow-box .arrow.arrow-l {
float: left;
}
.slide .arrow-box .arrow.arrow-r {
float: right;
}
三、index.js
注:js代碼中,每個(gè)變量均已給了注釋。為了防止快速多次點(diǎn)擊,而出現(xiàn)動(dòng)畫(huà)不停的現(xiàn)象,這里在每次切換圖片的時(shí)候先調(diào)用stop(false,true)。但是注意在向左側(cè)滾動(dòng)的時(shí)候,滾動(dòng)到最后一張圖圖片后,再次切換時(shí)就不要用stop(false,true),而是要瞬間定位到第一張圖片(其實(shí)是dom結(jié)構(gòu)中的第二張)的位置,同樣,向右側(cè)滾動(dòng)時(shí),當(dāng)滾動(dòng)到第一張圖片后,再次切換時(shí)就不用stop(false,true),而是要瞬間定位到最后一張圖片(其實(shí)是dom結(jié)構(gòu)中的倒數(shù)第二張)的位置。
var interval = 3000; //輪播間隔時(shí)間
var arrowL = $('#arrow_l'); //左側(cè)箭頭
var arrowR = $('#arrow_r'); //右側(cè)箭頭
var slideBox = $('#slide'); //輪播圖區(qū)域
var innerBox = $('#inner'); //內(nèi)層大盒子
var img = innerBox.children('li'); //每個(gè)圖片
var dot = $('#dot'); //小圓點(diǎn)盒子
var imgW = $(img[0]).outerWidth(); //每個(gè)li標(biāo)簽的寬度
var imgCount = 5; //總圖片個(gè)數(shù)(不同圖片的個(gè)數(shù))(實(shí)際dom上是有7張)
var i = 0; //初始化為第0張圖片
timer = null; //定時(shí)器
//自動(dòng)輪播
timer = setInterval(function () {
i++;
innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
dot.find('li').removeClass('active').eq(i-1).addClass('active')
if(i > imgCount){
innerBox.animate({'left':-1*imgW+'px'},0);
dot.find('li').removeClass('active').eq(0).addClass('active')
i = 1;
}
},interval)
//點(diǎn)擊右側(cè)箭頭,播放下一張
arrowR.click(function () {
i++;
innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
dot.find('li').removeClass('active').eq(i-1).addClass('active')
if(i > imgCount){
innerBox.animate({'left':-1*imgW+'px'},0);
dot.find('li').removeClass('active').eq(0).addClass('active')
i = 1;
}
})
//點(diǎn)擊左側(cè)箭頭,播放上一張
arrowL.click(function () {
i--;
innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
dot.find('li').removeClass('active').eq(i-1).addClass('active')
if(i < 1){
innerBox.animate({'left':-imgCount*imgW+'px'},0);
dot.find('li').removeClass('active').eq(imgCount-1).addClass('active')
i = imgCount;
}
})
//鼠標(biāo)經(jīng)過(guò)輪播圖區(qū)域時(shí),清除定時(shí)器,停止自動(dòng)輪播
slideBox.mouseenter(function () {
clearInterval(timer);
})
//鼠標(biāo)離開(kāi)輪播圖區(qū)域時(shí),重新啟動(dòng)自動(dòng)輪播
slideBox.mouseleave(function () {
timer = setInterval(function () {
i++;
innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
dot.find('li').removeClass('active').eq(i-1).addClass('active')
if(i > imgCount){
innerBox.animate({'left':-1*imgW+'px'},0);
dot.find('li').removeClass('active').eq(0).addClass('active')
i = 1;
}
},interval)
})
四、效果圖展示



- jquery實(shí)現(xiàn)左右輪播圖效果
- JQuery和html+css實(shí)現(xiàn)帶小圓點(diǎn)和左右按鈕的輪播圖實(shí)例
- jquery版輪播圖效果和extend擴(kuò)展
- jQuery制作全屏寬度固定高度輪播圖(實(shí)例講解)
- jquery實(shí)現(xiàn)左右滑動(dòng)式輪播圖
- jQuery實(shí)現(xiàn)一個(gè)簡(jiǎn)單的輪播圖
- jQuery按需加載輪播圖(web前端性能優(yōu)化)
- jquery實(shí)現(xiàn)輪播圖效果
- 用jQuery實(shí)現(xiàn)優(yōu)酷首頁(yè)輪播圖
- jQuery無(wú)縫輪播圖代碼
- jquery 實(shí)現(xiàn)輪播圖詳解及實(shí)例代碼
- 原生Javascript和jQuery做輪播圖簡(jiǎn)單例子
- jQuery實(shí)現(xiàn)簡(jiǎn)潔的輪播圖效果實(shí)例
相關(guān)文章
jQuery監(jiān)聽(tīng)文件上傳實(shí)現(xiàn)進(jìn)度條效果的方法
下面小編就為大家?guī)?lái)一篇jQuery監(jiān)聽(tīng)文件上傳實(shí)現(xiàn)進(jìn)度條效果的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
jQuery選擇器源碼解讀(八):addCombinator函數(shù)
這篇文章主要介紹了jQuery選擇器源碼解讀(八):addCombinator函數(shù),本文用詳細(xì)的注釋解讀了addCombinator函數(shù)的實(shí)現(xiàn)源碼,需要的朋友可以參考下2015-03-03
jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼
jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10
jQuery Mobile中的button按鈕組件基礎(chǔ)使用教程
jQuery Mobile中的button默認(rèn)提供了很多用于制作移動(dòng)Web頁(yè)面按鈕的屬性,這里我們來(lái)整理一下jQuery Mobile中的button按鈕組件基礎(chǔ)使用教程,需要的朋友可以參考下2016-05-05
jQuery實(shí)現(xiàn)復(fù)制到粘貼板功能
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)復(fù)制到粘貼板功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

