Javascript classList.add not working

I am having a problem with the javascript function of the List.add class. I am trying to add an “active” class to elements and apply the css style to these active classes. However, this doesn't seem to work, and it's hard for me to handle it. Can someone help me on this? Below is the current part of my HTML file and the css part that corresponds to this javascript.

Part:

<script> function debounce(func, wait = 20, immediate = true) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if (!immediate) func.apply(context, args); }; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; } const sliderImages = document.querySelectorAll('.slide-in'); function checkSlide(e) { sliderImages.forEach(sliderImage => { const slideInAt = (window.scrollY + window.innerHeight) - sliderImage.height / 2; const imageBottom = sliderImage.offsetTop + sliderImage.height; const isHalfShown = slideInAt > sliderImage.offsetTop; const isNotScrolledPast = window.scrollY < imageBottom; if(isHalfShown && isNotScrolledPast) { sliderImage.classList.add('active'); } else { sliderImage.classList.remove('active'); } }) } window.addEventListener('scroll', debounce(checkSlide)); </script> 

The part of CSS that corresponds to the above javascript:

 .slide-in { opacity: 0; transition:all .5s; } .align-left { float: left; /*margin-right: 20px;*/ } .align-right { float: right; /*margin-right: 20px;*/ } .align-left.slide-in { transform:translateX(-30%) scale(0.95); } .align-right.slide-in { transform:translateX(30%) scale(0.95); } .slide-in.active { opacity: 1; transform: translateX(0%) scale(1); } 

Please help me with this problem.

+5
source share
1 answer

There is nothing wrong with the functionality of classList, instead I made a few changes to the code, as shown below,

 <script> function debounce(func, wait , immediate) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if (!immediate) func.apply(context, args); }; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; }; const sliderImages = document.querySelectorAll('.slide-in'); function checkSlide(e) { sliderImages.forEach(sliderImage => { const slideInAt = (window.scrollY + window.innerHeight) - (sliderImage.clientHeight / 2); const imageBottom = sliderImage.offsetTop + sliderImage.clientHeight; const isHalfShown = slideInAt > sliderImage.offsetTop; const isNotScrolledPast = window.scrollY < imageBottom; if(isHalfShown && isNotScrolledPast) { sliderImage.classList.add('active'); } else { sliderImage.classList.remove('active'); } }) } var myEfficientFn = debounce(function() {checkSlide();}, 20,true); window.addEventListener("scroll", myEfficientFn); </script> 
  • I put the <script> tag inside the <body> .
  • In the checkSlide function, I replaced using the height property with clientHeight
  • And finally, I returned the debounce function, as shown in the last line of code, instead of calling the function, because when the code looked like this window.addEventListener('scroll', debounce(checkSlide)); , the debounce function called when the window loaded called the function was used. When we use the code window.addEventListener("scroll", function(){return debounce(checkSlide);}); , a function is assigned to this event and is called every time this event occurs.
+1
source

Source: https://habr.com/ru/post/1261892/


All Articles