main.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. convert a cubic bezier value to a custom mina easing
  3. http://stackoverflow.com/questions/25265197/how-to-convert-a-cubic-bezier-value-to-a-custom-mina-easing-snap-svg
  4. */
  5. var duration = 300,
  6. delay = 300,
  7. epsilon = (1000 / 60 / duration) / 4,
  8. firstCustomMinaAnimation = bezier(.42, .03, .77, .63, epsilon),
  9. secondCustomMinaAnimation = bezier(.27, .5, .6, .99, epsilon);
  10. jQuery(document).ready(function () {
  11. //initialize the slider
  12. jQuery('.cd-slider-wrapper').each(function () {
  13. initSlider(jQuery(this));
  14. });
  15. });
  16. function initSlider(sliderWrapper) {
  17. //cache jQuery objects
  18. slider = sliderWrapper.find('.cd-slider'),
  19. sliderNavigation = sliderWrapper.find('.cd-slider-navigation').find('li'),
  20. svgCoverLayer = sliderWrapper.find('div.cd-svg-cover'),
  21. pathId = svgCoverLayer.find('path').attr('id'),
  22. svgPath = Snap('#' + pathId);
  23. //store path 'd' attribute values
  24. pathArray = [];
  25. pathArray[0] = svgCoverLayer.data('step1');
  26. pathArray[1] = svgCoverLayer.data('step6');
  27. pathArray[2] = svgCoverLayer.data('step2');
  28. pathArray[3] = svgCoverLayer.data('step7');
  29. pathArray[4] = svgCoverLayer.data('step3');
  30. pathArray[5] = svgCoverLayer.data('step8');
  31. pathArray[6] = svgCoverLayer.data('step4');
  32. pathArray[7] = svgCoverLayer.data('step9');
  33. pathArray[8] = svgCoverLayer.data('step5');
  34. pathArray[9] = svgCoverLayer.data('step10');
  35. //update visible slide when user clicks .cd-slider-navigation buttons
  36. sliderNavigation.on('click', function (event) {
  37. event.preventDefault();
  38. var selectedItem = jQuery(this);
  39. if (!selectedItem.hasClass('selected')) {
  40. // if it's not already selected
  41. var selectedSlidePosition = selectedItem.index(),
  42. selectedSlide = slider.children('li').eq(selectedSlidePosition),
  43. visibleSlide = slider.find('.visible'),
  44. visibleSlidePosition = visibleSlide.index(),
  45. direction = '';
  46. direction = (visibleSlidePosition < selectedSlidePosition) ? 'next' : 'prev';
  47. updateSlide(visibleSlide, selectedSlide, direction, svgCoverLayer, sliderNavigation, pathArray, svgPath);
  48. }
  49. });
  50. }
  51. function nextSlide() {
  52. var visibleSlide = slider.find('.visible');
  53. jQuery.post("?page=newsletter_main_welcome&noheader=1&action=save", jQuery("#tnp-welcome").serialize());
  54. var visibleSlidePosition = visibleSlide.index();
  55. var selectedSlide = slider.children('li').eq(visibleSlidePosition + 1);
  56. if (selectedSlide.hasClass("tnp-last-slide")) {
  57. jQuery(".cd-slider-navigation").hide();
  58. }
  59. updateSlide(visibleSlide, selectedSlide, "next", svgCoverLayer, sliderNavigation, pathArray, svgPath);
  60. }
  61. function prevSlide() {
  62. var visibleSlide = slider.find('.visible');
  63. var visibleSlidePosition = visibleSlide.index();
  64. var selectedSlide = slider.children('li').eq(visibleSlidePosition - 1);
  65. updateSlide(visibleSlide, selectedSlide, "prev", svgCoverLayer, sliderNavigation, pathArray, svgPath);
  66. if (selectedSlide.hasClass("tnp-first-slide")) {
  67. jQuery(".cd-slider-navigation a.tnp-welcome-prev").hide();
  68. }
  69. }
  70. function updateSlide(oldSlide, newSlide, direction, svgCoverLayer, sliderNavigation, paths, svgPath) {
  71. if (direction == 'next') {
  72. var path1 = paths[0],
  73. path2 = paths[2],
  74. path3 = paths[4];
  75. path4 = paths[6];
  76. path5 = paths[8];
  77. } else {
  78. var path1 = paths[1],
  79. path2 = paths[3],
  80. path3 = paths[5];
  81. path4 = paths[7];
  82. path5 = paths[9];
  83. }
  84. svgCoverLayer.addClass('is-animating');
  85. svgPath.attr('d', path1);
  86. svgPath.animate({'d': path2}, duration, firstCustomMinaAnimation, function () {
  87. svgPath.animate({'d': path3}, duration, secondCustomMinaAnimation, function () {
  88. oldSlide.removeClass('visible');
  89. newSlide.addClass('visible');
  90. updateNavSlide(newSlide, sliderNavigation);
  91. setTimeout(function () {
  92. svgPath.animate({'d': path4}, duration, firstCustomMinaAnimation, function () {
  93. svgPath.animate({'d': path5}, duration, secondCustomMinaAnimation, function () {
  94. svgCoverLayer.removeClass('is-animating');
  95. if (direction == "next") {
  96. jQuery(".cd-slider-navigation a.tnp-welcome-prev").show();
  97. }
  98. });
  99. });
  100. }, delay);
  101. });
  102. });
  103. }
  104. function updateNavSlide(actualSlide, sliderNavigation) {
  105. var position = actualSlide.index();
  106. sliderNavigation.removeClass('selected').eq(position).addClass('selected');
  107. }
  108. function bezier(x1, y1, x2, y2, epsilon) {
  109. //https://github.com/arian/cubic-bezier
  110. var curveX = function (t) {
  111. var v = 1 - t;
  112. return 3 * v * v * t * x1 + 3 * v * t * t * x2 + t * t * t;
  113. };
  114. var curveY = function (t) {
  115. var v = 1 - t;
  116. return 3 * v * v * t * y1 + 3 * v * t * t * y2 + t * t * t;
  117. };
  118. var derivativeCurveX = function (t) {
  119. var v = 1 - t;
  120. return 3 * (2 * (t - 1) * t + v * v) * x1 + 3 * (-t * t * t + 2 * v * t) * x2;
  121. };
  122. return function (t) {
  123. var x = t, t0, t1, t2, x2, d2, i;
  124. // First try a few iterations of Newton's method -- normally very fast.
  125. for (t2 = x, i = 0; i < 8; i++) {
  126. x2 = curveX(t2) - x;
  127. if (Math.abs(x2) < epsilon)
  128. return curveY(t2);
  129. d2 = derivativeCurveX(t2);
  130. if (Math.abs(d2) < 1e-6)
  131. break;
  132. t2 = t2 - x2 / d2;
  133. }
  134. t0 = 0, t1 = 1, t2 = x;
  135. if (t2 < t0)
  136. return curveY(t0);
  137. if (t2 > t1)
  138. return curveY(t1);
  139. // Fallback to the bisection method for reliability.
  140. while (t0 < t1) {
  141. x2 = curveX(t2);
  142. if (Math.abs(x2 - x) < epsilon)
  143. return curveY(t2);
  144. if (x > x2)
  145. t0 = t2;
  146. else
  147. t1 = t2;
  148. t2 = (t1 - t0) * .5 + t0;
  149. }
  150. // Failure
  151. return curveY(t2);
  152. };
  153. }