Tan-007 /
myslideshow
| 1 | (function ($) { |
||
| 2 | 'use strict'; |
||
| 3 | |||
| 4 | class SlideShow { |
||
| 5 | |||
| 6 | constructor(context) { |
||
| 7 | // initialize variables |
||
| 8 | this.intervalId; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 9 | this.current = 0; |
||
| 10 | this.slides = context.find('.slide'); |
||
| 11 | this.indicators = context.find('.indicators li'); |
||
| 12 | this.num_of_slides = this.slides.length; |
||
| 13 | |||
| 14 | // display first slide on start |
||
| 15 | this.toggleActive(this.indicators.eq(this.current)); |
||
| 16 | this.slides.eq(this.current).css('transform-origin', 'left'); |
||
| 17 | this.slides.eq(0).css('transform', 'scaleX(1)'); |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * function: nextSlide |
||
| 22 | * Shows the next image slide on arrow click |
||
| 23 | * |
||
| 24 | * @since 1.0.3 |
||
| 25 | * @param args contains the object on which the operation is to be done |
||
| 26 | */ |
||
| 27 | nextSlide(object) { |
||
| 28 | |||
| 29 | var slides = object.slides; |
||
| 30 | var indicators = object.indicators; |
||
| 31 | var num_of_slides = object.num_of_slides; |
||
| 32 | // cant store current because it has to be changed everytime |
||
| 33 | |||
| 34 | object.toggleActive(indicators.eq(object.current)); |
||
| 35 | slides.eq(object.current).css('transform-origin', 'right'); |
||
| 36 | slides.eq(object.current).css('transform', 'scaleX(0)'); |
||
| 37 | |||
| 38 | object.current++; |
||
| 39 | |||
| 40 | if (object.current >= num_of_slides) { |
||
| 41 | object.current = 0; |
||
| 42 | } |
||
| 43 | |||
| 44 | object.toggleActive(indicators.eq(object.current)); |
||
| 45 | slides.eq(object.current).css('transform-origin', 'left'); |
||
| 46 | slides.eq(object.current).css('transform', 'scaleX(1)'); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * function: nextSlideHelper |
||
| 51 | * this function is required because the arguments passed through the interval |
||
| 52 | * function and a listener are different. This function implicitly calls the |
||
| 53 | * original nextSlide function after extracting the object from the args. |
||
| 54 | * |
||
| 55 | * @since 1.0.3 |
||
| 56 | * @param args contains the object on which the operation is to be done |
||
| 57 | */ |
||
| 58 | nextSlideHelper(args) { |
||
| 59 | var object = args.data.object; |
||
| 60 | object.nextSlide(object); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * function: previousSlide |
||
| 65 | * Shows the previous image slide on arrow click |
||
| 66 | * |
||
| 67 | * @since 1.0.3 |
||
| 68 | * @param args contains the object on which the operation is to be done |
||
| 69 | */ |
||
| 70 | previousSlide( args ) { |
||
| 71 | |||
| 72 | var object = args.data.object; |
||
| 73 | var slides = object.slides; |
||
| 74 | var indicators = object.indicators; |
||
| 75 | var num_of_slides = object.num_of_slides; |
||
| 76 | // cant store current because it has to be changed everytime |
||
| 77 | |||
| 78 | object.toggleActive(indicators.eq(object.current)); |
||
| 79 | slides.eq(object.current).css('transform-origin', 'left'); |
||
| 80 | slides.eq(object.current).css('transform', 'scaleX(0)'); |
||
| 81 | |||
| 82 | object.current--; |
||
| 83 | |||
| 84 | if (object.current < 0) { |
||
| 85 | object.current = num_of_slides - 1; |
||
| 86 | } |
||
| 87 | |||
| 88 | object.toggleActive(indicators.eq(object.current)); |
||
| 89 | slides.eq(object.current).css('transform-origin', 'right'); |
||
| 90 | slides.eq(object.current).css('transform', 'scaleX(1)'); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * function: nextSlide |
||
| 95 | * toggles the active class on the bottom slide indicators |
||
| 96 | * |
||
| 97 | * @since 1.0.3 |
||
| 98 | * @param indicator contains the object on which the operation is to be done |
||
| 99 | */ |
||
| 100 | toggleActive(indicator) { |
||
| 101 | indicator.toggleClass('active'); |
||
| 102 | } |
||
| 103 | }; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * create SlideShow objects for every container |
||
| 107 | */ |
||
| 108 | $(function () { |
||
| 109 | // bind slideshows for each container on page |
||
| 110 | $('.mss-slides-container').each(bindSlideShow); |
||
| 111 | }); |
||
| 112 | |||
| 113 | // core slideshow mechanism |
||
| 114 | function bindSlideShow() { |
||
| 115 | var slideShow = new SlideShow($(this)); |
||
| 116 | |||
| 117 | // setting listeners for arrows |
||
| 118 | $(this).find('.arrow.left').on('click', {object: slideShow }, slideShow.previousSlide); |
||
| 119 | $(this).find('.arrow.right').on('click', { object: slideShow }, slideShow.nextSlideHelper); |
||
| 120 | |||
| 121 | // setting the interval |
||
| 122 | slideShow.intervalId = setInterval(slideShow.nextSlide, 5000, slideShow); |
||
| 123 | |||
| 124 | // stop if hovering |
||
| 125 | $(this).hover(function () { |
||
| 126 | clearInterval(slideShow.intervalId); |
||
| 127 | }, function () { |
||
| 128 | if (slideShow.num_of_slides > 1) { |
||
| 129 | slideShow.intervalId = setInterval(slideShow.nextSlide, 5000, slideShow); |
||
| 130 | } |
||
| 131 | }); |
||
| 132 | |||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | })(jQuery); |
||
| 137 |