revolution.extension.carousel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /********************************************
  2. * REVOLUTION 5.4.2 EXTENSION - CAROUSEL
  3. * @version: 1.2.1 (18.11.2016)
  4. * @requires jquery.themepunch.revolution.js
  5. * @author ThemePunch
  6. *********************************************/
  7. (function($) {
  8. "use strict";
  9. var _R = jQuery.fn.revolution,
  10. extension = { alias:"Carousel Min JS",
  11. name:"revolution.extensions.carousel.min.js",
  12. min_core: "5.3.0",
  13. version:"1.2.1"
  14. };
  15. ///////////////////////////////////////////
  16. // EXTENDED FUNCTIONS AVAILABLE GLOBAL //
  17. ///////////////////////////////////////////
  18. jQuery.extend(true,_R, {
  19. // CALCULATE CAROUSEL POSITIONS
  20. prepareCarousel : function(opt,a,direction,speed) {
  21. if (_R.compare_version(extension).check==="stop") return false;
  22. direction = opt.carousel.lastdirection = dircheck(direction,opt.carousel.lastdirection);
  23. setCarouselDefaults(opt);
  24. opt.carousel.slide_offset_target = getActiveCarouselOffset(opt);
  25. if (speed!==undefined) {
  26. animateCarousel(opt,direction,false,0);
  27. } else {
  28. if (a==undefined)
  29. _R.carouselToEvalPosition(opt,direction);
  30. else
  31. animateCarousel(opt,direction,false);
  32. }
  33. },
  34. // MOVE FORWARDS/BACKWARDS DEPENDING ON THE OFFSET TO GET CAROUSEL IN EVAL POSITION AGAIN
  35. carouselToEvalPosition : function(opt,direction) {
  36. var _ = opt.carousel;
  37. direction = _.lastdirection = dircheck(direction,_.lastdirection);
  38. var bb = _.horizontal_align==="center" ? ((_.wrapwidth/2-_.slide_width/2) - _.slide_globaloffset) / _.slide_width : (0 - _.slide_globaloffset) / _.slide_width,
  39. fi = _R.simp(bb,opt.slideamount,false);
  40. var cm = fi - Math.floor(fi),
  41. calc = 0,
  42. mc = -1 * (Math.ceil(fi) - fi),
  43. mf = -1 * (Math.floor(fi) - fi);
  44. calc = cm>=0.3 && direction==="left" || cm>=0.7 && direction==="right" ? mc : cm<0.3 && direction==="left" || cm<0.7 && direction==="right" ? mf : calc;
  45. calc = _.infinity==="off" ? fi<0 ? fi : bb>opt.slideamount-1 ? bb-(opt.slideamount-1) : calc : calc;
  46. _.slide_offset_target = calc * _.slide_width;
  47. // LONGER "SMASH" +/- 1 to Calc
  48. if (Math.abs(_.slide_offset_target) !==0)
  49. animateCarousel(opt,direction,true);
  50. else {
  51. _R.organiseCarousel(opt,direction);
  52. }
  53. },
  54. // ORGANISE THE CAROUSEL ELEMENTS IN POSITION AND TRANSFORMS
  55. organiseCarousel : function(opt,direction,setmaind,unli) {
  56. direction = direction === undefined || direction=="down" || direction=="up" || direction===null || jQuery.isEmptyObject(direction) ? "left" : direction;
  57. var _ = opt.carousel,
  58. slidepositions = new Array(),
  59. len = _.slides.length,
  60. leftlimit = _.horizontal_align ==="right" ? opt.width : 0;
  61. for (var i=0;i<len;i++) {
  62. var pos = (i * _.slide_width) + _.slide_offset;
  63. if (_.infinity==="on") {
  64. pos = pos>_.wrapwidth-_.inneroffset && direction=="right" ? _.slide_offset - ((_.slides.length-i)*_.slide_width) : pos;
  65. pos = pos<0-_.inneroffset-_.slide_width && direction=="left" ? pos + _.maxwidth : pos;
  66. }
  67. slidepositions[i] = pos;
  68. }
  69. var maxd = 999;
  70. // SECOND RUN FOR NEGATIVE ADJUSTMENETS
  71. if (_.slides)
  72. jQuery.each(_.slides,function(i,slide) {
  73. var pos = slidepositions[i];
  74. if (_.infinity==="on") {
  75. pos = pos>_.wrapwidth-_.inneroffset && direction==="left" ? slidepositions[0] - ((len-i)*_.slide_width) : pos;
  76. pos = pos<0-_.inneroffset-_.slide_width ? direction=="left" ? pos + _.maxwidth : direction==="right" ? slidepositions[len-1] + ((i+1)*_.slide_width) : pos : pos;
  77. }
  78. var tr= new Object();
  79. tr.left = pos + _.inneroffset;
  80. // CHCECK DISTANCES FROM THE CURRENT FAKE FOCUS POSITION
  81. var d = _.horizontal_align==="center" ? (Math.abs(_.wrapwidth/2) - (tr.left+_.slide_width/2))/_.slide_width : (_.inneroffset - tr.left)/_.slide_width,
  82. offsdir = d<0 ? -1:1,
  83. ha = _.horizontal_align==="center" ? 2 : 1;
  84. if ((setmaind && Math.abs(d)<maxd) || d===0) {
  85. maxd = Math.abs(d);
  86. _.focused = i;
  87. }
  88. tr.width =_.slide_width;
  89. tr.x = 0;
  90. tr.transformPerspective = 1200;
  91. tr.transformOrigin = "50% "+_.vertical_align;
  92. // SET VISIBILITY OF ELEMENT
  93. if (_.fadeout==="on")
  94. if (_.vary_fade==="on")
  95. tr.autoAlpha = 1-Math.abs(((1/Math.ceil(_.maxVisibleItems/ha))*d))
  96. else
  97. switch(_.horizontal_align) {
  98. case "center":
  99. tr.autoAlpha = Math.abs(d)<Math.ceil((_.maxVisibleItems/ha)-1) ? 1 : 1-(Math.abs(d)-Math.floor(Math.abs(d)));
  100. break;
  101. case "left":
  102. tr.autoAlpha = d<1 && d>0 ? 1-d : Math.abs(d)>_.maxVisibleItems-1 ? 1- (Math.abs(d)-(_.maxVisibleItems-1)) : 1;
  103. break;
  104. case "right":
  105. tr.autoAlpha = d>-1 && d<0 ? 1-Math.abs(d) : d>_.maxVisibleItems-1 ? 1- (Math.abs(d)-(_.maxVisibleItems-1)) : 1;
  106. break;
  107. }
  108. else
  109. tr.autoAlpha = Math.abs(d)<Math.ceil((_.maxVisibleItems/ha)) ? 1 : 0;
  110. // SET SCALE DOWNS
  111. if (_.minScale!==undefined && _.minScale >0) {
  112. if (_.vary_scale==="on") {
  113. tr.scale = 1- Math.abs(((_.minScale/100/Math.ceil(_.maxVisibleItems/ha))*d));
  114. var sx = (_.slide_width - (_.slide_width*tr.scale)) *Math.abs(d);
  115. } else {
  116. tr.scale = d>=1 || d<=-1 ? 1 - _.minScale/100 : (100-( _.minScale*Math.abs(d)))/100;
  117. var sx=(_.slide_width - (_.slide_width*(1 - _.minScale/100)))*Math.abs(d);
  118. }
  119. }
  120. // ROTATION FUNCTIONS
  121. if (_.maxRotation!==undefined && Math.abs(_.maxRotation)!=0) {
  122. if (_.vary_rotation ==="on") {
  123. tr.rotationY = Math.abs(_.maxRotation) - Math.abs((1-Math.abs(((1/Math.ceil(_.maxVisibleItems/ha))*d))) * _.maxRotation);
  124. tr.autoAlpha = Math.abs(tr.rotationY)>90 ? 0 : tr.autoAlpha;
  125. } else {
  126. tr.rotationY = d>=1 || d<=-1 ? _.maxRotation : Math.abs(d)*_.maxRotation;
  127. }
  128. tr.rotationY = d<0 ? tr.rotationY*-1 : tr.rotationY;
  129. }
  130. // SET SPACES BETWEEN ELEMENTS
  131. tr.x = (-1*_.space) * d;
  132. tr.left = Math.floor(tr.left);
  133. tr.x = Math.floor(tr.x);
  134. // ADD EXTRA SPACE ADJUSTEMENT IF COVER MODE IS SELECTED
  135. tr.scale !== undefined ? d<0 ? tr.x-sx :tr.x+sx : tr.x;
  136. // ZINDEX ADJUSTEMENT
  137. tr.zIndex = Math.round(100-Math.abs(d*5));
  138. // TRANSFORM STYLE
  139. tr.transformStyle = opt.parallax.type!="3D" && opt.parallax.type!="3d" ? "flat" : "preserve-3d";
  140. // ADJUST TRANSFORMATION OF SLIDE
  141. punchgs.TweenLite.set(slide,tr);
  142. });
  143. if (unli) {
  144. opt.c.find('.next-revslide').removeClass("next-revslide");
  145. jQuery(_.slides[_.focused]).addClass("next-revslide");
  146. opt.c.trigger("revolution.nextslide.waiting");
  147. }
  148. var ll = _.wrapwidth/2 - _.slide_offset ,
  149. rl = _.maxwidth+_.slide_offset-_.wrapwidth/2;
  150. }
  151. });
  152. /**************************************************
  153. - CAROUSEL FUNCTIONS -
  154. ***************************************************/
  155. var defineCarouselElements = function(opt) {
  156. var _ = opt.carousel;
  157. _.infbackup = _.infinity;
  158. _.maxVisiblebackup = _.maxVisibleItems;
  159. // SET DEFAULT OFFSETS TO 0
  160. _.slide_globaloffset = "none";
  161. _.slide_offset = 0;
  162. // SET UL REFERENCE
  163. _.wrap = opt.c.find('.tp-carousel-wrapper');
  164. // COLLECT SLIDES
  165. _.slides = opt.c.find('.tp-revslider-slidesli');
  166. // SET PERSPECTIVE IF ROTATION IS ADDED
  167. if (_.maxRotation!==0)
  168. if (opt.parallax.type!="3D" && opt.parallax.type!="3d")
  169. punchgs.TweenLite.set(_.wrap,{perspective:1200,transformStyle:"flat"});
  170. else
  171. punchgs.TweenLite.set(_.wrap,{perspective:1600,transformStyle:"preserve-3d"});
  172. if (_.border_radius!==undefined && parseInt(_.border_radius,0) >0) {
  173. punchgs.TweenLite.set(opt.c.find('.tp-revslider-slidesli'),{borderRadius:_.border_radius});
  174. }
  175. }
  176. var setCarouselDefaults = function(opt) {
  177. if (opt.bw===undefined) _R.setSize(opt);
  178. var _=opt.carousel,
  179. loff = _R.getHorizontalOffset(opt.c,"left"),
  180. roff = _R.getHorizontalOffset(opt.c,"right");
  181. // IF NO DEFAULTS HAS BEEN DEFINED YET
  182. if (_.wrap===undefined) defineCarouselElements(opt);
  183. // DEFAULT LI WIDTH SHOULD HAVE THE SAME WIDTH OF TH OPT WIDTH
  184. _.slide_width = _.stretch!=="on" ? opt.gridwidth[opt.curWinRange]*opt.bw : opt.c.width();
  185. // CALCULATE CAROUSEL WIDTH
  186. _.maxwidth = opt.slideamount*_.slide_width;
  187. if (_.maxVisiblebackup>_.slides.length+1)
  188. _.maxVisibleItems = _.slides.length+2;
  189. // SET MAXIMUM CAROUSEL WARPPER WIDTH (SHOULD BE AN ODD NUMBER)
  190. _.wrapwidth = (_.maxVisibleItems * _.slide_width) + ((_.maxVisibleItems - 1) * _.space);
  191. _.wrapwidth = opt.sliderLayout!="auto" ?
  192. _.wrapwidth>opt.c.closest('.tp-simpleresponsive').width() ? opt.c.closest('.tp-simpleresponsive').width() : _.wrapwidth :
  193. _.wrapwidth>opt.ul.width() ? opt.ul.width() : _.wrapwidth;
  194. // INFINITY MODIFICATIONS
  195. _.infinity = _.wrapwidth >=_.maxwidth ? "off" : _.infbackup;
  196. // SET POSITION OF WRAP CONTAINER
  197. _.wrapoffset = _.horizontal_align==="center" ? (opt.c.width()-roff - loff - _.wrapwidth)/2 : 0;
  198. _.wrapoffset = opt.sliderLayout!="auto" && opt.outernav ? 0 : _.wrapoffset < loff ? loff : _.wrapoffset;
  199. var ovf = "hidden";
  200. if ((opt.parallax.type=="3D" || opt.parallax.type=="3d"))
  201. ovf = "visible";
  202. if (_.horizontal_align==="right")
  203. punchgs.TweenLite.set(_.wrap,{left:"auto",right:_.wrapoffset+"px", width:_.wrapwidth, overflow:ovf});
  204. else
  205. punchgs.TweenLite.set(_.wrap,{right:"auto",left:_.wrapoffset+"px", width:_.wrapwidth, overflow:ovf});
  206. // INNER OFFSET FOR RTL
  207. _.inneroffset = _.horizontal_align==="right" ? _.wrapwidth - _.slide_width : 0;
  208. // THE REAL OFFSET OF THE WRAPPER
  209. _.realoffset = (Math.abs(_.wrap.position().left)); // + opt.c.width()/2);
  210. // THE SCREEN WIDTH/2
  211. _.windhalf = jQuery(window).width()/2;
  212. }
  213. // DIRECTION CHECK
  214. var dircheck = function(d,b) {
  215. return d===null || jQuery.isEmptyObject(d) ? b : d === undefined ? "right" : d;;
  216. }
  217. // ANIMATE THE CAROUSEL WITH OFFSETS
  218. var animateCarousel = function(opt,direction,nsae,speed) {
  219. var _ = opt.carousel;
  220. direction = _.lastdirection = dircheck(direction,_.lastdirection);
  221. var animobj = new Object(),
  222. _ease = nsae ? punchgs.Power2.easeOut : _.easing;
  223. animobj.from = 0;
  224. animobj.to = _.slide_offset_target;
  225. speed = speed===undefined ? _.speed/1000 : speed;
  226. speed = nsae ? 0.4 : speed;
  227. if (_.positionanim!==undefined)
  228. _.positionanim.pause();
  229. _.positionanim = punchgs.TweenLite.to(animobj,speed,{from:animobj.to,
  230. onUpdate:function() {
  231. _.slide_offset = _.slide_globaloffset + animobj.from;
  232. _.slide_offset = _R.simp(_.slide_offset , _.maxwidth);
  233. _R.organiseCarousel(opt,direction,false,false);
  234. },
  235. onComplete:function() {
  236. _.slide_globaloffset = _.infinity==="off" ? _.slide_globaloffset + _.slide_offset_target : _R.simp(_.slide_globaloffset + _.slide_offset_target, _.maxwidth);
  237. _.slide_offset = _R.simp(_.slide_offset , _.maxwidth);
  238. _R.organiseCarousel(opt,direction,false,true);
  239. var li = jQuery(opt.li[_.focused]);
  240. opt.c.find('.next-revslide').removeClass("next-revslide");
  241. if (nsae) _R.callingNewSlide(opt.c,li.data('index'));
  242. }, ease:_ease});
  243. }
  244. var breduc = function(a,m) {
  245. return Math.abs(a)>Math.abs(m) ? a>0 ? a - Math.abs(Math.floor(a/(m))*(m)) : a + Math.abs(Math.floor(a/(m))*(m)) : a;
  246. }
  247. // CAROUSEL INFINITY MODE, DOWN OR UP ANIMATION
  248. var getBestDirection = function(a,b,max) {
  249. var dira = b-a,max,
  250. dirb = (b-max) - a,max;
  251. dira = breduc(dira,max);
  252. dirb = breduc(dirb,max);
  253. return Math.abs(dira)>Math.abs(dirb) ? dirb : dira;
  254. }
  255. // GET OFFSETS BEFORE ANIMATION
  256. var getActiveCarouselOffset = function(opt) {
  257. var ret = 0,
  258. _ = opt.carousel;
  259. if (_.positionanim!==undefined) _.positionanim.kill();
  260. if (_.slide_globaloffset=="none")
  261. _.slide_globaloffset = ret = _.horizontal_align==="center" ? (_.wrapwidth/2-_.slide_width/2) : 0;
  262. else {
  263. _.slide_globaloffset = _.slide_offset;
  264. _.slide_offset = 0;
  265. var ci = opt.c.find('.processing-revslide').index(),
  266. fi = _.horizontal_align==="center" ? ((_.wrapwidth/2-_.slide_width/2) - _.slide_globaloffset) / _.slide_width : (0 - _.slide_globaloffset) / _.slide_width;
  267. fi = _R.simp(fi,opt.slideamount,false);
  268. ci = ci>=0 ? ci : opt.c.find('.active-revslide').index();
  269. ci = ci>=0 ? ci : 0;
  270. ret = _.infinity==="off" ? fi-ci : -getBestDirection(fi,ci,opt.slideamount);
  271. ret = ret * _.slide_width;
  272. }
  273. return ret;
  274. }
  275. })(jQuery);