revolution.extension.carousel.js 12 KB

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