jQuery輪播圖實(shí)例詳解
本文實(shí)例為大家分享了jQuery輪播圖實(shí)例的具體代碼,供大家參考,具體內(nèi)容如下
1、html+css+js代碼
<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; text-decoration: none; } body{ padding: 20px; } #container{ min-width: 1000px; /*width: 1300px;*/ height: 400px; overflow: hidden; position: relative; margin: 0 auto; } #list{ /*width: 9100px;*/ height: 400px; position: absolute; z-index: 1; top:0; left: 0; overflow: hidden; } #list img{ float: left; /*width: 1300px;*/ height: 400px; } #buttons{ position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 660px; text-align: center; } #buttons span{ cursor: pointer; float: left; width: 10px; height: 10px; margin-right: 9px; display: inline-block; background-image: url(img/_eb1b95e.png); background-position: -1079px -687px; } #buttons .on{ background-position: -1049px -687px; } .arrow{ cursor: pointer; display: none; width: 36px; height: 76px; position: absolute; z-index: 2; top: 180px; background-color: rgba(0,0,0,.3); color: #fff; } #container:hover .arrow{ display: block; } #prev{ background: url(img/_eb1b95e.png); background-position: -569px -491px; left: 20px; } #next{ background: url(img/_eb1b95e.png); background-position: -513px -491px; right: 20px; } </style> </head> <body> <div id="container"> <div id="list" > <img src="img/5.jpg" alt="1"/> <img src="img/1.jpg" alt="1"/> <img src="img/2.jpg" alt="2"/> <img src="img/3.jpg" alt="3"/> <img src="img/4.jpg" alt="4"/> <img src="img/5.jpg" alt="5"/> <img src="img/1.jpg" alt="5"/> </div> <div id="buttons"> <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="javascript:;" id="prev" class="arrow"></a> <a href="javascript:;" id="next" class="arrow"></a> </div> <script type="text/javascript" src="js/jquery.1.10.2.js"></script> <script type="text/javascript"> var container = $("#container"); var list = $("#list"); var listimg = $("#list img"); var buttons = $("#buttons span"); var prev = $("#prev"); var next = $("#next"); var index = 1; var len = 5; var num =len+2; var interval = 3000;//變換周期 var timer; var clientwidth=document.documentElement.clientWidth;//屏幕的寬度 var conwidth = parseInt(clientwidth)-100;//顯示界面的寬度 $(function(){ setwidth();//設(shè)置container的寬度以及里面元素list和list中img的寬度 function animate(offset){ var left = parseInt(list.css("left"))+offset; // list.animate({left:left+'px'},'normal'); list.animate({left:left+'px'},conwidth,function(){ //第一位規(guī)定產(chǎn)生動(dòng)畫(huà)效果的樣式,第二位設(shè)置速度,第三位是動(dòng)畫(huà)函數(shù)執(zhí)行完后執(zhí)行的函數(shù) if (left > -conwidth) {//如果是第一個(gè)元素還向前移,就讓最后一個(gè)元素是這個(gè)元素 list.css('left',-conwidth*len); } if (left < (-conwidth*len)) {//如果是最后一個(gè)元素還向后移,就讓第一個(gè)元素是這個(gè)元素 list.css('left', -conwidth); } }); } function showbutton(){//通過(guò)操作css來(lái)將顯示的圖片代表的下方原點(diǎn)變大,其余變小 buttons.eq(index-1).addClass('on').siblings().removeClass('on'); } function play(){ timer = setTimeout(function(){ next.trigger('click');//trigger()方法觸發(fā)被選元素的指定事件類(lèi)型。 play(); },interval); } function stop(){ clearTimeout(timer); } next.bind('click',function(){ if (list.is(':animated')) { return; } if (index == 5) { index = 1; } else{ index++; } animate(-conwidth); showbutton(); }); prev.bind('click',function(){ if (list.is(':animated')) { return; } if (index == 1) { index = 5; } else{ index--; } animate(conwidth); showbutton(); }); buttons.each(function(){ $(this).bind('click',function(){ if (list.is(':animated') || $(this).attr('class')=='on') { return; } var myindex = parseInt($(this).attr('index')); var offset = -conwidth*(myindex - index); animate(offset); index = myindex; showbutton(); }) }); container.hover(stop,play);//鼠標(biāo)懸停時(shí)執(zhí)行stop()函數(shù),移開(kāi)時(shí)執(zhí)行play() play(); }); function setwidth(){//設(shè)置container的寬度以及里面元素list和list中img的寬度 container[0].style.width = conwidth +'px' ; list[0].style.width = num*conwidth +'px'; list[0].style.left = '-'+conwidth +'px'; for (var i = 0; i < listimg.length; i++) { listimg[i].style.width = conwidth + 'px'; } } </script> </body> </html>
2、實(shí)現(xiàn)思路
輪播圖的功能可分為:自動(dòng)循環(huán)播放,點(diǎn)擊左邊按鈕顯示前面圖片,點(diǎn)擊后邊顯示后面圖片,點(diǎn)擊下方的小圓點(diǎn)實(shí)現(xiàn)跳轉(zhuǎn)播放。
1.自動(dòng)播放功能:設(shè)置一個(gè)定時(shí)器,每隔一個(gè)周期的時(shí)間,觸發(fā)一次點(diǎn)擊右邊按鈕的函數(shù)功能。
2.點(diǎn)擊左邊按鈕顯示前面圖片:首先我們應(yīng)該了解到輪播圖的原理。圖解
大盒子是container,小盒子是list,list里面有很多圖片,沒(méi)有間隔的排列在一行,用絕對(duì)定位來(lái)操縱每次可以看到的圖片,也就是定位在container里面的是可見(jiàn)部分。當(dāng)點(diǎn)擊左邊的按鈕時(shí),前面的圖片右移,相當(dāng)于絕對(duì)定位中的left值加一個(gè)圖片的寬度。
3.點(diǎn)擊右邊按鈕顯示后面圖片:原理和左邊的相同,相當(dāng)于圖片左移,讓后面的圖片顯示出來(lái)。
4.點(diǎn)擊下方的小圓點(diǎn)實(shí)現(xiàn)跳轉(zhuǎn)播放:此時(shí)頁(yè)面是第二個(gè)圖片,要跳轉(zhuǎn)到第五個(gè),相當(dāng)于點(diǎn)擊了三次右邊的按鈕,也相當(dāng)于圖片左移三個(gè)圖片的寬度。
3、需要掌握的知識(shí)點(diǎn):
css:
絕對(duì)定位
js+jq:
document.documentElement.clientWidth; obj.animate(); obj.css(); obj.eq() obj.addClass(); obj.siblings(); obj.removeClass(); setTimeout(); clearTimeout(); obj.trigger(); obj.attr(); obj.bind();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一個(gè)簡(jiǎn)單的瀑布流效果(主體形式自寫(xiě))
瀑布流的主體即為 幾個(gè)ul標(biāo)簽,新增加的元素以 li的形式按照當(dāng)前 ul的高度有選擇性的插入到某個(gè)ul下,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-05-05Jquery中.bind()、.live()、.delegate()和.on()之間的區(qū)別詳解
在我們?nèi)粘i_(kāi)發(fā)中經(jīng)常會(huì)使用到.bind()、.live()、.delegate()和.on(),有些同學(xué)會(huì)對(duì)這四者存在一些疑慮,所以下面這篇文章主要給大家介紹了關(guān)于Jquery中.bind()、.live()、.delegate()和.on()之間區(qū)別的相關(guān)資料,需要的朋友可以參考借鑒。2017-08-08jquery關(guān)于事件冒泡和事件委托的技巧及阻止與允許事件冒泡的三種實(shí)現(xiàn)方法
大家都知道,jQuery事件觸發(fā)時(shí)有2種機(jī)制,一種是事件委托,另一種是事件冒泡,冒泡或默認(rèn)的事件發(fā)生,在某些時(shí)候是不需要的,在此就需要一些可以阻止冒泡和默認(rèn)的事件的方法,本文介紹三種方法做到不同程度的阻止,感興趣的朋友可以了解下,或許對(duì)你了解冒泡事件有所幫助2015-11-11jQuery實(shí)現(xiàn)鼠標(biāo)移到某個(gè)對(duì)象時(shí)彈出顯示層功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)移到某個(gè)對(duì)象時(shí)彈出顯示層功能,涉及jQuery基于事件響應(yīng)動(dòng)態(tài)操作頁(yè)面元素屬性相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-08-08jquery通過(guò)load獲取文件的內(nèi)容并跳到錨點(diǎn)的方法
這篇文章主要介紹了jquery通過(guò)load獲取文件的內(nèi)容并跳到錨點(diǎn)的方法,涉及l(fā)oad及animate等方法的使用技巧,需要的朋友可以參考下2015-01-01JQuery的Validation插件中Remote驗(yàn)證的中文問(wèn)題
前段時(shí)間,再次出現(xiàn)AJAX中文編碼問(wèn)題,導(dǎo)致會(huì)員名重復(fù)檢測(cè)失敗,不過(guò)這次出現(xiàn)問(wèn)題的是Validation插件的remote驗(yàn)證。2010-07-07