Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript classList.add is not working

I am having a problem with classList.add javascript function. I am trying to add "active" class onto elements and apply css style for those active classes. However, it doesn't seem to work and I am having hard time with it. Could anyone help me with this issue? Below is the current part from my HTML file and 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>

CSS part that corresponds to 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 issue.

like image 366
Tae Avatar asked Dec 26 '16 04:12

Tae


People also ask

How do you add two classes to a classList add?

To add multiple classes to an element, select the element and pass multiple classes to the classList. add() method, e.g. box. classList. add('bg-blue', 'text-white') .

Can you add multiple classes with classList add?

classList property The classList property has add() and remove() methods that allow passing multiple classes as arguments. Let's say we have a button with id value of button . To add multiple classes, you'll need to pass each class as a separate parameter to the add method.

Does ie11 support classList?

IE 11 doesn't support assigning to classList · Issue #3808 · Fyrd/caniuse · GitHub.

Why can't I add a class to an element?

If the specified class already exist, the class will not be added. contains(class) Returns a Boolean value, indicating whether an element has the specified class name. Possible values: true - the element contains the specified class name. false - the element does not contain the specified class name.

How to add CSS class to HTML element using JavaScript?

There are 3 ways to add CSS class using javascript. 1. Using classList property The classList property of the HTML element returns a live representation of the element's class attribute known as DOMTokenList. The DOMTokenList is read-only but it provides add () methods to add classes to the element. Select the HTML element.

What is the use of classlist in HTML?

Definition and Usage. The classList property returns the class name (s) of an element, as a DOMTokenList object. This property is useful to add, remove and toggle CSS classes on an element. The classList property is read-only, however, you can modify it by using the add () and remove () methods.

How do I add a class to a list in CSS?

The classList property is read-only, but you can use add () and remove () methods to add or remove CSS classes. The classList property is useful both to add, remove and toggle CSS classes on an element. The number of classes in the list.


1 Answers

There is nothing wrong with the classList functionality instead i have made a few changes in 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 had placed the <script> tag inside the <body> tag .
  • In the checkSlide function i have replace the height property usage by the clientHeight
  • And finally i have returned the debounce function as seen in the last line of the code ,instead of calling the function because when the code was like this window.addEventListener('scroll', debounce(checkSlide)); the debounce function got called on load of the window the call of the function was used.When we use the code window.addEventListener("scroll", function(){return debounce(checkSlide);}); the function gets assigned to that event and gets called every time that event happens.
like image 189
GraveyardQueen Avatar answered Oct 01 '22 12:10

GraveyardQueen