functions.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Handles sticky header
  3. */
  4. ( function () {
  5. 'use strict';
  6. var stickyHeader = document.querySelector( '.sticky-wrapper' );
  7. var topBar = document.querySelector( '.top-bar' );
  8. var stickyHeaderOffset = parseFloat(
  9. ( getComputedStyle( topBar ).height || '0' ).replace( 'px', '' )
  10. );
  11. var nextFrame = null;
  12. if ( ! stickyHeader || ! topBar ) {
  13. return;
  14. }
  15. function stickyTime() {
  16. var windowWidth = document.documentElement.clientWidth;
  17. if ( window.pageYOffset >= stickyHeaderOffset && windowWidth >= 1100 ) {
  18. stickyHeader.classList.add( 'sticking' );
  19. var stuckHeader = document.querySelector( '.sticky-wrapper.sticking' );
  20. var stickyHeaderHeight = getComputedStyle( stuckHeader ).height;
  21. document.body.style.paddingTop = stickyHeaderHeight;
  22. topBar.style.visibility = 'hidden';
  23. } else {
  24. stickyHeader.classList.remove( 'sticking' );
  25. document.body.removeAttribute( 'style' );
  26. topBar.removeAttribute( 'style' );
  27. }
  28. nextFrame = null;
  29. }
  30. if ( document.readyState === 'complete' ) {
  31. stickyTime();
  32. } else {
  33. window.addEventListener( 'load', stickyTime );
  34. }
  35. document.addEventListener( 'scroll', function () {
  36. if ( window.requestAnimationFrame ) {
  37. nextFrame = nextFrame || requestAnimationFrame( stickyTime );
  38. } else {
  39. stickyTime();
  40. }
  41. } );
  42. } )();