Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var pluginName = 'carousel',
  4. defaults = {
  5. next: '.slider-nav-next',
  6. prev: '.slider-nav-prev',
  7. item: '.slider-item',
  8. dots: false,
  9. dotClass: '.slider-dot',
  10. autoplay: false,
  11. autoplayTime: 3000
  12. };
  13.  
  14. function carousel(element, options) {
  15. this.$document = $(document);
  16. this.$window = $(window);
  17. this.$element = $(element);
  18. this.options = $.extend({}, defaults, options);
  19. this.init();
  20. }
  21.  
  22. carousel.prototype.init = function () {
  23. this.setup();
  24. this.attachEventHandlers();
  25. this.update();
  26. };
  27.  
  28. carousel.prototype.setup = function (argument) {
  29. this.$slides = this.$element.find(this.options.item);
  30. this.count = this.$slides.length;
  31. this.index = 0;
  32.  
  33. this.$next = $(this.options.next);
  34. this.$prev = $(this.options.prev);
  35.  
  36. this.$canvas = $(document.createElement('div'));
  37. this.$canvas.addClass('slider-canvas').appendTo(this.$element);
  38. this.$slides.appendTo(this.$canvas);
  39.  
  40. this.$dots = $(this.options.dots);
  41. this.$dots.length && this.createDots();
  42. };
  43.  
  44. carousel.prototype.createDots = function () {
  45. var dots = [];
  46. for (var i = 0; i < this.count; i += 1) {
  47. dots[i] = '<span data-index="' + i + '" class="' + this.options.dotClass + '"></span>';
  48. }
  49. this.$dots.append(dots);
  50. };
  51.  
  52. carousel.prototype.attachEventHandlers = function () {
  53. this.$element.on('prev.slider', this.prev.bind(this));
  54. this.$document.on('click', this.options.prev (function (e) {
  55. this.$element.trigger('prev.slider');
  56. }).bind(this));
  57.  
  58. this.$element.on('next.slider', this.next.bind(this));
  59. this.$document.on('click', this.options.next (function (e) {
  60. this.$element.trigger('next.slider');
  61. }).bind(this));
  62.  
  63. this.$element.on('update.slider', this.update.bind(this));
  64. this.$window.on('resize load'(function (e) {
  65. this.$element.trigger('update.slider');
  66. }).bind(this));
  67.  
  68. this.$element.on('jump.slider', this.jump.bind(this));
  69. this.$document.on('click'('.' + this.options.dotClass)(function (e) {
  70. var index = parseInt($(e.target).attr('data-index'));
  71. this.$element.trigger('jump.slider', index);
  72. }).bind(this));
  73.  
  74. this.$element.on('autoplay.slider', this.autoplay.bind(this));
  75. this.$element.on('autoplayOn.slider', this.autoplayOn.bind(this));
  76. this.$element.on('autoplayOff.slider', this.autoplayOff.bind(this));
  77. this.$element.bind('prev.slider next.slider jump.slider', this.autoplay.bind(this));
  78. this.options.autoplay && this.$element.trigger('autoplayOn.slider');
  79. };
  80.  
  81. carousel.prototype.next = function (e) {
  82. this.index = (this.index + 1) % this.count;
  83. this.slide();
  84. };
  85.  
  86. carousel.prototype.prev = function (e) {
  87. this.index = Math.abs(this.index - 1 + this.count) % this.count;
  88. this.slide();
  89. };
  90.  
  91. carousel.prototype.jump = function (e, index) {
  92. this.index = index % this.count;
  93. this.slide();
  94. };
  95.  
  96. carousel.prototype.autoplayOn = function (argument) {
  97. this.options.autoplay = true;
  98. this.$element.trigger('autoplay.slider');
  99. };
  100.  
  101. carousel.prototype.autoplayOff = function () {
  102. this.autoplayClear();
  103. this.options.autoplay = false;
  104. };
  105.  
  106. carousel.prototype.autoplay = function (argument) {
  107. this.autoplayClear();
  108. if (this.options.autoplay) {
  109. this.autoplayId = setTimeout($(function () {
  110. this.$element.trigger('next.slider');
  111. this.$element.trigger('autoplay.slider');
  112. }).bind(this), this.options.autoplayTime);
  113. }
  114. };
  115.  
  116. carousel.prototype.autoplayClear = function () {
  117. this.autoplayId && clearTimeout(this.autoplayId);
  118. };
  119.  
  120. carousel.prototype.slide = function (index) {
  121. undefined === index && (index = this.index);
  122. var position = index * this.width * -1;
  123. this.$canvas.css({
  124. 'transform': 'translate3d(' + position + 'px, 0, 0)'
  125. });
  126. this.updateCssClass();
  127. };
  128.  
  129. carousel.prototype.update = function () {
  130. this.width = this.$element.width();
  131. this.$canvas.width(this.width * this.count);
  132. this.$slides.width(this.width);
  133. this.slide();
  134. };
  135.  
  136. carousel.prototype.updateCssClass = function () {
  137. this.$slides
  138. .removeClass('active')
  139. .eq(this.index)
  140. .addClass('active');
  141.  
  142. this.$dots
  143. .find('.' + this.options.dotClass)
  144. .removeClass('active')
  145. .eq(this.index)
  146. .addClass('active');
  147. };
  148.  
  149. $.fn[pluginName] = function (options) {
  150. return this.each(function () {
  151. !$.data(this, pluginName) && $.data(this, pluginName, new slider(this, options));
  152. });
  153. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement