Advertisement
Guest User

Untitled

a guest
May 26th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. /**
  2. * Makes some sections appear to scroll at different speeds than others
  3. * @requires requestAnimationFrame
  4. * @requires getSupportedPropertyName
  5. * @returns window.Parallax object
  6. * @author jack@kratedesign.com, updated 6.30.14
  7. * API:
  8. * .add(el) - add [el] to the array of elements being parallaxed
  9. * .setAll(array) - set multiple elements and initialize Parallax
  10. * .init() - start parallax effects on all els added by 'set'/'setAll' and/or 'fix'
  11. * .stop() - stop effects without clearing the element array
  12. * .destroy() - stop effects and clear the element array
  13. * .debug() - turn on console logging for events
  14. * .quiet() - turn off debugging
  15. * .isActive() - check whether Parallax is running
  16. *
  17. */
  18. Parallax = (function() {
  19.  
  20. 'use strict';
  21.  
  22. /* Some defaults and initial/static vars: */
  23. var latestKnownScrollY = 0,
  24. transforms = ["webkitTransform", "transform","msTransform", "mozTransform", "oTransform"],
  25. transformProp = getSupportedPropertyName(transforms),
  26. pxElements = [],
  27. active = false,
  28. ticking = false,
  29. debugging = false,
  30. defaultSpeed = 0.3;
  31.  
  32.  
  33. /* Here's where the actual math happens to translate the sections */
  34. function calcParallax(scrollPosition, rate, el, offset) {
  35. var adjustment = -scrollPosition*rate + (100*offset),
  36. cutoff = 0;
  37.  
  38. if ( adjustment <= cutoff ) {
  39. el.style[transformProp] = "translateY(" + adjustment + "px) translateZ(0px)";
  40. }
  41. }
  42.  
  43.  
  44. function getSpeed(elem) {
  45. if( elem.getAttribute('data-speed') != null ) {
  46. return elem.getAttribute('data-speed');
  47. }
  48. else {
  49. return defaultSpeed;
  50. }
  51. }
  52.  
  53. /* Set up our event listener */
  54. function bindListeners() {
  55. window.addEventListener('scroll', onScroll, false);
  56. active = true;
  57.  
  58. if( debugging ) {
  59. console.log('Parallax started');
  60. }
  61. }
  62.  
  63.  
  64. /* onScroll is our event handler and hooks into our rAF call */
  65. function onScroll() {
  66. active = true;
  67. latestKnownScrollY = getScrollPosition();
  68. requestTick();
  69. }
  70.  
  71.  
  72. /* If we've scrolled, wait till it's convenient for the browser, then update */
  73. function requestTick() {
  74. if( !ticking ) {
  75. requestAnimationFrame(update);
  76. }
  77. ticking = true;
  78. }
  79.  
  80.  
  81. /* It's convenient now? Okay! */
  82. function update() {
  83. ticking = false;
  84. var currentScrollY = latestKnownScrollY;
  85.  
  86. for ( var i = 0; i < pxElements.length; i++ ) {
  87. var el = pxElements[i],
  88. speed = getSpeed(el);
  89. calcParallax( currentScrollY, speed, el, i );
  90. }
  91. }
  92.  
  93.  
  94. /* Add one element to an existing parallax */
  95. function add(ele) {
  96. pxElements.push(ele);
  97. }
  98.  
  99.  
  100. /* Add a collection, then kick off the effect */
  101. function setAll(arr) {
  102. pxElements.length = 0;
  103. for ( var ind = 0; ind < arr.length; ind++ ) {
  104. pxElements[ind] = arr[ind];
  105. }
  106. if( debugging ) {
  107. window.setTimeout(
  108. function() { console.log(pxElements); }, 10);
  109. }
  110. init();
  111. };
  112.  
  113.  
  114.  
  115. /* Print out some useful console logs */
  116. function debug() {
  117. debugging = true;
  118. var msg = [];
  119. if( !active ) {
  120. msg.push('Parallax is not active, use Parallax.init() to restart it.');
  121. }
  122. else {
  123. msg.push('Parallax is active on ' + pxElements.length + ' elements.');
  124. }
  125.  
  126. for( var k = 0; k < msg.length; k++ ) {
  127. var message = msg[k];
  128. console.log(message);
  129. }
  130. }
  131.  
  132.  
  133. /* Or don't. */
  134. function quiet() {
  135. debugging = false;
  136. }
  137.  
  138.  
  139. /* Another debug function; check to see if we're running */
  140. function isActive() {
  141. return active;
  142. }
  143.  
  144.  
  145. /* Get everything started */
  146. function init() {
  147. bindListeners();
  148. }
  149.  
  150.  
  151. /* Pause the effect (can be undone) */
  152. function stop() {
  153. window.removeEventListener('scroll', onScroll, false);
  154. if( debugging ) {
  155. console.log('Parallax stopped, use Parallax.init() to resume');
  156. }
  157. active = false;
  158. }
  159.  
  160.  
  161. /* Destroy the instance (needs to be re-set and invoked after this) */
  162. function destroy() {
  163. for( var p = 0; p < pxElements.length; p++ ) {
  164. pxElements[p].style[transformProp] = 'translateZ(0px)';
  165. }
  166. pxElements.length = 0;
  167. window.removeEventListener('scroll', onScroll, false);
  168. if( debugging ) {
  169. console.log('Parallax removed.');
  170. }
  171. active = false;
  172. }
  173.  
  174.  
  175.  
  176. return {
  177. init: init,
  178. add: add,
  179. setAll: setAll,
  180. isActive: isActive,
  181. debug: debug,
  182. quiet: quiet,
  183. stop: stop,
  184. destroy: destroy
  185. }
  186.  
  187. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement