下面小編就為大家帶來一篇使用JS實作圖片輪播的實例(前後首尾相接)。小編覺得挺不錯的,現在就js原始碼分享給大家,也給大家做個參考。對js有興趣的一起跟著小編過來看看吧
最近各種跑面試,終於還是被問到這個,一腦子漿糊,當時沒想出來首尾相接怎麼搞,回來之後研究了一波,終於搞出來了,不多說,直接看代碼
代碼參考了一位已經寫好了圖片輪播功能的(再次表示感謝),但是沒有首尾相接的功能,本人在此基礎上增加了首尾相接功能。
效果如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>图片轮播</title> <style type="text/css"> body,p,ul,li,a,img{margin: 0;padding: 0;} ul,li{list-style: none;} a{text-decoration: none;} #wrapper{ position: relative; margin: 30px auto; /* 上下边距30px,水平居中 */ width: 400px; height: 200px; } #banner{ position:relative; width: 400px; height: 200px; overflow: hidden; } .imgList{ position:relative; width:2000px; height:200px; z-index: 10; overflow: hidden; } .imgList li{float:left;display: inline;} #prev, #next{ position: absolute; top:80px; z-index: 20; cursor: pointer; opacity: 0.2; filter:alpha(opacity=20); } #prev{left: 10px;} #next{right: 10px;} #prev:hover, #next:hover{opacity: 0.5;filter:alpha(opacity=50);} </style> </head> <body> <p id="wrapper"><!-- 最外层部分 --> <p id="banner"><!-- 轮播部分 --> <ul class="imgList"><!-- 图片部分 --> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/1.jpg" class="lazy" style="max-width:90%" height="200px" alt="1"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/2.jpg" class="lazy" style="max-width:90%" height="200px" alt="2"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/3.jpg" class="lazy" style="max-width:90%" height="200px" alt="3"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/4.jpg" class="lazy" style="max-width:90%" height="200px" alt="4"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/5.jpg" class="lazy" style="max-width:90%" height="200px" alt="5"></a></li> </ul> <img src="/static/imghwm/default1.png" data-src="./img/prev.png" class="lazy" style="max-width:90%" height="40px" id="prev" alt="使用JS實作圖片輪播的實例" > <img src="/static/imghwm/default1.png" data-src="./img/next.png" class="lazy" style="max-width:90%" height="40px" id="next" alt="使用JS實作圖片輪播的實例" > </p> </p> <script type="text/javascript" src="./js/jquery-3.2.1.min.js"></script> <script type="text/javascript"> var curIndex = 0, //当前index imgLen = $(".imgList li").length; //图片总数 $(".imgList").css("width", (imgLen+1)*400+"px"); // 定时器自动变换3秒每次 var autoChange = setInterval(function(){ if(curIndex < imgLen-1){ curIndex ++; }else{ curIndex = 0; } //调用变换处理函数 changeTo(curIndex); },3000); //左箭头滑入滑出事件处理 $("#prev").hover(function(){ //滑入清除定时器 clearInterval(autoChange); }, function(){ //滑出则重置定时器 autoChangeAgain(); }); //左箭头点击处理 $("#prev").click(function(){ //根据curIndex进行上一个图片处理 // curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1); if (curIndex == 0) { var element = document.createElement("li"); element.innerHTML = $(".imgList li")[imgLen - 1].innerHTML; // $(".imgList li")[imgLen - 1].remove(); $(".imgList").prepend(element); $(".imgList").css("left", -1*400+"px"); changeTo(curIndex); curIndex = -1; } else if (curIndex == -1) { $(".imgList").css("left", -(imgLen-1)*400+"px"); curIndex = imgLen-2; $(".imgList li")[0].remove(); changeTo(curIndex); } else { --curIndex; changeTo(curIndex); } }); //右箭头滑入滑出事件处理 $("#next").hover(function(){ //滑入清除定时器 clearInterval(autoChange); }, function(){ //滑出则重置定时器 autoChangeAgain(); }); //右箭头点击处理 $("#next").click(function(){ // curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0; console.log(imgLen); if (curIndex == imgLen-1) { var element = document.createElement("li"); element.innerHTML = $(".imgList li")[0].innerHTML; // $(".imgList li")[0].remove(); $(".imgList").append(element); ++curIndex; } else if (curIndex == imgLen) { curIndex = 0; $(".imgList").css("left", "0px"); $(".imgList li")[imgLen].remove(); curIndex++; } else { ++curIndex; } changeTo(curIndex); }); //清除定时器时候的重置定时器--封装 function autoChangeAgain(){ autoChange = setInterval(function(){ if(curIndex < imgLen-1){ curIndex ++; }else{ curIndex = 0; } //调用变换处理函数 changeTo(curIndex); },3000); } function changeTo(num){ var goLeft = num * 400; $(".imgList").animate({left: "-" + goLeft + "px"},500); } </script> </body> </html>
以上這篇使用JS實作圖片輪播的實例就是小編分享給大家的全部內容了,希望能給大家一個參考! !
相關推薦:
以上是使用JS實作圖片輪播的實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr

Node.js擅長於高效I/O,這在很大程度上要歸功於流。 流媒體匯總處理數據,避免內存過載 - 大型文件,網絡任務和實時應用程序的理想。將流與打字稿的類型安全結合起來創建POWE

Python和JavaScript在性能和效率方面的差異主要體現在:1)Python作為解釋型語言,運行速度較慢,但開發效率高,適合快速原型開發;2)JavaScript在瀏覽器中受限於單線程,但在Node.js中可利用多線程和異步I/O提升性能,兩者在實際項目中各有優勢。

JavaScript起源於1995年,由布蘭登·艾克創造,實現語言為C語言。 1.C語言為JavaScript提供了高性能和系統級編程能力。 2.JavaScript的內存管理和性能優化依賴於C語言。 3.C語言的跨平台特性幫助JavaScript在不同操作系統上高效運行。

JavaScript在瀏覽器和Node.js環境中運行,依賴JavaScript引擎解析和執行代碼。 1)解析階段生成抽象語法樹(AST);2)編譯階段將AST轉換為字節碼或機器碼;3)執行階段執行編譯後的代碼。

Python和JavaScript的未來趨勢包括:1.Python將鞏固在科學計算和AI領域的地位,2.JavaScript將推動Web技術發展,3.跨平台開發將成為熱門,4.性能優化將是重點。兩者都將繼續在各自領域擴展應用場景,並在性能上有更多突破。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。