简体   繁体   中英

jQuery Waypoints handles the first element with a specified class only

I am trying to implement the jQuery Waypoints plugin to add an .active class to any element with a .foo class as it gets into the viewport:

<div class="foo"></div>
<div class="foo"></div>

var inview = new Waypoint.Inview({
  element: $('.foo')[0],
  entered: function(direction) {
    $(this.element).addClass("active");
  }
});

JS Fiddle: http://jsfiddle.net/g6mouxnd/

The example above will add the .active class to the first .foo container only. How do I make it work for the second and any subsequent .foo container as well?

You can loop over every .foo and create an Inview for each:

$('.foo').each(function() {
  new Waypoint.Inview({
    element: this,
    entered: function(direction) {
      $(this.element).addClass('active');
    }
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM