Observe an element when a new class appears

Asked

Viewed 22 times

2

It would be these fixed frontend bars, the problem is that I can’t find how a class is inserted to put things in it.

Example:

<div class="main-menu">conteudo</div>

After a little scrolling the screen is inserted the class:

<div class="main-menu isStuck">conteudo</div>

And I need to watch when you put and when you remove this class to put some "clones" inside it.

  • After scrolling the screen, the div wins the class, ok! And when she will lose the class, when scrolling up?

  • @DVD This after scrolling again until the initial state it disappears

1 answer

1


Use the .on with scroll to observe, when scrolling the screen, whether or not the element has the class:

$(window).on('scroll', function(){
   if($('.main-menu').hasClass('isStuck')){
     console.log("tem a classe");
   }else{
     console.log("não tem a classe");
   }
});
  • This already occurs, and I can’t override the event, I need to detect when the class "isStuck" arises and some of the div

  • @Dexxtz I didn’t get it right. You can try to explain it better?

  • @Dexxtz Well, if I understand it, the div wins the class without being related to scrolling to the top of the screen, is that it? Scrolling can be anywhere on the screen?

  • It works like this, there is already an event that puts and removes the class from the screen scroll, I need an observer in that div, and know the moment that the class is inserted and removed, example at some point of scrolling this div will win a class that my condition will insert content, but I don’t know at what point this will occur, I can’t remake this scrolling event just detect bind type, Trigger these things

  • @Dexxtz I updated the answer. See if that’s it.

  • @Dexxtz If you already have a "scroll" observer, there is no problem in adding this to the code, it will work parallel to each other, separately.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.