js實現無縫輪播圖插件封裝
前言:頁面中輪播圖,對于一個前端開發(fā)者來說,是最基本的技能,不論是的商城網站,還是企業(yè)站,輪播圖已經成為不可缺少的一個模塊,而常見的輪播圖不外乎兩種,一種是漸隱漸現輪播圖,一種是無縫輪播圖。網上關于輪播圖的件也有很多,但是用人家的代碼總會出現各種各樣的bug,我們修改bug往往要耗費很多時間,而且有些插件的效果還不符合我們的需求,那么我們該如何封裝一個自己的輪播插件呢?這就是我們今天的任務,封裝輪播插件。
1、特效離不開合理的頁面布局,所以我們首先需要進行頁面布局:
HTML代碼:
<div class="container mycontainer"> <div class="wrapper"> <div class="slide"> <img src="image/jd01.jpg" alt=""> </div> <div class="slide"> <img src="image/jd02.jpg" alt=""> </div> <div class="slide"> <img src="image/jd03.jpg" alt=""> </div> <div class="slide"> <img src="image/jd04.jpg" alt=""> </div> </div> <!-- 分頁器 --> <ul class="pagination"></ul> <!-- 導航按鈕 --> <div class="button-pre"></div> <div class="button-next"></div> </div>
2、在HTML頁面中引入css樣式文件:css樣式文件代碼如下:
CSS代碼:
* {
margin: 0;
padding: 0;
}
ul,
li {
list-style: none;
}
.container {
margin: 200px auto;
position: relative;
overflow: hidden;
}
.slide {
float: left;
}
img {
display: block;
}
.pagination {
width: 160px;
position: absolute;
bottom: 30px;
margin-left: -80px;
left: 50%;
}
.pagination li {
float: left;
width: 20px;
height: 20px;
background-color: blue;
margin: 0 10px;
border-radius: 50%;
}
.button-pre,
.button-next {
width: 22px;
height: 40px;
position: absolute;
top: 50%;
margin-top: -20px;
}
.button-pre {
left: 30px;
background: url('../image/left.png') no-repeat center center;
}
.button-next {
right: 30px;
background: url('../image/right.png') no-repeat center center;
}
.pagination .active {
background-color: red;
}
.mycontainer{
width: 590px;
height: 470px;
}
頁面布局完成后,接下來就是javaScript代碼,綁定事件;
在綁定事件之前,我們首先要知道無縫輪播圖的原理和一些技術關鍵點。
輪播圖的原理:最外層視野區(qū)域固定大小且的溢出隱藏,通過動態(tài)控制包裹圖片的父元素的marginLeft值實現輪播;
關鍵點1:最外層的盒子container固定大小,是我們的視野區(qū)域,需要設置溢出隱藏overflow:hidden;
關鍵點2:所有輪播的圖片使用一個共同的父元素wrapper包裹起來;
關鍵點3:動態(tài)克隆第一張圖片所在的元素silde,然后添加到最后;
關鍵點4:父元素wrapper的寬度為圖片個數(原始圖片個數+1,因為克隆多添加了一張圖片)乘以單獨一張圖片的寬度;
關鍵點5:實現無縫輪播的判斷條件,marginleft樣式重置時機;
關鍵點6:動態(tài)生成分頁器按鈕,并設置分頁器pagination樣式;
關鍵點7:實現自動播放和清除播放,使用計時器setInterval()和 clearInterval()
關鍵點8:實現代碼復用,借助面向對象的開發(fā)——構造函數+原型對象+jQuery插件封裝機制實現
3、關鍵點梳理完之后,就可以開始javascript代碼:通過自執(zhí)行函數實現;需要在HTML頁面引入JS文件,JS文件代碼如下:
JS代碼:
;(function($){
// 默認設置
var defaults = {
speed:1000,
interval:2000
}
function Banner(ele,options){
// 獲取元素對象
this.element = $(ele);
// 合并設置項
this.options = $.extend({},defaults,options);
// 獲取包裹圖片的父元素
this.wrapper = this.element.children().first();
// 獲取要克隆的元素
this.firstChild = this.wrapper.find('.slide:first');
// 獲取一張圖片寬度
this.Width = this.firstChild.width();
// 記錄圖片下標
this.n = 0;
// 獲取圖片個數
this.len = this.wrapper.find('.slide').length;
// 獲取切換導航按鈕
this.prev = this.element.find('.button-pre');
this.next = this.element.find('.button-next');
// 獲取分頁器
this.pagination = this.element.find('.pagination');
// 計時器
this.timer = null;
}
// 初始化
Banner.prototype.init = function(){
var self = this;
(function () {
// 克隆第一張圖片并添加到元素的最后邊,設置包裹圖片父盒子的寬度
self.wrapper.append(self.firstChild.clone(true));
self.wrapper.css({ width:self.Width * (self.len + 1)});
// 生成對應的分頁器按鈕
for(var i = 0; i < self.len; i++){
$('<li></li>').appendTo(self.pagination);
}
// 動態(tài)設置分頁器的樣式
self.pagination.find('li:first').addClass('active');
var btnWidth = self.pagination.find('li:first').outerWidth(true) * self.len;
self.pagination.css({width:btnWidth,marginLeft:-btnWidth / 2})
})()
// 調用所有綁定的事件
this.nextClick();
this.preClick();
this.btnClick();
this.autoPlay();
this.clearPlay(this.element);
}
// 切換下一張圖片事件
Banner.prototype.nextClick = function(){
var self = this;
this.next.click(function(){
self.moveNext();
})
}
// 切換圖片,同時也為實現自動播放
Banner.prototype.moveNext = function() {
this.n++;
// 判斷重置時機和重置樣式
if (this.n > this.len ) {
this.n = 1;
this.wrapper.css({ marginLeft: 0 });
}
this.changeBtn(this.n > 3 ? 0 : this.n);
this.wrapper.stop(true,true).animate({ marginLeft: -this.Width * this.n }, this.options.speed)
}
// 點擊切換上一張圖片
Banner.prototype.preClick = function(){
var self = this;
this.prev.click(function () {
self.n--;
if (self.n < 0) {
self.n = self.len - 1;
self.wrapper.css({ marginLeft: -(self.len) * self.Width });
}
self.changeBtn(self.n < 0 ? self.n = 3 : self.n);
self.wrapper.animate({ marginLeft: -self.Width * self.n }, self.options.speed)
})
}
// 點擊分頁器切換圖片
Banner.prototype.btnClick = function(){
var self = this;
this.pagination.find('li').click(function () {
var index = $(this).index();
self.n = index;
self.changeBtn(index);
self.wrapper.animate({ marginLeft: -self.Width * index }, self.options.speed)
})
}
// 動態(tài)修改分頁器按鈕的樣式
Banner.prototype.changeBtn = function(index){
this.pagination.find('li').eq(index).addClass('active').siblings().removeClass('active');
}
// 自動輪播
Banner.prototype.autoPlay = function(){
var self = this;
/* 計時器中調用函數是,函數中的this 指向 window, 所以需要使用self.timer = setInterval(function(){
self.moveNext();
},2000);
不能直接使用 self.timer = setInterval(self.moveNext(),2000); 形式 */
self.timer = setInterval(function(){
self.moveNext();
},self.options.interval);
}
// 清除自動播放
Banner.prototype.clearPlay = function(ele){
var self = this;
ele.mouseenter(function(){
clearInterval(self.timer)
}).mouseleave(function(){
// 再次開啟自動輪播
self.timer = setInterval(function(){
self.moveNext();
},self.options.interval);
})
}
// jQuery插件實現
$.fn.myBanner = function(params){
// params 是自定義的配置項
var banner = new Banner(this,params);
banner.init();
// 如果需要鏈式調用
return this;
}
})(jQuery)
最后在HTML頁面中進行初始化,最好放到HTML結束標簽之前的位置,因為我們封裝的插件是依賴于jQuery的,因此首先引入jQuery文件,然后在引入我們自己封裝的js文件;最后就是進行初始化設置:
<script>
$(function(){
$('.mycontainer').myBanner({
// speed:圖片切換速度
// interval:圖片切換的時間間隔
speed:500,
interval:3000
});
})
</script>
到此為止,我們已經實現了輪播插件的封裝并且實現了復用,只需要動態(tài)的綁定不同的元素mycontainer(可以動態(tài)修改成自己的元素名字)即可實現復用。
4、如果需要修改容器的大小和圖片的大小,可以直接覆蓋樣式即可:
.mycontainer2{
width: 300px;
height:200px;
}
.mycontainer2 img{
width: 300px;
height:200px;
}
5、完成。恭喜你,你的輪播插件可以正常切換了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
javascript 動態(tài)改變onclick事件觸發(fā)函數代碼
javascript 動態(tài)改變onclick事件觸發(fā)函數代碼,需要的朋友可以參考下。2011-08-08
ES6新特性二:Iterator(遍歷器)和for-of循環(huán)詳解
這篇文章主要介紹了ES6新特性二:Iterator(遍歷器)和for-of循環(huán),結合實例形式分析了ES6中Iterator(遍歷器)和for-of循環(huán)遍歷操作的相關實現技巧與注意事項,需要的朋友可以參考下2017-04-04

