1(function() { 2 var SOURCES = window.TEXT_VARIABLES.sources; 3 window.Lazyload.js(SOURCES.jquery, function() { 4 function swiper(options) { 5 var $window = $(window), $root = this, $swiperWrapper, $swiperSlides, $swiperButtonPrev, $swiperButtonNext, 6 initialSlide, animation, onChange, onChangeEnd, 7 rootWidth, count, preIndex, curIndex, translateX, CRITICAL_ANGLE = Math.PI / 3; 8 9 function setOptions(options) { 10 var _options = options || {}; 11 initialSlide = _options.initialSlide || 0; 12 animation = _options.animation === undefined && true; 13 onChange = onChange || _options.onChange; 14 onChangeEnd = onChangeEnd || _options.onChangeEnd; 15 } 16 17 function init() { 18 $swiperWrapper = $root.find('.swiper__wrapper'); 19 $swiperSlides = $root.find('.swiper__slide'); 20 $swiperButtonPrev = $root.find('.swiper__button--prev'); 21 $swiperButtonNext = $root.find('.swiper__button--next'); 22 animation && $swiperWrapper.addClass('swiper__wrapper--animation'); 23 calc(true); 24 } 25 26 function preCalc() { 27 rootWidth = $root.width(); 28 count = $swiperWrapper.children('.swiper__slide').length; 29 if (count < 2) { 30 $swiperButtonPrev.addClass('d-none'); 31 $swiperButtonNext.addClass('d-none'); 32 } 33 curIndex = initialSlide || 0; 34 translateX = getTranslateXFromCurIndex(); 35 } 36 37 var calc = (function() { 38 var preAnimation, $swiperSlide, $preSwiperSlide; 39 return function (needPreCalc, params) { 40 needPreCalc && preCalc(); 41 var _animation = (params && params.animation !== undefined) ? params.animation : animation; 42 if (preAnimation === undefined || preAnimation !== _animation) { 43 preAnimation = _animation ? $swiperWrapper.addClass('swiper__wrapper--animation') : 44 $swiperWrapper.removeClass('swiper__wrapper--animation'); 45 } 46 if (preIndex !== curIndex) { 47 ($preSwiperSlide = $swiperSlides.eq(preIndex)).removeClass('active'); 48 ($swiperSlide = $swiperSlides.eq(curIndex)).addClass('active'); 49 onChange && onChange(curIndex, $swiperSlides.eq(curIndex), $swiperSlide, $preSwiperSlide); 50 if (onChangeEnd) { 51 if (_animation) { 52 setTimeout(function() { 53 onChangeEnd(curIndex, $swiperSlides.eq(curIndex), $swiperSlide, $preSwiperSlide); 54 }, 400); 55 } else { 56 onChangeEnd(curIndex, $swiperSlides.eq(curIndex), $swiperSlide, $preSwiperSlide); 57 } 58 } 59 preIndex = curIndex; 60 } 61 $swiperWrapper.css('transform', 'translate(' + translateX + 'px, 0)'); 62 if (count > 1) { 63 if (curIndex <= 0) { 64 $swiperButtonPrev.addClass('disabled'); 65 } else { 66 $swiperButtonPrev.removeClass('disabled'); 67 } 68 if (curIndex >= count - 1) { 69 $swiperButtonNext.addClass('disabled'); 70 } else { 71 $swiperButtonNext.removeClass('disabled'); 72 } 73 } 74 }; 75 })(); 76 77 function getTranslateXFromCurIndex() { 78 return curIndex <= 0 ? 0 : - rootWidth * curIndex; 79 } 80 81 function moveToIndex(index ,params) { 82 preIndex = curIndex; 83 curIndex = index; 84 translateX = getTranslateXFromCurIndex(); 85 calc(false, params); 86 } 87 88 function move(type) { 89 var nextIndex = curIndex, unstableTranslateX; 90 if (type === 'prev') { 91 nextIndex > 0 && nextIndex--; 92 } else if (type === 'next') { 93 nextIndex < count - 1 && nextIndex++; 94 } 95 if (type === 'cur') { 96 moveToIndex(curIndex, { animation: true }); 97 return; 98 } 99 unstableTranslateX = translateX % rootWidth !== 0; 100 if (nextIndex !== curIndex || unstableTranslateX) { 101 unstableTranslateX ? moveToIndex(nextIndex, { animation: true }) : moveToIndex(nextIndex); 102 } 103 } 104 105 setOptions(options); 106 init(); 107 preIndex = curIndex; 108 109 $swiperButtonPrev.on('click', function(e) { 110 e.stopPropagation(); 111 move('prev'); 112 }); 113 $swiperButtonNext.on('click', function(e) { 114 e.stopPropagation(); 115 move('next'); 116 }); 117 $window.on('resize', function() { 118 calc(true, { animation: false }); 119 }); 120 121 (function() { 122 var pageX, pageY, velocityX, preTranslateX = translateX, timeStamp, touching; 123 function handleTouchstart(e) { 124 var point = e.touches ? e.touches[0] : e; 125 pageX = point.pageX; 126 pageY = point.pageY; 127 velocityX = 0; 128 preTranslateX = translateX; 129 } 130 function handleTouchmove(e) { 131 if (e.touches && e.touches.length > 1) { 132 return; 133 } 134 var point = e.touches ? e.touches[0] : e; 135 var deltaX = point.pageX - pageX; 136 var deltaY = point.pageY - pageY; 137 velocityX = deltaX / (e.timeStamp - timeStamp); 138 timeStamp = e.timeStamp; 139 if (e.cancelable && Math.abs(Math.atan(deltaY / deltaX)) < CRITICAL_ANGLE) { 140 touching = true; 141 translateX += deltaX; 142 calc(false, { animation: false }); 143 } 144 pageX = point.pageX; 145 pageY = point.pageY; 146 } 147 function handleTouchend() { 148 touching = false; 149 var deltaX = translateX - preTranslateX; 150 var distance = deltaX + velocityX * rootWidth; 151 if (Math.abs(distance) > rootWidth / 2) { 152 distance > 0 ? move('prev') : move('next'); 153 } else { 154 move('cur'); 155 } 156 } 157 $swiperWrapper.on('touchstart', handleTouchstart); 158 $swiperWrapper.on('touchmove', handleTouchmove); 159 $swiperWrapper.on('touchend', handleTouchend); 160 $swiperWrapper.on('touchcancel', handleTouchend); 161 162 (function() { 163 var pressing = false, moved = false; 164 $swiperWrapper.on('mousedown', function(e) { 165 pressing = true; handleTouchstart(e); 166 }); 167 $swiperWrapper.on('mousemove', function(e) { 168 pressing && (e.preventDefault(), moved = true, handleTouchmove(e)); 169 }); 170 $swiperWrapper.on('mouseup', function(e) { 171 pressing && (pressing = false, handleTouchend(e)); 172 }); 173 $swiperWrapper.on('mouseleave', function(e) { 174 pressing && (pressing = false, handleTouchend(e)); 175 }); 176 $swiperWrapper.on('click', function(e) { 177 moved && (e.stopPropagation(), moved = false); 178 }); 179 })(); 180 181 $root.on('touchmove', function(e) { 182 if (e.cancelable & touching) { 183 e.preventDefault(); 184 } 185 }); 186 })(); 187 188 return { 189 setOptions: setOptions, 190 previous: function(){ 191 move('prev'); 192 }, 193 next: function(){ 194 move('next'); 195 }, 196 refresh: function() { 197 calc(true, { animation: false }); 198 } 199 }; 200 } 201 $.fn.swiper = swiper; 202 }); 203})();