Udoro

Sticky shadow

Oct 3rd, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. // STICKY SHOW BOTTOM
  2. const el = document.querySelector(".product-sub-footer")
  3.  
  4.  
  5. function addShadow(x) {
  6. if (x.matches) { // If media query matches
  7. const observer = new IntersectionObserver(
  8. ([e]) => e.target.classList.toggle("sticky-shadow", e.intersectionRatio < 1),
  9. { threshold: [1] }
  10. );
  11.  
  12. observer.observe(el);
  13. }
  14. else {
  15. el.classList.remove("sticky-shadow");
  16. }
  17. }
  18.  
  19. var x = window.matchMedia("(min-width: 768px)")
  20. addShadow(x) // Call listener function at run time
  21. x.addListener(addShadow) // Attach listener function on state changes
  22.  
  23.  
  24.  
  25.  
  26. const els = document.querySelector(".product-sub-detail")
  27. const observers = new IntersectionObserver(
  28. ([e]) => e.target.classList.toggle("sticky-shadow", e.intersectionRatio < 1),
  29. { threshold: [1] }
  30. );
  31.  
  32. observers.observe(els);
  33.  
  34.  
  35. // STICKY SHOW TOP
  36. const nav = document.querySelector(".sticky-nav")
  37.  
  38.  
  39.  
  40. const observer = new IntersectionObserver(
  41.  
  42. entries => {
  43. entries.forEach(entry => {
  44. if(!entry.isIntersecting) {
  45.  
  46. entry.target.classList.add("sticky-shadow")
  47.  
  48. } else{
  49.  
  50. entry.target.classList.remove("sticky-shadow")
  51. }
  52.  
  53. })
  54.  
  55. }, {threshold: 0.9,}
  56.  
  57. )
  58.  
  59.  
  60. observer.observe(nav);
  61.  
  62.  
  63.  
  64.  
Add Comment
Please, Sign In to add comment