上一篇文章 案例|原生手寫一個輪播圖——漸隱漸顯版 還有不少不足,這裏要很是感謝大佬 csdoker給出的寶貴意見和指導🙏,因此筆者決定從新完善一下輪播圖的案例,打算作一個簡易版的左右輪播圖插件的封裝;javascript
封裝一款插件,首先第一件事情必定是分析,分析最後實現的效果和能夠支持的配置項css
寫插件的目的是方便後期的開發,並且須要讓更多人使用(開源)html
插件 組件 類庫 等這些東西封裝 基本上都要基於面向對象思想前端
- 面向對象的思想可以保證每個實例之間沒有關聯
- 面向對象的最主要目的就是:插件組件封裝
這一部分咱們能夠寫一點架子出來,用戶使用時直接CV過來,更換本身的圖片,配置本身的一些細節內容便可; 倉庫地址java
<!-- 輪播圖總體容器 -->
<div class="xiaozhima-container">
<!-- 全部圖片容器 -->
<div class="xiaozhima-wrapper">
<!-- 每一個圖片容器 -->
<div class="xiaozhima-slide">
<!-- 插入您的圖片 -->
<img src=" " alt="">
</div>
<div class="xiaozhima-slide">
<img src=" " alt="">
</div>
<div class="xiaozhima-slide">
<img src=" " alt="">
</div>
<div class="xiaozhima-slide">
<img src=" " alt="">
</div>
</div>
<!-- 分頁器容器 -->
<div class="xiaozhima-pagination"></div>
<!-- 左右按鈕容器 -->
<a href="javascript:;" class="xiaozhima-arrow-prev"></a>
<a href="javascript:;" class="xiaozhima-arrow-next"></a>
</div>
複製代碼
這一部分咱們也是寫一點架子出來,把咱們須要實現的內容配置好,細節樣式由用戶本身指定便可node
banner-plugin.css
的樣式表便可 ;倉庫地址 (你們也能夠本身建立一個CSS文件,把代碼直接粘貼進去便可);全部圖片容器的:git
每一個圖片容器的:github
左右按鈕:(北京圖片轉爲BASE64編碼)數組
插件中的樣式資源最好不要使用圖片(由於這樣須要用戶額外的導入,不利於插件的推廣和使用) => 須要使用圖片,咱們最好把圖片設置爲BASE64編碼app
一個把圖片轉爲BASE64編碼的在線網站:tool.css-js.com/base64.html
.xiaozhima-container {
/* * 須要用戶後期本身配置的樣式 * width * height */
position: relative;
box-sizing: border-box;
width: 100%;
overflow: hidden;
}
.xiaozhima-container .xiaozhima-wrapper {
/* * 須要在JS中動態計算的樣式 * width 根據SLIDE的個數(含克隆的)* CONTAINER容器的寬度 * transition 是在JS中設置的 * left 初始展現哪個SLIDE,LEFT值就應該是計算好的 */
display: flex;
box-sizing: border-box;
position: absolute;
top: 0;
height: 100%;
}
.xiaozhima-container .xiaozhima-wrapper .xiaozhima-slide {
/* * 須要用戶後期本身配置的樣式 * width 和CONTAINER的寬度保持一致(JS中處理) */
box-sizing: border-box;
height: 100%;
overflow: hidden;
}
.xiaozhima-container .xiaozhima-wrapper .xiaozhima-slide img {
display: block;
width: 100%;
height: 100%;
}
.xiaozhima-container .xiaozhima-pagination {
position: absolute;
bottom: 10px;
left: 50%;
z-index: 999;
transform: translateX(-50%);
padding: 3px 6px;
background: rgba(255, 255, 255, .3);
font-size: 0;
border-radius: 10px;
}
.xiaozhima-container .xiaozhima-pagination span {
display: inline-block;
margin: 0 6px;
width: 10px;
height: 10px;
border-radius: 50%;
background: lightblue;
cursor: pointer;
}
.xiaozhima-container .xiaozhima-pagination span.active {
background: lightcoral;
}
.xiaozhima-arrow-prev,
.xiaozhima-arrow-next {
display: none;
position: absolute;
z-index: 999;
top: 50%;
margin-top: -22.5px;
width: 30px;
height: 45px;
/* 插件中的樣式資源最好不要使用圖片(由於這樣須要用戶額外的導入,不利於插件的推廣和使用) => 須要使用圖片,咱們最好把圖片設置爲BASE64編碼 */
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAF8AAAA1CAYAAAAnIzfJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKTWlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVN3WJP3Fj7f92UPVkLY8LGXbIEAIiOsCMgQWaIQkgBhhBASQMWFiApWFBURnEhVxILVCkidiOKgKLhnQYqIWotVXDjuH9yntX167+3t+9f7vOec5/zOec8PgBESJpHmomoAOVKFPDrYH49PSMTJvYACFUjgBCAQ5svCZwXFAADwA3l4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HI8cfvM55PfGm/K3O233vuO+638e9H5ko/ED+UPPR+mPHp9BP9z7nfP78L/eE8/sl0p8zAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAEISURBVHja7NrLFYMwEENRUEtM/xW4J7PO0pgwsuepgBx8BcEfzuu6+vFhWmvnP343IpYbhw6SFvDBB5+ADz4BH3wCPvgEfPB/0lqzH6zbNeqtQUWEPX5EWBWgKvCOBagSvFsBqgbvVIAqwrsUoKrwDgWoMnx2AaoOn1mAgM8rQMDnFSDHxzF7C+Krm02Oj2MF+KEX7u4FZPy9Dk01dy0g6702vMjarYDMCcWj7YVdCsieyT3eWFu9AIcp9NSW8qoFuKxdpg9TVivAadH4yjHiKgW4rdZfO0Bf5QzXKXw6Aj74BHzwCfjgE/DBJ+CDTyZz9t5R4M4Hn4APPgEffAL+XrkBAAD//wMAxT+JY8GI0xgAAAAASUVORK5CYII=") no-repeat 0 0;
}
.xiaozhima-arrow-prev {
left: 0;
}
.xiaozhima-arrow-next {
right: 0;
background-position: -50px 0;
}
.xiaozhima-container:hover .xiaozhima-arrow-prev,
.xiaozhima-container:hover .xiaozhima-arrow-next {
display: block;
}
複製代碼
bannerPlugin
方法,可讓指定的容器實現出左右運動版的輪播圖效果;上面咱們說過了參數的處理方式有兩種,咱們這裏選擇基於對象健值對的方式處理;
這裏用到了咱們昨天鋪墊的深比較、循環迭代方法(深克隆 VS 淺克隆|深比較 VS 淺比較|回調函數),還有數據類型檢測的方法,文章JS中數據類型檢測四種方式的優缺點 的末尾提到過,我把他做爲工具包先放在這裏
倉庫地址 你們也能夠直接CV
/* 工具包 */
(function () {
// 數據類型檢測
let class2type = {};
["Boolean", "Number", "String", "Function", "Array", "Date", "RegExp", "Object", "Error", "Symbol"].forEach(item => {
class2type["[object " + item + "]"] = item.toLowerCase();
});
function toType(obj) {
if (obj == null) return obj + "";
return typeof obj === "object" || typeof obj === "function" ?
class2type[class2type.toString.call(obj)] || "object" :
typeof obj;
}
// 檢測是否爲函數
function isFunction(obj) {
return typeof obj === "function" && typeof obj.nodeType !== "number";
}
// 檢測window
function isWindow(obj) {
return obj != null && obj === obj.window;
}
// 數組/類數組
function isArrayLike(obj) {
var length = !!obj && "length" in obj && obj.length,
type = toType(obj);
if (isFunction(obj) || isWindow(obj)) return false;
return type === "array" || length === 0 || typeof length === "number" && length > 0 && (length - 1) in obj;
}
// 循環迭代
function _each(obj, callback, context = window) {
if (/^(ARRAY|OBJECT)$/i.test(obj.constructor)) {
obj = _cloneDeep(obj);
}
if (isArrayLike(obj)) {
for (let i = 0; i < obj.length; i++) {
let res = callback && callback.call(context, obj[i], i);
if (res === false) break;
if (res !== undefined) obj[i] = res;
}
} else {
for (let key in obj) {
if (!obj.hasOwnProperty(key)) break;
let res = callback && callback.call(context, obj[key], key);
if (res === false) break;
if (res !== undefined) obj[key] = res;
}
}
return obj;
}
// 深克隆
function _cloneDeep(obj) {
if (obj === null) return null;
if (typeof obj !== "object") return obj;
if (obj instanceof RegExp) return new RegExp(obj);
if (obj instanceof Date) return new Date(obj);
let cloneObj = new obj.constructor;
for (let key in obj) {
if (!obj.hasOwnProperty(key)) break;
cloneObj[key] = _cloneDeep(obj[key]);
}
return cloneObj;
}
// 深比較
function _assignDeep(obj1, obj2) {
let obj = _cloneDeep(obj1);
for (let key in obj2) {
if (!obj2.hasOwnProperty(key)) break;
let v2 = obj2[key],
v1 = obj[key];
if ((v1 !== null && typeof v1 === "object") && (v2 !== null && typeof v2 === "object")) {
obj[key] = _assignDeep(v1, v2);
continue;
}
obj[key] = v2;
}
return obj;
}
['_each', '_cloneDeep', '_assignDeep', 'toType', 'isFunction', 'isWindow', 'isArrayLike'].forEach(item => {
window[item] = eval(item);
});
})();
複製代碼
function bannerPlugin(container, options = {}) {
// 參數初始化:默認配置信息
let defaultParams = {
initialSlide: 0,
autoplay: 3000,
speed: 300,
pagination: {
el: '.xiaozhima-pagination',
clickable: true
},
navigation: {
nextEl: '.xiaozhima-arrow-next',
prevEl: '.xiaozhima-arrow-prev'
},
on: {
init() {},
transitionStart() {},
transitionEnd() {}
}
};
// 利用深比較,把用戶傳遞進來的參數和默認配置合併(達到替換默認配置的效果)
options = _assignDeep(defaultParams, options);
// 判斷用戶傳遞進來的第一個參數是否爲元素容器,支持直接傳遞標籤
typeof container === "string" ? container = document.querySelector(container) : null;
if (!container || container.nodeType !== 1) {
throw new TypeError('container must be an element!');
}
return new Banner(container, options);
}
複製代碼
class Banner {
constructor(container, options) {
// 把傳遞進來的信息都掛載到當前類的實例上
// 1.信息都做爲他的私有屬性(這樣每個實例之間互不影響)
// 2.掛載到實例上,之後在當前類的其它方法中,只要保證THIS是實例,均可以基於THIS.XXX獲取和操做
_each(options, (item, key) => {
this[key] = item;
});
this.container = container;
this.activeIndex = this.initialSlide;
}
}
複製代碼
computed() {
let {
container,
pagination,
navigation
} = this;
// 輪播圖
this.wrapper = container.querySelector('.xiaozhima-wrapper');
this.slidesTrue = container.querySelectorAll('.xiaozhima-slide');
// 克隆第一張到容器的末尾
this.wrapper.appendChild(this.slidesTrue[0].cloneNode(true));
this.slides = container.querySelectorAll('.xiaozhima-slide');
// 分頁器
this.paginationBox = null;
this.paginationList = null;
if (toType(pagination) === "object") {
let el = pagination.el;
if (el) {
this.paginationBox = container.querySelector(el);
// 建立SPAN
let str = ``;
_each(this.slidesTrue, item => {
str += `<span></span>`;
});
this.paginationBox.innerHTML = str;
this.paginationList = this.paginationBox.querySelectorAll('span');
}
}
// 左右切換
this.arrowPrev = null;
this.arrowNext = null;
if (toType(navigation) === "object") {
navigation.prevEl ? this.arrowPrev = container.querySelector(navigation.prevEl) : null;
navigation.nextEl ? this.arrowNext = container.querySelector(navigation.nextEl) : null;
}
// 控制元素的樣式(包含初始展現哪個)
this.changeWidth = parseFloat(getComputedStyle(container).width);
this.activeIndex = this.activeIndex < 0 ? 0 : (this.activeIndex > this.slides.length - 1 ? this.slides.length - 1 : this.activeIndex);
this.wrapper.style.width = `${this.changeWidth*this.slides.length}px`;
this.wrapper.style.transition = `left ${this.speed}ms`;
this.wrapper.style.left = `${-this.activeIndex*this.changeWidth}px`;
_each(this.slides, item => {
item.style.width = `${this.changeWidth}px`;
});
}
複製代碼
// 實現輪播圖切換
change(now = false) {
//now 傳值是當即切換,不傳是默認的有動畫切換
let {
wrapper,
speed,
activeIndex,
changeWidth,
on
} = this;
let isO = toType(on) === "object" ? true : false,
transitionStart = isO ? on.transitionStart : null,
transitionEnd = isO ? on.transitionEnd : null;
// 切換以前觸發的鉤子函數
!now && transitionStart ? transitionStart.call(this, this) : null;
// 若是傳了now 當即切換,則不須要有動畫
wrapper.style.transitionDuration = `${now?0:speed}ms`;
wrapper.style.left = `${-activeIndex*changeWidth}px`;
if (now) {
// 若是當即切換我須要讓上面先渲染一次,利用讀寫分離;
wrapper.offsetWidth;
}
// 切換以後觸發的鉤子函數
let fn = () => {
!now && transitionEnd ? transitionEnd.call(this, this) : null;
// 每一次都會從新監聽,因此監聽完須要把上一次監聽的移除掉
wrapper.removeEventListener('transitionend', fn);
};
wrapper.addEventListener('transitionend', fn);
}
複製代碼
// 自動輪播
autoMove() {
if (this.activeIndex === this.slides.length - 1) {
this.activeIndex = 0;
this.change(true);
}
this.activeIndex++;
this.change();
}
複製代碼
// 實現焦點對齊
autoFocus() {
let {
paginationList,
activeIndex,
slides
} = this;
if (!paginationList) return;
activeIndex === slides.length - 1 ? activeIndex = 0 : null;
_each(paginationList, (item, index) => {
if (index === activeIndex) {
item.className = 'active';
return;
}
item.className = '';
});
}
複製代碼
init() {
// THIS:當前類的實例
// 入口,在這裏控制代碼執行的邏輯順序
// 先基於它獲取元素後,才能夠獲取實例上的信息
this.computed();
let {
autoplay,
autoMove,
container,
pagination,
paginationList,
arrowNext,
arrowPrev
} = this;
// 控制是否自動切換
if (autoplay) {
this.autoTimer = setInterval(autoMove.bind(this), autoplay);
// 箭頭函數中的THIS是上下文中的,也就是實例
container.onmouseenter = () => clearInterval(this.autoTimer);
container.onmouseleave = () => this.autoTimer = setInterval(autoMove.bind(this), autoplay);
}
// 控制焦點切換
if (toType(pagination) === "object" && pagination.clickable === true && paginationList) {
_each(paginationList, (item, index) => {
item.onclick = () => {
let {
activeIndex,
slides
} = this;
if ((index === activeIndex) || (index === 0 && activeIndex === slides.length - 1)) return;
this.activeIndex = index;
this.change();
};
});
}
// 控制左右按鈕切換
arrowNext ? arrowNext.onclick = autoMove.bind(this) : null;
arrowPrev ? arrowPrev.onclick = () => {
if (this.activeIndex === 0) {
this.activeIndex = this.slides.length - 1;
this.change(true);
}
this.activeIndex--;
this.change();
} : null;
// 初始化完成,觸發INIT回調函數
if (toType(this.on) === "object" && isFunction(this.on.init)) {
// 把回調函數執行,讓方法中的THIS是實例,而且傳遞的第一個參數也是實例
this.on.init.call(this, this);
}
}
複製代碼
上面JS代碼彙總整理
banner-plugin.js
的JS文件,把代碼直接粘貼進去便可;(function () {
class Banner {
constructor(container, options) {
// 把傳遞進來的信息都掛載到當前類的實例上
// 1.信息都做爲他的私有屬性(這樣每個實例之間互不影響)
// 2.掛載到實例上,之後在當前類的其它方法中,只要保證THIS是實例,均可以基於THIS.XXX獲取和操做
_each(options, (item, key) => {
this[key] = item;
});
this.container = container;
this.activeIndex = this.initialSlide;
this.init();
}
init() {
// THIS:當前類的實例
// 入口,在這裏控制代碼執行的邏輯順序
// 先基於它獲取元素後,才能夠獲取實例上的信息
this.computed();
let {
autoplay,
autoMove,
container,
pagination,
paginationList,
arrowNext,
arrowPrev
} = this;
// 控制是否自動切換
if (autoplay) {
this.autoTimer = setInterval(autoMove.bind(this), autoplay);
// 箭頭函數中的THIS是上下文中的,也就是實例
container.onmouseenter = () => clearInterval(this.autoTimer);
container.onmouseleave = () => this.autoTimer = setInterval(autoMove.bind(this), autoplay);
}
// 控制焦點切換
if (toType(pagination) === "object" && pagination.clickable === true && paginationList) {
_each(paginationList, (item, index) => {
item.onclick = () => {
let {
activeIndex,
slides
} = this;
if ((index === activeIndex) || (index === 0 && activeIndex === slides.length - 1)) return;
this.activeIndex = index;
this.change();
};
});
}
// 控制左右按鈕切換
arrowNext ? arrowNext.onclick = autoMove.bind(this) : null;
arrowPrev ? arrowPrev.onclick = () => {
if (this.activeIndex === 0) {
this.activeIndex = this.slides.length - 1;
this.change(true);
}
this.activeIndex--;
this.change();
} : null;
// 初始化完成,觸發INIT回調函數
if (toType(this.on) === "object" && isFunction(this.on.init)) {
// 把回調函數執行,讓方法中的THIS是實例,而且傳遞的第一個參數也是實例
this.on.init.call(this, this);
}
}
// 計算結構和樣式
computed() {
let {
container,
pagination,
navigation
} = this;
// 輪播圖
this.wrapper = container.querySelector('.xiaozhima-wrapper');
this.slidesTrue = container.querySelectorAll('.xiaozhima-slide');
// 克隆第一張到容器的末尾
this.wrapper.appendChild(this.slidesTrue[0].cloneNode(true));
this.slides = container.querySelectorAll('.xiaozhima-slide');
// 分頁器
this.paginationBox = null;
this.paginationList = null;
if (toType(pagination) === "object") {
let el = pagination.el;
if (el) {
this.paginationBox = container.querySelector(el);
// 建立SPAN
let str = ``;
_each(this.slidesTrue, item => {
str += `<span></span>`;
});
this.paginationBox.innerHTML = str;
this.paginationList = this.paginationBox.querySelectorAll('span');
}
}
// 左右切換
this.arrowPrev = null;
this.arrowNext = null;
if (toType(navigation) === "object") {
navigation.prevEl ? this.arrowPrev = container.querySelector(navigation.prevEl) : null;
navigation.nextEl ? this.arrowNext = container.querySelector(navigation.nextEl) : null;
}
// 控制元素的樣式(包含初始展現哪個)
this.changeWidth = parseFloat(getComputedStyle(container).width);
this.activeIndex = this.activeIndex < 0 ? 0 : (this.activeIndex > this.slides.length - 1 ? this.slides.length - 1 : this.activeIndex);
this.wrapper.style.width = `${this.changeWidth * this.slides.length}px`;
this.wrapper.style.transition = `left ${this.speed}ms`;
this.wrapper.style.left = `${-this.activeIndex * this.changeWidth}px`;
_each(this.slides, item => {
item.style.width = `${this.changeWidth}px`;
});
this.autoFocus();
}
// 自動輪播
autoMove() {
if (this.activeIndex === this.slides.length - 1) {
this.activeIndex = 0;
this.change(true);
}
this.activeIndex++;
this.change();
}
// 實現輪播圖切換
change(now = false) {
//now 傳值是當即切換,不傳是有動畫切換
let {
wrapper,
speed,
activeIndex,
changeWidth,
on
} = this;
let isO = toType(on) === "object" ? true : false,
transitionStart = isO ? on.transitionStart : null,
transitionEnd = isO ? on.transitionEnd : null;
// 切換以前觸發的鉤子函數
!now && transitionStart ? transitionStart.call(this, this) : null;
// 若是傳了now 當即切換,則不須要有動畫
wrapper.style.transitionDuration = `${now ? 0 : speed}ms`;
wrapper.style.left = `${-activeIndex * changeWidth}px`;
if (now) {
// 若是當即切換我須要讓上面先渲染一次,利用讀寫分離;
wrapper.offsetWidth;
} else {
this.autoFocus();
}
// 切換以後觸發的鉤子函數
let fn = () => {
!now && transitionEnd ? transitionEnd.call(this, this) : null;
// 每一次都會從新監聽,因此監聽完須要把上一次監聽的移除掉
wrapper.removeEventListener('transitionend', fn);
};
wrapper.addEventListener('transitionend', fn);
}
// 實現焦點對齊
autoFocus() {
let {
paginationList,
activeIndex,
slides
} = this;
if (!paginationList) return;
activeIndex === slides.length - 1 ? activeIndex = 0 : null;
_each(paginationList, (item, index) => {
if (index === activeIndex) {
item.className = 'active';
return;
}
item.className = '';
});
}
}
function bannerPlugin(container, options = {}) {
let defaultParams = {
initialSlide: 0,
autoplay: 3000,
speed: 300,
pagination: {
el: '.xiaozhima-pagination',
clickable: true
},
navigation: {
nextEl: '.xiaozhima-arrow-next',
prevEl: '.xiaozhima-arrow-prev'
},
on: {
init() { },
transitionStart() { },
transitionEnd() { }
}
};
options = _assignDeep(defaultParams, options);
typeof container === "string" ? container = document.querySelector(container) : null;
if (!container || container.nodeType !== 1) {
throw new TypeError('container must be an element!');
}
return new Banner(container, options);
}
window.bannerPlugin = bannerPlugin;
})();
複製代碼
筆者也在前端這條路上不斷的學習和嘗試,文中內容有不對的地方,還請賜教,不勝感激🙏;
筆者想在掘金不斷成長,現階段目標升到3級,還望你們成全😄,不勝感激;