scrollreveal icon indicating copy to clipboard operation
scrollreveal copied to clipboard

Public method to remove ScrollReveal styles and event listeners

Open jgatjens opened this issue 10 years ago • 4 comments

I’m building a single-page application and Im using your awesome library for the homepage. When the user changes to another page, the DOM is removed by the router and I would like to ensure all events listener are removed as well to not have any memory leaks.

Example:

sr.destroy();

jgatjens avatar Jan 30 '16 17:01 jgatjens

Hey @jgatjens,

Since you don’t need to remove generated styles (because your DOM is destroyed by the router), it’s just a matter of removing the event listeners on all containers—so window and any custom nodes passed into config.container.

You can see where these are bound initially on line 333:

function _init(){
  if ( sr.supported() ){
    // Initial animate call triggers valid reveal animations on first load.
    // Subsequent animate calls are made inside the event handler.
    _animate();
    // Then we loop through all container nodes in the store and bind event
    // listeners to each.
    for ( var i = 0; i < sr.store.containers.length; i++ ){
      sr.store.containers[ i ].addEventListener( 'scroll', _handler );
      sr.store.containers[ i ].addEventListener( 'resize', _handler );
    }
    // Let’s also do a one-time binding of window event listeners.
    if ( !sr.initialized ){
      window.addEventListener( 'scroll', _handler );
      window.addEventListener( 'resize', _handler );
      sr.initialized = true;
    }
  }
  return sr;
}

For the time being, you can fork ScrollReveal and add a new destroy() method using similar code contained inside init()—but make sure to add it to the prototype so it’s exposed publicly.

Example:

ScrollReveal.prototype.destroy = function() {
  if ( sr.initialized ){
    for ( var i = 0; i < sr.store.containers.length; i++ ){
      sr.store.containers[ i ].removeEventListener( 'scroll', _handler );
      sr.store.containers[ i ].removeEventListener( 'resize', _handler );
    }
    window.removeEventListener( 'scroll', _handler );
    window.removeEventListener( 'resize', _handler );
    sr.initialized = false;
  }
  return sr;
}

This would allow you to call sr.destroy() after a user leaves your homepage, and should remove any lingering event listeners. Let me know how things work out. For now, I’ll icebox this for future a release—since anything I bake in will also need to go through the DOM and remove any generated styles.

Hope this helps! :bow:

jlmakes avatar Jan 30 '16 18:01 jlmakes

Thank you :100:

jgatjens avatar Jan 31 '16 22:01 jgatjens

Would also like this addition! Thank you.

flyarrowplane avatar Jul 19 '17 16:07 flyarrowplane

@relish27 This has been available in the ScrollReveal v4 beta for a few months.

Example using v4

sr.reveal('.triangles')
sr.reveal('.squares')
sr.reveal('.circles')

// we can remove styles/listeners from
// a specific element set like this:
sr.clean('.triangles')

// or remove them all, like this:
sr.destroy()

Check out what else is different in v4.

jlmakes avatar Jul 19 '17 16:07 jlmakes