Advertisement
afsarwebdev

JS-On-scroll-header-shrink and add class

Jul 29th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. // header shrink
  2. HTML:
  3. <div class="header" id="header-shrink">
  4. <h2>Header</h2>
  5. </div>
  6. //CSS:
  7. .header {
  8. height: 120px;
  9. background-color: orange;
  10. margin-bottom: 30px;
  11. display: table;
  12. width: 100%;
  13. position: fixed;
  14. top: 0;
  15. transition: 0.4s all;
  16. max-width: 1170px;
  17. margin: 0 auto;
  18. }
  19. .body_content {
  20. margin-top: 170px;
  21. }
  22. .header h2 {
  23. text-align: center;
  24. text-transform: uppercase;
  25. font-size: 50px;
  26. color: #fff;
  27. margin: 0px;
  28. display: table-cell;
  29. vertical-align: middle;
  30. }
  31. h2 {
  32. text-align: center;
  33. transition: 0.4s all;
  34. }
  35. .custom-shrink h2 {
  36. font-size: 30px;
  37. }
  38. //Javascript
  39.  
  40. var headerShrink, headerShrinkyPos;
  41. function headerShrink() {
  42. headerShrink = document.getElementById('header-shrink');
  43. headerShrinkyPos = window.pageYOffset;
  44. if(headerShrinkyPos > 150){
  45. headerShrink.style.height = "50px";
  46. headerShrink.classList.add('custom-shrink');
  47. } else {
  48. headerShrink.style.height = "120px";
  49. headerShrink.classList.remove('custom-shrink');
  50. }
  51. }
  52. window.addEventListener("scroll", headerShrink);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement