If child class exist then add class to parent

Viewed 162

On page scroll new class is added to a child and if the class is added to the child then also a new class should be added to the parent.

This is my code (wrapped like this because of the Drupal framework):

(function($) {
Drupal.behaviors.myClasses = {
  attach: function (context, settings) {

 if($("#navigation").hasClass("stickynav-active")){
    $("#header-wrapper").addClass("sticky-active");
 }

  }
};
})(jQuery);

#header-wrapper is the parent. If I scroll down the stickynav-active is added (by some module) to #navigation so it starts existing after scrolling down, but my code doesn't add sticky-active to #header-wrapper?

2 Answers

I think it's because jQuery didn't listen to the event when you add the class stickynav-active to #navigation...

Why don't you try to add sticky-active to #header-wrapper in the same function that you add the class stickynav-active to #navigation?

As someone suggested to wrap it in a scroll event.

(function($) {
Drupal.behaviors.myClasses = {
  attach: function (context, settings) {


$(window).scroll(function() {
    if($("#navigation").hasClass("stickynav-active")){
         $("#header-wrapper").addClass("sticky-active");
    }
    else {
        $("#header-wrapper").removeClass("sticky-active");
    }
});


  }
};
})(jQuery);
Related