Guest User

Untitled

a guest
May 27th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. var ImageSlide = new Class({
  2. Implements: [Options, Events],
  3.  
  4. options: {
  5. autoplay: true,
  6. effect: 'fade',
  7. duration: 1000,
  8. imagesClass : 'images',
  9. frameClass : 'frame',
  10. interval: 5000,
  11. width: 883,
  12. height: 360
  13. },
  14.  
  15. initialize: function(container, options) {
  16. this.container = $(container);
  17. this.setOptions(options);
  18.  
  19. // Create containers for the images, the frame, and the info box but don't insert it yet to the DOM
  20. this.imagesContainer = new Element('div', {'class': this.options.imagesClass});
  21. this.frameContainer = new Element('div', {'class': this.options.frameClass});
  22.  
  23. // Create global variables
  24. this.images = new Array();
  25.  
  26. this.currentImage = 0;
  27. this.previousImage = 0;
  28.  
  29. this.start();
  30. this.prepareNav();
  31. },
  32.  
  33. start: function() {
  34.  
  35. // Get all the elements that will be shown in the slideshow. Default is anchors with the image inside. But IMG is also acceptable
  36. this.container.getChildren().each(function(e){
  37. this.imagesContainer.grab(this.prepareImage(e));
  38. }, this);
  39.  
  40. // True if the transition animation is running
  41. this.transitioning = false;
  42.  
  43. // Insert the containers inside the main container
  44. this.container.grab(this.frameContainer);
  45. this.container.grab(this.imagesContainer);
  46.  
  47. // Display the first image
  48. this.images[0].setStyles({opacity: 1});
  49.  
  50. // Add the autoplay events
  51. this.addEvent('complete', function(){
  52. if (this.options.autoplay) {
  53. this.transition = this.startEffect.delay(this.options.interval, this);
  54. };
  55. this.transitioning = false;
  56. }.bind(this));
  57.  
  58. this.addEvent('start', function(){
  59. this.transitioning = true;
  60.  
  61. // Clear any time triggered calls
  62. $clear(this.transition);
  63. }.bind(this));
  64.  
  65. // Play the slideshow
  66. if (this.options.autoplay) {
  67. this.startEffect.delay(this.options.interval, this);
  68. };
  69. },
  70.  
  71. prepareImage: function(e){
  72.  
  73. var info = {};
  74.  
  75. if (e.tagName == 'A') {
  76. var image = $(e).getFirst('img');
  77. info.url = $(e).get('href');
  78. }else {
  79. var image = $(e);
  80. info.url = $empty;
  81. };
  82.  
  83. info.title = image.get('alt'),
  84. info.description = image.get('title');
  85.  
  86. image.store('info', info);
  87.  
  88. this.prepareEffect(image);
  89.  
  90. this.images.push(image);
  91.  
  92. return image;
  93. },
  94.  
  95. prepareEffect: function(e) {
  96. var options = {
  97. duration: this.options.duration,
  98. onComplete: function() {
  99. this.fireEvent('complete');
  100. }.bind(this)
  101. };
  102.  
  103. switch(this.options.effect) {
  104. default:
  105. e.setStyles({opacity: 0, position: 'absolute', left: 0, top: 0});
  106. options.property = 'opacity';
  107. e.fx = new Fx.Tween(e, options);
  108. break;
  109. }
  110. },
  111.  
  112. startEffect: function() {
  113.  
  114. if (this.transitioning) {
  115. return false;
  116. };
  117.  
  118. this.currentImage++;
  119.  
  120. if (this.currentImage >= this.images.length) {
  121. this.currentImage = 0;
  122. }
  123.  
  124. var current = this.images[this.currentImage];
  125. var previous = this.images[this.previousImage];
  126.  
  127. this.endEffect(previous);
  128.  
  129. switch(this.options.effect) {
  130. default:
  131. current.fx.start(1);
  132. this.fireEvent('start');
  133. break;
  134. }
  135.  
  136. this.previousImage = this.currentImage;
  137.  
  138. this.fireEvent('next', this);
  139. },
  140.  
  141. endEffect: function(e) {
  142. switch(this.options.effect) {
  143. default:
  144. e.get('tween', {property: 'opacity', duration: this.options.duration}).start(0);
  145. break;
  146. }
  147. },
  148.  
  149. getCurImage: function() {
  150. return this.images[this.currentImage];
  151. },
  152.  
  153. getNextImage: function() {
  154. var next = this.currentImage + 1;
  155.  
  156. if (next >= this.images.length) {
  157. next = 0;
  158. }
  159.  
  160. return this.images[next];
  161. },
  162.  
  163. getPrevImage: function() {
  164. var prev = this.currentImage - 1;
  165.  
  166. if (prev <= 0) {
  167. prev = this.images.length - 1;
  168. }
  169.  
  170. return this.images[prev];
  171. },
  172.  
  173. getCurInfo: function() {
  174. return this.getCurImage().retrieve('info');
  175. },
  176.  
  177. prepareNav: function() {
  178. var next = new Element('a', {
  179. 'class': 'next',
  180. 'text': this.getNextImage().retrieve('info').title,
  181. 'href': '#',
  182. 'styles' :{
  183. 'z-index': Number(this.frameContainer.getStyle('z-index')) + 1
  184. },
  185. 'events': {
  186. 'click': function(e) {
  187. if (this.transitioning) {
  188. return false;
  189. };
  190.  
  191. this.startEffect();
  192. return false;
  193. }.bind(this)
  194. }
  195. });
  196.  
  197. var prev = new Element('a', {
  198. 'class': 'prev',
  199. 'text': this.getPrevImage().retrieve('info').title,
  200. 'href': '#',
  201. 'styles' :{
  202. 'z-index': Number(this.frameContainer.getStyle('z-index')) + 1
  203. },
  204. 'events': {
  205. 'click': function(e) {
  206. if (this.transitioning) {
  207. return false;
  208. };
  209.  
  210. if (this.currentImage == 0) {
  211. this.currentImage = this.images.length - 2;
  212. this.startEffect();
  213. return false;
  214. };
  215.  
  216. this.currentImage = this.currentImage - 2;
  217. this.startEffect();
  218. return false;
  219. }.bind(this)
  220. }
  221. });
  222.  
  223. this.container.adopt(next, prev);
  224. }
  225. });
Add Comment
Please, Sign In to add comment