Created
October 17, 2018 12:58
-
-
Save FellowshipAgency/832b8909f46e2583bd28aa4319c30ad4 to your computer and use it in GitHub Desktop.
Intersection Observer
This file contains 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
// Include intersection-observer-polyfill.js for older browsers | |
// https://github.com/w3c/IntersectionObserver/tree/master/polyfill | |
// JS: | |
// Get scroll direction | |
var lastScrollTop = 0, delta = 5; | |
var lastScrollDirection = null; | |
$(window).scroll(function(event){ | |
var st = $(this).scrollTop(); | |
if(Math.abs(lastScrollTop - st) <= delta) | |
return; | |
if (st > lastScrollTop){ | |
// downscroll code | |
lastScrollDirection = "down"; | |
} else { | |
// upscroll code | |
lastScrollDirection = "up"; | |
} | |
lastScrollTop = st; | |
}); | |
// Start IntersectionObserver | |
const animatedElements = document.querySelectorAll('.animate-me'); | |
observer = new IntersectionObserver((entries) => { | |
entries.forEach(entry => { | |
if (entry.intersectionRatio > 0) { | |
entry.target.classList.add('in-view'); | |
entry.target.classList.remove('not-in-view'); | |
} | |
}); | |
}); | |
animatedElements.forEach(animatedElement => { | |
observer.observe(animatedElement); | |
}); | |
// end IntersectionObserver | |
// SCSS: | |
/// Transitions | |
@mixin transition-on-scroll-initial { | |
@include media(">sm") { | |
transition: all 1s; | |
transform: translate(0,50px); | |
opacity: 0; | |
} | |
} | |
@mixin transition-on-scroll-end { | |
@include media(">sm") { | |
transition: all 1s; | |
transform: translate(0,0); | |
opacity: 1; | |
} | |
} | |
html.js .not-in-view { | |
@include transition-on-scroll-initial; | |
} | |
.in-view { | |
@include transition-on-scroll-end; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment