| 1 |
( function( $ ) { |
| 2 |
function LP_Advertisement_Slider( el, options ) { |
| 3 |
this.options = $.extend( {}, options || {} ); |
| 4 |
let $el = $( el ), |
| 5 |
$items = $el.find( '.slide-item' ), |
| 6 |
$controls = $( '<div class="slider-controls"><div class="prev-item"></div><div class="next-item"></div></div>' ), |
| 7 |
$wrapItems = $( '<div class="slider-items"></div>' ).append( $items ), |
| 8 |
itemIndex = 0, |
| 9 |
timer = null; |
| 10 |
|
| 11 |
function init() { |
| 12 |
createHTML(); |
| 13 |
bindEvents(); |
| 14 |
activeItem(); |
| 15 |
} |
| 16 |
|
| 17 |
function createHTML() { |
| 18 |
$el.append( $wrapItems ); |
| 19 |
$items.each( function() { |
| 20 |
$( this ).append( $controls.clone() ); |
| 21 |
} ); |
| 22 |
} |
| 23 |
|
| 24 |
function activeItem( index ) { |
| 25 |
index = index !== undefined ? index : itemIndex; |
| 26 |
const activeItem = $items.eq( index ); |
| 27 |
|
| 28 |
activeItem.show(); |
| 29 |
// A ha??? |
| 30 |
setTimeout( function() { |
| 31 |
activeItem.addClass( 'slide-active' ); |
| 32 |
}, 1 ); |
| 33 |
|
| 34 |
activeItem.siblings().removeClass( 'slide-active' ); |
| 35 |
|
| 36 |
timer && clearTimeout( timer ); |
| 37 |
timer = setTimeout( function() { |
| 38 |
activeItem.siblings().hide(); |
| 39 |
}, 500 ); |
| 40 |
} |
| 41 |
|
| 42 |
function nextItem() { |
| 43 |
if ( itemIndex < $items.length - 1 ) { |
| 44 |
itemIndex++; |
| 45 |
} else { |
| 46 |
itemIndex = 0; |
| 47 |
} |
| 48 |
activeItem( itemIndex ); |
| 49 |
} |
| 50 |
|
| 51 |
function prevItem() { |
| 52 |
if ( itemIndex > 0 ) { |
| 53 |
itemIndex--; |
| 54 |
} else { |
| 55 |
itemIndex = $items.length - 1; |
| 56 |
} |
| 57 |
activeItem( itemIndex ); |
| 58 |
} |
| 59 |
|
| 60 |
function bindEvents() { |
| 61 |
$el.on( 'click', '.next-item', nextItem ); |
| 62 |
$el.on( 'click', '.prev-item', prevItem ); |
| 63 |
} |
| 64 |
|
| 65 |
init(); |
| 66 |
} |
| 67 |
|
| 68 |
$.fn.LP( 'Advertisement', function( opts ) { |
| 69 |
return $.each( this, function() { |
| 70 |
let $slider = $( this ).data( 'LP_Advertisement_Slider' ); |
| 71 |
if ( ! $slider ) { |
| 72 |
$slider = new LP_Advertisement_Slider( this, opts ); |
| 73 |
$( this ).data( 'LP_Advertisement_Slider', $slider ); |
| 74 |
} |
| 75 |
return this; |
| 76 |
} ); |
| 77 |
} ); |
| 78 |
}( jQuery ) ); |
| 79 |
|
| 80 |
|