column-parallax.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ( function( v, undefined ) {
  2. 'use strict';
  3. var initialized = false;
  4. v.parallaxBackground = {
  5. /**
  6. * Loop through all rows with a parallax background,
  7. * load the image asynchronously,
  8. * and create the necessary elements
  9. *
  10. * Also bind resize/load events here
  11. */
  12. init: function() {
  13. this.rows = document.querySelectorAll( '.fl-row-bg-parallax' );
  14. for ( var i = 0; i < this.rows.length; i++ ) {
  15. var row = this.rows[ i ];
  16. var src = row.getAttribute( 'data-parallax-image' );
  17. if ( ! row.vamtamParallaxLoaded && src ) {
  18. var img = new Image();
  19. img.addEventListener( 'load', this.loadImageCallback );
  20. img.row = row;
  21. img.src = src;
  22. }
  23. }
  24. window.addEventListener( 'resize', window.VAMTAM.debounce( this.onresize, 100 ).bind( this ), false );
  25. window.addEventListener( 'load', window.VAMTAM.debounce( this.onresize, 100 ).bind( this ), false );
  26. this.onresize();
  27. },
  28. /**
  29. * Fired when the background image is loaded,
  30. * this creates the element holding the background
  31. */
  32. loadImageCallback: function( e ) {
  33. var row = e.target.row;
  34. var contentWrap = row.firstElementChild;
  35. var imageHolder = document.createElement( 'div' );
  36. imageHolder.classList.add( 'vamtam-parallax-bg' );
  37. Object.assign( imageHolder.style, {
  38. backgroundImage: 'url(' + e.target.src + ')',
  39. backgroundSize: 'cover',
  40. backgroundRepeat: 'repeat',
  41. position: 'absolute',
  42. top: '-300px',
  43. right: 0,
  44. bottom: '-300px',
  45. left: 0,
  46. 'will-change': 'transform',
  47. } );
  48. requestAnimationFrame( function() {
  49. row.vamtamParallaxLoaded = true;
  50. var content = contentWrap.querySelector( '.fl-node-content' );
  51. content.before( imageHolder );
  52. contentWrap.style.overflow = 'hidden';
  53. content.style.zIndex = 1;
  54. content.style.position = 'relative';
  55. } );
  56. },
  57. /**
  58. * Measure and store the offset for each row
  59. * This only needs to happen on resize/page load
  60. */
  61. onresize: function() {
  62. requestAnimationFrame( function() {
  63. var cpos = window.pageYOffset;
  64. for ( var i = 0; i < this.rows.length; i++ ) {
  65. this.rows[ i ].vamtamParallaxOffset = v.offset( this.rows[ i ].firstElementChild );
  66. }
  67. this.measure( cpos );
  68. this.mutate( cpos );
  69. }.bind( this ) );
  70. },
  71. measure: function() {
  72. },
  73. /**
  74. * Reposition the background elements.
  75. */
  76. mutate: function( cpos ) {
  77. for ( var i = 0; i < this.rows.length; i++ ) {
  78. if ( this.rows[ i ].vamtamParallaxLoaded ) {
  79. var speed = this.rows[ i ].getAttribute( 'data-parallax-speed' );
  80. var pos = - ( ( cpos - this.rows[ i ].vamtamParallaxOffset.top ) / speed );
  81. this.rows[ i ].firstElementChild.firstElementChild.style.transform = 'translateY(' + pos + 'px)';
  82. }
  83. }
  84. },
  85. };
  86. window.FLBuilderLayout && Object.assign( window.FLBuilderLayout, {
  87. /**
  88. * Monkey patches the built-in parallax with a better implementation
  89. */
  90. _initParallaxBackgrounds: function() {
  91. if ( ! initialized ) {
  92. initialized = true;
  93. // parallax should only be enabled if Beaver Builder is not active,
  94. // that is, only on pages which are not currently being edited
  95. if ( ! document.body.classList.contains( 'fl-builder-active' ) ) {
  96. v.addScrollHandler( v.parallaxBackground );
  97. } else {
  98. var rows = document.querySelectorAll( '.fl-row-bg-parallax' );
  99. for ( var i = 0; i < rows.length; i++ ) {
  100. var row = rows[ i ];
  101. var src = row.getAttribute( 'data-parallax-image' );
  102. Object.assign( row.style, {
  103. backgroundImage: 'url(' + src + ')',
  104. backgroundSize: 'cover',
  105. backgroundRepeat: 'repeat',
  106. } );
  107. }
  108. }
  109. }
  110. },
  111. _scrollParallaxBackgrounds: function() {
  112. // should only be called once after we remove the event listener
  113. jQuery( window ).off( 'scroll.fl-bg-parallax' );
  114. },
  115. } );
  116. } )( window.VAMTAM );