1(function () { 2 'use strict'; 3 4 function initCarousel( root ) { 5 var track = root.querySelector( '.banext-carousel-track' ); 6 var viewport = root.querySelector( '.banext-carousel-viewport' ); 7 var slides = track ? Array.prototype.slice.call( track.children ) : []; 8 9 if ( ! track || ! viewport || ! slides.length ) { 10 return; 11 } 12 13 var prevBtn = root.querySelector( '.banext-prev' ); 14 var nextBtn = root.querySelector( '.banext-next' ); 15 var speed = parseInt( root.getAttribute( 'data-speed' ), 10 ) || 3000; 16 var index = 0; 17 var timer = null; 18 19 function slideWidth() { 20 return slides[ 0 ].getBoundingClientRect().width || 1; 21 } 22 23 function visibleCount() { 24 return Math.max( 1, Math.round( viewport.getBoundingClientRect().width / slideWidth() ) ); 25 } 26 27 function maxIndex() { 28 return Math.max( 0, slides.length - visibleCount() ); 29 } 30 31 function update() { 32 track.style.transform = 'translateX(-' + ( index * slideWidth() ) + 'px)'; 33 } 34 35 function goTo( target ) { 36 var max = maxIndex(); 37 if ( target < 0 ) { 38 target = max; 39 } else if ( target > max ) { 40 target = 0; 41 } 42 index = target; 43 update(); 44 } 45 46 function next() { 47 goTo( index + 1 ); 48 } 49 50 function prev() { 51 goTo( index - 1 ); 52 } 53 54 function play() { 55 stop(); 56 if ( slides.length > visibleCount() ) { 57 timer = window.setInterval( next, speed ); 58 } 59 } 60 61 function stop() { 62 if ( timer ) { 63 window.clearInterval( timer ); 64 timer = null; 65 } 66 } 67 68 if ( nextBtn ) { 69 nextBtn.addEventListener( 'click', function () { 70 next(); 71 play(); 72 } ); 73 } 74 75 if ( prevBtn ) { 76 prevBtn.addEventListener( 'click', function () { 77 prev(); 78 play(); 79 } ); 80 } 81 82 root.addEventListener( 'mouseenter', stop ); 83 root.addEventListener( 'mouseleave', play ); 84 85 var touchStartX = null; 86 87 viewport.addEventListener( 'touchstart', function ( e ) { 88 touchStartX = e.touches[ 0 ].clientX; 89 stop(); 90 }, { passive: true } ); 91 92 viewport.addEventListener( 'touchend', function ( e ) { 93 if ( null === touchStartX ) { 94 return; 95 } 96 var diff = e.changedTouches[ 0 ].clientX - touchStartX; 97 if ( Math.abs( diff ) > 40 ) { 98 diff < 0 ? next() : prev(); 99 } 100 touchStartX = null; 101 play(); 102 }, { passive: true } ); 103 104 window.addEventListener( 'resize', function () { 105 goTo( Math.min( index, maxIndex() ) ); 106 } ); 107 108 update(); 109 play(); 110 } 111 112 document.addEventListener( 'DOMContentLoaded', function () { 113 var carousels = document.querySelectorAll( '.banext-carousel' ); 114 carousels.forEach( initCarousel ); 115 } ); 116})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.