blob: 9f3e66c81ff892c658163da3af2c2b98450149fd [file] [log] [blame]
Unknownd2db0f12019-01-10 14:22:00 -05001/**
2 * animate-in.js 1.0.0
3 * Animate elements on entrance
4 *
5 * Usage:
6 *
7 * Make sure to add this to the <head> of your page to avoid flickering on load
8 * Based on https://andycaygill.github.io/scroll-entrance/
9 */
10(function() {
11
12 //Set up defaults
13 var duration = "1000";
14 var heightOffset = 100;
15
16
17 // document.write("<style id='temp-animate-in'>*[class^='animate-in'], *[class*=' animate-in'] {display:none}</style>")
18
19 function isElementVisible(elem) {
20
21 var rect = elem.getBoundingClientRect();
22
23 //Return true if any of the following conditions are met:
24 return (
25 // The top is in view: the top is more than 0 and less than the window height (the top of the element is in view)
26 ( (rect.top + heightOffset) >= 0 && (rect.top + heightOffset) <= window.innerHeight ) ||
27 // The bottom is in view: bottom position is greater than 0 and greater than the window height
28 ( (rect.bottom + heightOffset) >= 0 && (rect.bottom + heightOffset) <= window.innerHeight ) ||
29 // The top is above the viewport and the bottom is below the viewport
30 ( (rect.top + heightOffset) < 0 && (rect.bottom + heightOffset) > window.innerHeight )
31 )
32
33 }
34
35
36 function update() {
37 var nodes = document.querySelectorAll("*:not(.animate-in-done)[class^='animate-in'], *:not(.animate-in-done)[class*=' animate-in']")
38
39 for (var i = 0; i < nodes.length; i++) {
40 if (isElementVisible(nodes[i])) {
41 nodes[i].classList.remove("out-of-viewport")
42 nodes[i].classList.add("animate-in-done")
43 } else {
44 nodes[i].classList.add("out-of-viewport")
45 }
46 }
47 }
48
49 document.addEventListener("DOMContentLoaded", function(event) {
50 update()
51 // setTimeout(function() {
52 // document.querySelector("#temp-animate-in").remove()
53 // })
54 });
55
56 window.addEventListener("scroll", function() {
57 update()
58 })
59
60})();
61