Created
          September 12, 2017 12:27 
        
      - 
      
- 
        Save chancesmith/c00876775c709023d6ab345ca212efbe to your computer and use it in GitHub Desktop. 
    Animate elements in and out on scroll
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // onScroll animation | |
| $(function() { | |
| var $window = $(window), | |
| isTouch = Modernizr.touch; | |
| if (isTouch) { $('.add-animation').addClass('animated'); } | |
| $window.on('scroll', revealAnimation); | |
| function revealAnimation() { | |
| // Showed... | |
| $(".add-animation:not(.animated)").each(function () { | |
| var $this = $(this), | |
| offsetTop = $this.offset().top, | |
| scrolled = $window.scrollTop(), | |
| win_height_padded = $window.height(); | |
| if (scrolled + win_height_padded > offsetTop) { | |
| $this.addClass('animated'); | |
| } | |
| }); | |
| // Hidden... | |
| $(".add-animation.animated").each(function (index) { | |
| var $this = $(this), | |
| offsetTop = $this.offset().top; | |
| scrolled = $window.scrollTop(), | |
| win_height_padded = $window.height() * 0.8; | |
| if (scrolled + win_height_padded < offsetTop) { | |
| $(this).removeClass('animated') | |
| } | |
| }); | |
| } | |
| revealAnimation(); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment