jQuery實現(xiàn)的超簡單輪播圖功能示例【代碼解釋】
更新時間:2023年05月20日 16:00:27 作者:澤澤澤Ya
這篇文章主要介紹了jQuery實現(xiàn)的超簡單輪播圖功能,結合完整實例形式分析了基于jQuery實現(xiàn)的輪播圖相關功能實現(xiàn)、樣式設置與注意事項,需要的朋友可以參考下
先看效果
鼠標懸浮時停止輪播,離開時自動輪播,點下一張小圓點會隨著動
直接上代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery輪播圖</title> <script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script> <style> .wrapper { width: 600px; height: 350px; border: 1px solid red; position: relative; } /* 5張圖片疊加到一塊 */ .wrapper img { width: 100%; height: 100%; position: absolute; left: 0; top: 0; display: none; } .wrapper img:nth-of-type(1) { display: block; } /* 小圓點 */ .btn { width: 150px; display: flex; justify-content: space-around; position: absolute; left: 225px; bottom: 10px; z-index: 100 } .btn span { display: block; width: 15px; height: 15px; border: 3px solid white; border-radius: 50%; } /* 左右箭頭 */ .wrapper a { text-decoration: none; font-size: 50px; color: red; position: absolute; top: 35%; } .wrapper a:nth-of-type(1) { left: 10px; } .wrapper a:nth-of-type(2) { right: 10px; } .active { background-color: red; } </style> </head> <body> <div> <div> <img src="./imgs/1.png" alt=""> <img src="./imgs/2.png" alt=""> <img src="./imgs/3.png" alt=""> <img src="./imgs/4.png" alt=""> <img src="./imgs/5.png" alt=""> </div> <div> <span></span> <span></span> <span></span> <span></span> <span></span> </div> <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" ><</a> <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" >">></a> </div> <script src="js/jquery-3.6.0.js"></script> <script> var index=0; // 點擊上一張 $("a:first").click(function(){ prev_pic(); }) // 點擊下一張 $("a:last").click(function(){ next_pic(); }) // 懸浮停止 $(".wrapper").mouseover(function(){ clearInterval(id); }); $(".wrapper").mouseout(function(){ autoplay(); }) // 下一張 function next_pic(){ index++; if(index>4){ index=0; } addStyle(); } // 上一張 function prev_pic(){ index--; if(index<0){ index=4; } addStyle(); } // 控制圖片顯示隱藏,小圓點背景色 function addStyle(){ $("img").eq(index).fadeIn(); $("img").eq(index).siblings().fadeOut(); $("span").eq(index).addClass("active"); $("span").eq(index).siblings().removeClass("active"); } // 自動輪播 var id; autoplay(); function autoplay(){ id=setInterval(function(){ next_pic(); },1000) } </script> </body> </html>
這里只要把圖片地址更換一下 ,引用jquery文件就可以了
另外,很多UI前端框架也有現(xiàn)成的輪播圖效果代碼,如:bootstrap、layui、amazeui等等。感興趣的朋友可以到對應的官網(wǎng)查看示例代碼。
相關文章
學習從實踐開始之jQuery插件開發(fā) 對話框插件開發(fā)
之所以寫下這篇文章,是想將我的想法分享給大家;對于初學者,我希望他能從這篇文章中獲取對他有用的東西,對于經(jīng)驗豐富的開發(fā)者,我希望他能指出我的不足,給我更多的意見和建議;目的就是共同進步2012-04-04IE下支持文本框和密碼框placeholder效果的JQuery插件分享
這篇文章主要介紹了IE下支持文本框和密碼框placeholder效果的JQuery插件分享,本文給出插件源碼和使用示例,需要的朋友可以參考下2015-01-01jQuery效果 slideToggle() 方法(在隱藏和顯示之間切換)
slideToggle() 方法通過使用滑動效果(高度變化)來切換元素的可見狀態(tài)。2011-06-06使用Ajax和Jquery配合數(shù)據(jù)庫實現(xiàn)下拉框的二級聯(lián)動的示例
下面小編就為大家分享一篇使用Ajax和Jquery配合數(shù)據(jù)庫實現(xiàn)下拉框的二級聯(lián)動的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01