function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Swiper = function Swiper(ele) { var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, Swiper); var container = document.querySelector(ele), bannerWrap = container.querySelector('.wyz-swiper-wrap'), lis = container.querySelectorAll('.wyz-swiper-wrap li'), p1 = lis[0], p2 = lis[1], p3 = lis[2], p4 = lis[3], p5 = lis[4], p6 = lis[5], prev = null, next = null, maxNum = lis.length - 1, dots = {}, timer = 0, dotIndex = 0, isAddDot = false, startP, disX, disY, index; if (lis.length < 3) { throw "鍥剧墖鏈€锟?3锟?"; } if (config.navigator) { if (config.navigator.prev) { prev = document.querySelector(config.navigator.prev); } if (config.navigator.next) { next = document.querySelector(config.navigator.next); } } if (lis.length > 6) { p5 = lis[lis.length - 2]; p6 = lis[lis.length - 1]; for (var i = 0; i < lis.length; i++) { lis[i].classList.add("p4"); } p1.className = "p1"; p2.className = "p2"; p3.className = "p3"; p5.className = "p5"; p6.className = "p6"; bannerWrap.classList.add("swiper-6"); } else { for (var _i = 0; _i < lis.length; _i++) { lis[_i].classList.add('p' + (_i + 1)); } bannerWrap.classList.add('swiper-' + lis.length); } // 璁剧疆锟? if (config.pagination) { var p = container.querySelector(config.pagination); for (var _i2 = 0, len = lis.length; _i2 < len; _i2++) { var li = document.createElement("li"); p.appendChild(li); } dots = p.querySelectorAll('li'); dots[0].className = "active"; } bannerWrap.classList.add("transition"); // 鍥剧墖绉诲姩,dire鍒ゆ柇鏄惁婊戝姩锟? function move(dire) { var arrCls = []; // 淇濆瓨绫诲悕,鐢ㄦ潵璧嬶拷? dire = dire || false; dotIndex++; // 寰楀埌鎵€鏈夌被锟? for (var i = 0; i < lis.length; i++) { arrCls.push(lis[i].className); } // 寰幆璧嬪€肩被锟? for (var i = 0; i < lis.length; i++) { if (dire) { // 鍒ゆ柇宸︽粦 if (dire < 0) { index = i - 1; // 寰楀埌鏈€鍓嶉潰鍏冪礌鐨勪笂涓€锟?, 鑷繁鑴戣ˉ杩囩▼ if (index < 0) { index = maxNum; } } else if (dire > 0) { // 鍒ゆ柇鍙虫粦 index = i + 1; // 涓嬩竴锟? if (index > maxNum) { index = 0; } if (!isAddDot) { // 鐐瑰鑸笅涓€锟? dotIndex -= 2; isAddDot = true; } } } // 濡傛灉娌℃湁鍙傛暟,姝e父寰幆 if (!dire) { index = i - 1; if (index < 0) { index = maxNum; } } lis[i].className = arrCls[index]; } if (dotIndex > maxNum) { dotIndex = 0; } else if (dotIndex < 0) { dotIndex = maxNum; } isAddDot = false; // 鍒ゆ柇鏄惁鍔犺繃 setDotActive(dotIndex); } function setDotActive(index) { if (config.pagination) { for (var i = 0; i < dots.length; i++) { dots[i].className = ''; } dots[index].className = 'active'; } } function addTransition() { bannerWrap.classList.add('transition'); } function removeTransition() { bannerWrap.classList.remove('transition'); } function picTransform(x) { var p1 = container.querySelector('.p1'), p2 = container.querySelector('.p2'), p3 = container.querySelector('.p3'), p4 = container.querySelector('.p4'), p5 = container.querySelector('.p5'), p6 = container.querySelector('.p6'); var percentV2 = Math.floor(p2.offsetWidth * 0.15), // p2 鐨勭櫨鍒嗘瘮杞琾x percentV3 = Math.floor(p3.offsetWidth * 0.26); // p3 var number = lis.length > 6 ? 6 : lis.length; if (x < -7) { x = -7; } else if (x > 12) { x = 12; } p1.style.transform = 'translate3d(' + x + 'px,0,100px) scale(.5)'; p2.style.transform = 'translate3d(' + (percentV2 - x) + 'px,0,90px) scale(.4)'; switch (number) { case 3: p3.style.transform = 'translate3d(' + (-percentV2 - x) + 'px,0,90px) scale(.4)'; break; case 4: p4.style.transform = 'translate3d(' + (-percentV2 - x) + 'px,0,90px) scale(.4)'; break; case 5: p3.style.transform = 'translate3d(' + (percentV3 - x * 2) + 'px,0,80px) scale(.3)'; p4.style.transform = 'translate3d(' + (-percentV3 - x * 4) + 'px,0,80px) scale(.3)'; p5.style.transform = 'translate3d(' + (-percentV2 - x * 2) + 'px,0,90px) scale(.4)'; break; case 6: p3.style.transform = 'translate3d(' + (percentV3 - x * 2) + 'px,0,80px) scale(.3)'; p4.style.transform = 'translate3d(' + x + 'px,0,80px) scale(.3)'; p5.style.transform = 'translate3d(' + (-percentV3 - x * 4) + 'px,0,80px) scale(.3)'; p6.style.transform = 'translate3d(' + (-percentV2 - x * 2) + 'px,0,90px) scale(.4)'; break; } } function disabledPrev(e) { } timer = setInterval(move, 2000); bannerWrap.addEventListener('touchstart', function (e) { document.addEventListener("touchmove", disabledPrev(e), { passive: false }); // 绂佹婊戝姩鍚庨€€ removeTransition(); startP = e.changedTouches[0].clientX; clearInterval(timer); }, false); bannerWrap.addEventListener('touchmove', function (e) { var movex = e.changedTouches[0].clientX; disX = movex - startP; // 寰楀埌婊戝姩鐨勮窛锟? picTransform(disX / 20); // 璁╁浘鐗囩紦鎱㈡粦锟? }, false); bannerWrap.addEventListener('touchend', function (e) { document.removeEventListener("touchmove", disabledPrev(e), { passive: false }); // 绂佹婊戝姩鍚庨€€ addTransition(); var p1 = container.querySelector('.p1'), p2 = container.querySelector('.p2'), p3 = container.querySelector('.p3'), p4 = container.querySelector('.p4'), p5 = container.querySelector('.p5'), p6 = container.querySelector('.p6'); // 娓呴櫎琛屽唴鏍峰紡锛屽惁鍒欑被鍚嶇殑鏍峰紡涓嶇敓锟? if (p1) { p1.style.cssText = ''; } if (p2) { p2.style.cssText = ''; } if (p3) { p3.style.cssText = ''; } if (p4) { p4.style.cssText = ''; } if (p5) { p5.style.cssText = ''; } if (p6) { p6.style.cssText = ''; } // 闃叉鐐瑰嚮鏃朵篃浼氳Е锟? if (disX) { move(disX); disX = false; } timer = setInterval(move, 2000); }, false); bannerWrap.addEventListener("mouseenter", function () { clearInterval(timer); }); bannerWrap.addEventListener("mouseleave", function () { timer = setInterval(move, 2000); }); if (prev) { prev.onclick = function () { move(1); }; prev.onmousedown = function () { clearInterval(timer); }; prev.onmouseup = function () { timer = setInterval(move, 2000); }; } if (next) { next.onmousedown = function () { clearInterval(timer); }; next.onmouseup = function () { timer = setInterval(move, 2000); }; next.onclick = function () { move(-1); }; } };