JS實(shí)現(xiàn)普通輪播圖特效
本文實(shí)例為大家分享了JS實(shí)現(xiàn)輪播圖特效的具體代碼,供大家參考,具體內(nèi)容如下
知識(shí)點(diǎn)
輪播圖思想:
① 建立一個(gè)全局變量索引,始終標(biāo)記當(dāng)前顯示圖片。
② 根據(jù)當(dāng)前圖片的數(shù)量,動(dòng)態(tài)創(chuàng)建下方的●圖片指示器。
③ 輪播圖的初始狀態(tài)為第一張圖片在中間,剩余所有圖片均放在即將顯示圖片位置。
④ 當(dāng)點(diǎn)擊>的時(shí)候,當(dāng)前圖片調(diào)用動(dòng)畫(huà)移動(dòng)函數(shù)進(jìn)行左移,與此同時(shí)新的一張圖片調(diào)用動(dòng)畫(huà)函數(shù)移入到div中,而會(huì)將下一張展示的圖片移動(dòng)到div右側(cè)。
⑤ 需要進(jìn)行邊界判斷,如果當(dāng)前的圖片大于圖片數(shù)量或者小于等于0,重新給索引賦值。
⑥ 當(dāng)點(diǎn)擊圖片指示器的時(shí)候,首先判定點(diǎn)擊的與索引的位置關(guān)系,然后進(jìn)行動(dòng)畫(huà)移動(dòng)。
⑦ 給div添加定時(shí)器,自動(dòng)移動(dòng)圖片。當(dāng)鼠標(biāo)進(jìn)入div,刪除定時(shí)器,當(dāng)鼠標(biāo)移出div,設(shè)置定時(shí)器。
運(yùn)行效果
1.自動(dòng)輪播
2.點(diǎn)擊左右切換圖片
3.點(diǎn)擊下方圖片指示器切換圖片
代碼
引入MyTools.js庫(kù)
1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="1.css" rel="external nofollow" > </head> <body> <div id="box"> <div id="box_content"> <div class="box_img"><img src="casual01.jpg" alt=""></div> <div class="box_img"><img src="casual02.jpg" alt=""></div> <div class="box_img"><img src="casual03.jpg" alt=""></div> <div class="box_img"><img src="casual04.jpg" alt=""></div> <div class="box_img"><img src="casual05.jpg" alt=""></div> </div> <div id="box_control"> <a href="javascript:;" class="box_control_left"><i><</i></a> <a href="javascript:;" class="box_control_right"><i>></i></a> <ul> </ul> </div> </div> <script src="../JavaScript學(xué)習(xí)/00MyTools/MyTools.js"></script> <script src="1.js"></script> </body> </html>
2.css
*{margin: 0;padding: 0;} a{ color: #999; text-decoration: none; position: absolute; top: 50%; transform: translateY(-50%); background-color: rgba(0, 0, 0, .4); } a:hover{ color: #f8b62b; } i{ font-size: 50px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; } #box{ height: 482px; width: 830px; background-color: red; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); overflow: hidden; } #box_content{ height: 100%; width: 100%; cursor: pointer; } #box_content img{ position: absolute; vertical-align: top; height: 100%; width: 100%; /*left: 830px;*/ } .box_img{ width: 100%; height: 100%; position: absolute;} .box_control_right{ position: absolute; right: 0; } .box_control_left{ position: absolute; left: 0; } ul{ position: absolute; bottom: 30px; left: 50%; transform: translateX(-50%); display: flex; justify-content:space-evenly; } ul>li{ list-style: none; width: 16px; height: 16px; background-color: #fff; margin: 0 3px; border-radius: 50%; cursor: pointer; } ul>li.current{ background-color: darkorange; }
3.js
window.addEventListener('load',function (ev) { // 輪播圖 (function () { // 1. 獲取需要標(biāo)簽 var boxContent = myTool.$('box_content'); var contentImg = boxContent.children; var boxControl = myTool.$('box_control'); var controlBottom = boxControl.children[2]; // 2. 全局索引 var iNow = 0; // 3. 根據(jù)圖片個(gè)數(shù)動(dòng)態(tài)添加下方圖片指示器 for (var i = 0; i < contentImg.length; i++) { var li = document.createElement('li'); controlBottom.insertBefore(li,controlBottom.children[0]); } // 4. 讓第一個(gè)圖片指示器選中 controlBottom.children[0].className = 'current'; // 5. 讓除了第一張圖片以外所有圖片進(jìn)入待顯示區(qū)域 var scrollImgWidth = boxContent.offsetWidth; for (var j = 1; j < contentImg.length; j++) { contentImg[j].style.left = scrollImgWidth + 'px'; } // 6. 處理左右兩側(cè)點(diǎn)擊 var cPrev = boxControl.children[0]; var cNext = boxControl.children[1]; // 6.1 點(diǎn)擊左邊 cPrev.addEventListener('click',function (evt) { // 1. 當(dāng)前可視區(qū)域圖片快速右移 // 2. 上一張幻燈片出現(xiàn)在可視區(qū)域左側(cè) // 3. 讓這張幻燈片做動(dòng)畫(huà)進(jìn)入 myTool.slowMoving(contentImg[iNow],{'left':scrollImgWidth},null); iNow--; // 邊界處理 if (iNow < 0){ iNow = contentImg.length - 1; } contentImg[iNow].style.left = -scrollImgWidth + 'px'; myTool.slowMoving(contentImg[iNow],{'left':0},null); // 切換索引 changeIndex(); },false); // 6.2 點(diǎn)擊右邊 cNext.addEventListener('click',function (evt) { autoPlay(); },false); // 7. 下側(cè)圖片指示器操作 for (var k = 0; k < controlBottom.children.length; k++) { // 取出單個(gè)li標(biāo)簽 var bottomLi = controlBottom.children[k]; // 監(jiān)聽(tīng)鼠標(biāo)進(jìn)入 (function (index) { bottomLi.addEventListener('mouseover',function (evt) { // 比較當(dāng)前索引和點(diǎn)擊指示器位置關(guān)系 if (index > iNow){ myTool.slowMoving(contentImg[iNow],{'left':-scrollImgWidth},null); contentImg[index].style.left = scrollImgWidth + 'px'; }else if(index < iNow){ myTool.slowMoving(contentImg[iNow],{'left':scrollImgWidth},null); contentImg[index].style.left = -scrollImgWidth + 'px'; } iNow = index; myTool.slowMoving(contentImg[iNow],{'left':0}); // 切換索引 changeIndex(); },false); })(k) } /** * 切換索引操作 */ function changeIndex() { for (var i = 0; i < controlBottom.children.length; i++) { controlBottom.children[i].className = ''; } // 當(dāng)前的被選中 controlBottom.children[iNow].className = 'current'; } /** * 點(diǎn)擊右側(cè)和圖片自動(dòng)運(yùn)動(dòng)操作 */ function autoPlay(){ // 1. 當(dāng)前可視區(qū)域圖片快速左移 // 2. 下一張圖片出現(xiàn)在可視區(qū)域右側(cè) // 3. 讓這張圖片做動(dòng)畫(huà)進(jìn)入 myTool.slowMoving(contentImg[iNow],{'left':-scrollImgWidth},null); iNow++; // 邊界處理 if (iNow >= contentImg.length) { iNow = 0; } contentImg[iNow].style.left = scrollImgWidth + 'px'; myTool.slowMoving(contentImg[iNow], {"left": 0},null); // 切換索引 changeIndex(); } // 8. 設(shè)置定時(shí)器 var timerId = setInterval(autoPlay,2000); // 9. 鼠標(biāo)進(jìn)入圖片div后設(shè)置和清除定時(shí)器 myTool.$('box').addEventListener('mouseover',function () { clearInterval(timerId); }); myTool.$('box').addEventListener('mouseout',function () { timerId = setInterval(autoPlay,2000); }); })(); },false);
更多關(guān)于輪播圖效果的專題,請(qǐng)點(diǎn)擊下方鏈接查看學(xué)習(xí)
javascript圖片輪播效果匯總
jquery圖片輪播效果匯總
Bootstrap輪播特效匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:JavaScript
下一篇:nodejs開(kāi)發(fā)一個(gè)最簡(jiǎn)單的web服務(wù)器實(shí)例講解
本文標(biāo)題:JS實(shí)現(xiàn)普通輪播圖特效
本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9331.html
您可能感興趣的文章
- 04-02java后端代碼分頁(yè) java后端實(shí)現(xiàn)分頁(yè)page
- 01-10Echarts實(shí)現(xiàn)單條折線可拖拽效果
- 01-10在Vue項(xiàng)目中使用Typescript的實(shí)現(xiàn)
- 01-10js實(shí)現(xiàn)上傳圖片并顯示圖片名稱
- 01-10Vue中使用Lodop插件實(shí)現(xiàn)打印功能的簡(jiǎn)單方法
- 01-10echarts實(shí)現(xiàn)折線圖的拖拽效果
- 01-10d3.js實(shí)現(xiàn)圖形縮放平移
- 01-10小程序簡(jiǎn)單兩欄瀑布流效果的實(shí)現(xiàn)
- 01-10H5實(shí)現(xiàn)手機(jī)拍照和選擇上傳功能
- 01-10Echarts實(shí)現(xiàn)多條折線可拖拽效果


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02javascript點(diǎn)線,點(diǎn)線的代碼
- 04-02javascript潛力,javascript強(qiáng)大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報(bào),JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁(yè)面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?