js圖片輪播插件的封裝
更新時間:2017年07月21日 10:52:17 作者:趙-碧-菡
這篇文章主要為大家詳細介紹了js圖片輪播插件的封裝代碼,只需要獲取到圖片和按鈕,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了js圖片輪播插件的具體代碼,供大家參考,具體內容如下
我封裝的這個輪播插件只需要獲取到圖片和按鈕就可以啦。
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;
}
頁面結構 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部分,插件調用
<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();
}
//設置默認值
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();
//當鼠標經(jīng)過 輪播停止,移開繼續(xù)
opts.imgElement.hover(function() {
clearInterval(timeInter);
}, function() {
showTime();
});
//點擊li 顯示對應的圖片
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圖片輪播
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
理解Javascript的caller,callee,call,apply區(qū)別
理解Javascript的caller,callee,call,apply區(qū)別...2007-03-03
js 實現(xiàn) list轉換成tree的方法示例(數(shù)組到樹)
這篇文章主要介紹了js 實現(xiàn) list轉換成tree的方法示例(數(shù)組到樹),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
JavaScript切換搜索引擎的導航網(wǎng)頁搜索框實例代碼
這篇文章主要介紹了javascript切換搜索引擎的導航網(wǎng)頁搜索框的實例代碼,非常不錯,具有參考借鑒價值 ,需要的朋友可以參考下2017-06-06
微信小程序 多行文本顯示...+顯示更多按鈕和收起更多按鈕功能
這篇文章主要介紹了微信小程序多行文本顯示...+顯示更多按鈕和收起更多按鈕,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09

