/** * @desc 灏佽杞挱鍥剧粍浠 * @author: Demigodliu * @date: 2020/06/13 09:44 **/ (function() { let _swiper = { containerWidth: 0, // 杞挱瀹瑰櫒瀹藉害 itemLength: 0, // 瀛愰」涓暟 itemTotalWidth: 0, // 瀛愰」鎬诲 nowItemIndex: 0, // 褰撳墠瀛愰」 speed: 5000, // 瀛愰」鍒囨崲閫熷害 prevAndNextBtnGroup: false, // 鏄惁闇€瑕佸墠鍚庢寜閽粍 pagination: false, // 鏄惁闇€瑕佸垎椤靛櫒 renderFlag: false, // 鍒囨崲瀛愰」鑺傛祦闃叉姈寮€鍏 autoTimer: null, // 鑷姩鎾斁鐨勫畾鏃跺櫒 /** * @desc 鍒濆鍖 * @author: Demigodliu * @date: 2020/06/13 09:46 **/ init() { // 鎺ユ敹鐢ㄦ埛鐨勮嚜瀹氫箟鍙傛暟閰嶇疆 if (typeof swiperConfig1 !== "undefined") { let { width, height, speed, needPrevAndNextBtnGroup, needPagination } = swiperConfig1; console.log(width); $(".swiper-container1").css({ "width": (+width || 800) + 'px', "height": (+height || 300) + 'px' }); this.speed = speed || 5000; this.prevAndNextBtnGroup = needPrevAndNextBtnGroup || false; this.pagination = needPagination || false; } // 鍒濆鍖栬疆鎾 this.initSwiperInfo(); if (this.prevAndNextBtnGroup) { this.initPrevAndNextClickFn(); } if (this.pagination) { this.initPagination(); } this.autoCarousel(); }, /** * @desc 鍒濆鍖栫敤鎴峰~鍏呭瓙椤圭殑淇℃伅 * @author: Demigodliu * @date: 2020/06/13 09:50 **/ initSwiperInfo() { let _container = $(".swiper-container1"), _containerWidth = _container.width(), _itemLength = $(".swiper-item1").length, _itemTotalWidth = _containerWidth * _itemLength; this.containerWidth = _containerWidth; this.itemLength = _itemLength; this.itemTotalWidth = _itemTotalWidth; console.log(_containerWidth); // 瀛愰」鍒濆鍖栦綅缃 let _item = $(".swiper-item1"); for (let i = 0; i < _itemLength; i++) { _item.eq(i).css("left", _containerWidth * i); } }, /** * @desc 鍒濆鍖栧墠杩涘悗閫€鎸夐挳缁勪簨浠 * @author: Demigodliu * @date: 2020/06/13 09:47 **/ initPrevAndNextClickFn() { $(".swiper-container1").append(`
`); $(".swiper-btn-group_prev1").unbind("click").bind("click", () => _swiper.prevItem()); $(".swiper-btn-group_next1").unbind("click").bind("click", () => _swiper.nextItem()); }, /** * @desc 鍒濆鍖栧垎椤靛櫒 * @author: Demigodliu * @date: 2020/06/13 13:41 **/ initPagination() { let { itemLength } = this; $(".swiper-container1").append(`
`); // 鍒嗛〉鍣ㄦ覆鏌 let _paginationText = ''; for (let i = 0; i < itemLength; i++) { _paginationText += `
`; } $(".swiper-pagination-item1").html(_paginationText); $(".swiper-pagination-item-item1").eq(0).addClass("swiper-pagination-item-item_active1"); this.paginationFn(); }, /** * @desc 鑷姩杞挱 * @author: Demigodliu * @date: 2020/06/13 11:03 **/ autoCarousel() { this.autoTimer = setInterval(() => { this.nextItem(); }, this.speed); }, /** * @desc 鍓嶄竴寮 * @author: Demigodliu * @date: 2020/06/13 09:52 **/ prevItem() { let { nowItemIndex, itemLength, renderFlag } = this; // 閬垮厤鐬棿澶氭鎿嶄綔 if (!renderFlag) { this.renderFlag = true; // 鏆傚仠鑷姩鎾斁 clearInterval(this.autoTimer); // 鍒ゆ柇鏄惁鏄疆鎾竟鐣 this.nowItemIndex = !nowItemIndex ? itemLength - 1 : --nowItemIndex; // 杞挱鏇存柊 this.render() } }, /** * @desc 鍚庝竴寮 * @author: Demigodliu * @date: 2020/06/13 09:52 **/ nextItem() { let { nowItemIndex, itemLength, renderFlag } = this; // 閬垮厤鐬棿澶氭鎿嶄綔 if (!renderFlag) { this.renderFlag = true; // 鏆傚仠鑷姩鎾斁 clearInterval(this.autoTimer); // 鍒ゆ柇鏄惁鏄疆鎾竟鐣 this.nowItemIndex = nowItemIndex === itemLength - 1 ? 0 : ++nowItemIndex; // 杞挱鏇存柊 this.render() } }, /** * @desc 鍒嗛〉鍣ㄧ偣鍑 * @author: Demigodliu * @date: 2020/06/13 13:14 **/ paginationFn() { let that = this; $(".swiper-pagination-item-item1").unbind("click").bind("click", function() { let idx = $(this).index(); if (!that.renderFlag) { that.renderFlag = true; that.nowItemIndex = idx; // 鏆傚仠鑷姩鎾斁 clearInterval(that.autoTimer); that.render(); } }); }, /** * @desc 娓叉煋杞挱 * @author: Demigodliu * @date: 2020/06/13 09:58 **/ render() { let { containerWidth, nowItemIndex } = this; // 鎵ц閲嶇粯 $(".swiper-wrapper1").css("left", -containerWidth * nowItemIndex); // 鍒嗛〉鍣ㄥ悓姝 $(".swiper-pagination-item-item1").removeClass("swiper-pagination-item-item_active1").eq(nowItemIndex).addClass("swiper-pagination-item-item_active1"); setTimeout(() => { this.renderFlag = false; // 鎭㈠鑷姩鎾斁 this.autoCarousel(); }, 500); } }; $(() => _swiper.init()); }());