js圖片輪播插件的封裝
本文為大家分享了js圖片輪播插件的具體代碼,供大家參考,具體內(nèi)容如下
我封裝的這個(gè)輪播插件只需要獲取到圖片和按鈕就可以啦。
css 樣式
.body{ width: 700px; margin: 100px auto; position: relative; height: 300px; overflow: hidden; } .body img{ width: 700px; position: absolute; display: none; } .body ul{ position: absolute; bottom: 3px; left: 50%; transform: translateX(-50%); } .body li{ list-style: none; float: left; width: 15px; height: 15px; border-radius: 50px; background: none; border: 2px solid #fff; margin-right: 10px; cursor: pointer; } .active{ background-color: #fff !important; } .body a{ text-decoration: none; position: absolute; display: block; top: 50%; transform: translateY(-50%); height: 50px; width: 30px; background-size: 100% 60%; background-color: rgba(0,0,0,0.3); } .left{ left: 0; background: url('../img/left.png') no-repeat center center; } .right{ right: 0; background: url('../img/right.png') no-repeat center center; }
頁面結(jié)構(gòu) html 代碼
<body> <div class="body"> <img src="img/1.jpg"> <img src="img/2.jpg"> <img src="img/3.jpg"> <ul> <li class="active"></li> <li></li> <li></li> </ul> <a href="javascript:;" class="left"></a> <a href="javascript:;" class="right"></a> </div>
js部分,插件調(diào)用
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="js/slider.js"></script> <script type="text/javascript"> $.slider({ imgElement:$(".body img"), liElement:$(".body li"), leftBtn:$(".left"), rightBtn:$(".right"), time:2000 }) </script>
封裝的插件
(function($){ function slider(options){ this.opts=$.extend({},slider.defaluts,options); this._imgSlider(); } //設(shè)置默認(rèn)值 slider.defaluts={ imgElement:null, liElement:null, leftBtn:null, rightBtn:null, time:2000 } slider.prototype._imgSlider=function(){ var opts=this.opts, index=0, len=opts.imgElement.length, timeInter=null; if(opts.imgElement=='') return; opts.imgElement.eq(0).show(); showTime(); //當(dāng)鼠標(biāo)經(jīng)過 輪播停止,移開繼續(xù) opts.imgElement.hover(function() { clearInterval(timeInter); }, function() { showTime(); }); //點(diǎn)擊li 顯示對(duì)應(yīng)的圖片 opts.liElement.click(function(){ var id=$(this).index(); show(id); }); //向左向右 opts.leftBtn.click(function(){ clearInterval(timeInter); --index; index=Math.max(0,index); show(index); showTime(); }); opts.rightBtn.click(function(){ clearInterval(timeInter); ++index; index=Math.min(len-1,index); show(index); showTime(); }); function showTime(){ timeInter=setInterval(function(){ index++; if(index>len){ index=0; } show(index); },opts.time); } function show(index){ opts.imgElement.eq(index).fadeIn(1000).siblings('img').fadeOut(1000); opts.liElement.eq(index).addClass('active').siblings('li').removeClass('active'); } } $.extend({ slider:function(options){ new slider(options); } }) })(jQuery)
效果圖
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 原生JS實(shí)現(xiàn)旋轉(zhuǎn)木馬式圖片輪播插件
- Bootstrap 最常用的JS插件系列總結(jié)(圖片輪播、標(biāo)簽切換等)
- 非常優(yōu)秀的JS圖片輪播插件Swiper的用法
- Bootstrap輪播插件中圖片變形的終極解決方案 使用jqthumb.js
- 原生JS輪播圖插件
- jQuery實(shí)現(xiàn)圖片輪播效果代碼(基于jquery.pack.js插件)
- BootStrap的JS插件之輪播效果案例詳解
- 基于cssSlidy.js插件實(shí)現(xiàn)響應(yīng)式手機(jī)圖片輪播效果
- js輪播圖的插件化封裝詳解
- 原生js無縫輪播插件使用詳解
相關(guān)文章
基于JS實(shí)現(xiàn)橫線提示輸入驗(yàn)證碼隨驗(yàn)證碼輸入消失(js驗(yàn)證碼的實(shí)現(xiàn))
最近在開微信的頁面,在項(xiàng)目需求中遇到之前沒有做過的功能,要求橫線提示輸入驗(yàn)證碼隨驗(yàn)證碼輸入橫線消失,基于js怎么實(shí)現(xiàn)的呢?下面小編給大家分享基于js實(shí)現(xiàn)驗(yàn)證碼功能,感興趣的朋友一起看看吧2016-10-10JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹插入節(jié)點(diǎn)、生成二叉樹示例
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹插入節(jié)點(diǎn)、生成二叉樹,結(jié)合實(shí)例形式較為詳細(xì)的分析了javascript二叉樹相關(guān)概念、定義、節(jié)點(diǎn)插入、遍歷輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02functional繼承模式 摘自javascript:the good parts
javascript:the good parts 書中Inheritance部分講到了一種functional的繼承方式, 具體這個(gè)functional該如何翻譯,就不是很清楚了, 就直接意會(huì)一下吧2011-06-06js addDqmForPP給標(biāo)簽內(nèi)屬性值加上雙引號(hào)的函數(shù)
這篇文章主要介紹了js addDqmForPP給標(biāo)簽內(nèi)屬性值加上雙引號(hào)的函數(shù),需要的朋友可以參考下2016-12-12echarts報(bào)錯(cuò):Error?in?mounted?hook的解決方法
最近又遇到了寫echarts的時(shí)候常遇到的一個(gè)錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于echarts報(bào)錯(cuò):Error?in?mounted?hook:?“TypeError:?Cannot?read?properties?of?undefined?(reading?‘init‘)“的解決方法,需要的朋友可以參考下2022-07-07