jQuery Waypoints Refresh with CSS Transition

送分小仙女□ 提交于 2019-12-06 02:37:18

You are absolutely on the right path in calling refresh after the class toggle. You do want the recalculation of trigger points that it provides.

The issue here is that the CSS transition happens asynchronously. You toggle the class and your transition starts, meanwhile your JavaScript has already moved on to calling refresh but your transition has only begun. What you want is to wait until the transition is completed until you call refresh.

CSS Transitions, besides their CSS properties, also give us a JavaScript event called transitionend. Here is how you might use it (untested) on your page.

First, I'd like to refactor your existing code a bit to make it easier to reason about. All of your waypoints do the same thing and your sections have common markup we can use. So we can make this waypoint call only once and let Waypoints loop through the elements itself. And this within the waypoint callback refers to the HTML element itself, so we'll make use of that:

jQuery('.section-wrapper').waypoint( function() {
  jQuery(this).find('.section-header').toggleClass('header-animate');
}, { offset: 300 });

Ok, now let's add the transitionend handler:

jQuery('.section-wrapper').waypoint( function() {
  jQuery(this).find('.section-header').toggleClass('header-animate');
}, { offset: 300 });

jQuery('.section-header').on('webkitTransitionEnd transitionend', function() {
  jQuery.waypoints('refresh');
});

That should get the job done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!