jquery寫出PC端輪播圖實例
最近其他項目不是很忙,被安排給公司的官網(wǎng)項目做一個新的頁面(之前沒接觸公司官網(wǎng)項目),其中有一個用到輪播圖的地方,最開始想直接用swiper.js插件實現(xiàn)就好了,可是發(fā)現(xiàn)官網(wǎng)項目里之前都沒有引入過swiper.js,后來想了想,就不引入它了,免得又得增加依次一次網(wǎng)絡(luò)請求,項目里既然已經(jīng)用到了jQuery,那就索性用jQuery寫一個輪播圖吧。
現(xiàn)在把自己寫的輪播圖這塊代碼單獨拿出來,做一個小demo寫在這里記錄一下(demo中輪播圖的圖片網(wǎng)上隨意找的)
實現(xiàn)的效果:
1、自動輪播(輪播時間間隔在js代碼中自定義)
2、點擊左右側(cè)按鈕,實現(xiàn)手動切換
3、底部小圓點根據(jù)切換圖片的位置相應(yīng)的顯示active狀態(tài)
4、鼠標(biāo)經(jīng)過輪播圖區(qū)域,停止輪播,離開輪播圖區(qū)域開始輪播
代碼目錄結(jié)果如下:
一、index.html
注:這里以5張圖片為例,頁面上真正輪播展示給用戶看到的是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> <!--底部小圓點--> <ol class="dot" id="dot"> <li class="active"></li> <li></li> <li></li> <li></li> <li></li> </ol> </div> <!--左右兩側(cè)的點擊切換按鈕--> <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代碼中,每個變量均已給了注釋。為了防止快速多次點擊,而出現(xiàn)動畫不停的現(xiàn)象,這里在每次切換圖片的時候先調(diào)用stop(false,true)。但是注意在向左側(cè)滾動的時候,滾動到最后一張圖圖片后,再次切換時就不要用stop(false,true),而是要瞬間定位到第一張圖片(其實是dom結(jié)構(gòu)中的第二張)的位置,同樣,向右側(cè)滾動時,當(dāng)滾動到第一張圖片后,再次切換時就不用stop(false,true),而是要瞬間定位到最后一張圖片(其實是dom結(jié)構(gòu)中的倒數(shù)第二張)的位置。
var interval = 3000; //輪播間隔時間 var arrowL = $('#arrow_l'); //左側(cè)箭頭 var arrowR = $('#arrow_r'); //右側(cè)箭頭 var slideBox = $('#slide'); //輪播圖區(qū)域 var innerBox = $('#inner'); //內(nèi)層大盒子 var img = innerBox.children('li'); //每個圖片 var dot = $('#dot'); //小圓點盒子 var imgW = $(img[0]).outerWidth(); //每個li標(biāo)簽的寬度 var imgCount = 5; //總圖片個數(shù)(不同圖片的個數(shù))(實際dom上是有7張) var i = 0; //初始化為第0張圖片 timer = null; //定時器 //自動輪播 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) //點擊右側(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; } }) //點擊左側(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)過輪播圖區(qū)域時,清除定時器,停止自動輪播 slideBox.mouseenter(function () { clearInterval(timer); }) //鼠標(biāo)離開輪播圖區(qū)域時,重新啟動自動輪播 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實現(xiàn)左右輪播圖效果
- JQuery和html+css實現(xiàn)帶小圓點和左右按鈕的輪播圖實例
- jquery版輪播圖效果和extend擴展
- jQuery制作全屏寬度固定高度輪播圖(實例講解)
- jquery實現(xiàn)左右滑動式輪播圖
- jQuery實現(xiàn)一個簡單的輪播圖
- jQuery按需加載輪播圖(web前端性能優(yōu)化)
- jquery實現(xiàn)輪播圖效果
- 用jQuery實現(xiàn)優(yōu)酷首頁輪播圖
- jQuery無縫輪播圖代碼
- jquery 實現(xiàn)輪播圖詳解及實例代碼
- 原生Javascript和jQuery做輪播圖簡單例子
- jQuery實現(xiàn)簡潔的輪播圖效果實例
相關(guān)文章
Jquery 數(shù)據(jù)選擇插件Pickerbox使用介紹
目前市面上很少見或幾乎沒有這數(shù)據(jù)(對象)選擇插件.比如,點擊input , select 元素時彈出div(窗口),載入數(shù)據(jù)讓用戶選擇數(shù)據(jù),選擇后在填充回對應(yīng)的元素.2012-08-08Javascript中的異步編程規(guī)范Promises/A詳細介紹
這篇文章主要介紹了Javascript中的異步編程規(guī)范Promises/A詳細介紹,同時介紹了jQuery 中的 Deferred 和 Promises,需要的朋友可以參考下2014-06-06jQuery懸停文字提示框插件jquery.tooltipster.js用法示例【附demo源碼下載】
這篇文章主要介紹了jQuery懸停文字提示框插件jquery.tooltipster.js用法,涉及jQuery文字提示框插件的引入與調(diào)用實現(xiàn)技巧,非常簡單實用,需要的朋友可以參考下2016-07-07用原生JavaScript實現(xiàn)jQuery的$.getJSON的解決方法
本篇文章介紹了,用原生JavaScript實現(xiàn)jQuery的$.getJSON的解決方法。需要的朋友參考下2013-05-05jQuery實現(xiàn)的浮動層div瀏覽器居中顯示效果
這篇文章主要介紹了jQuery實現(xiàn)的浮動層div瀏覽器居中顯示效果,涉及jQuery及JS動態(tài)操作頁面元素與屬性相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2017-02-02