Last active
October 26, 2020 16:41
-
-
Save metatarz/0fda06c8288ad0fc9712d9f2f955c907 to your computer and use it in GitHub Desktop.
CSS reactive animations
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
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Reactive CSS Animations </title> | |
<style> | |
.long{ | |
background-color: #137b85; | |
height: 100vh; | |
width: 100vw; | |
} | |
#loading { | |
display: inline-block; | |
width: 50px; | |
height: 50px; | |
border: 3px solid rgba(255,255,255,.3); | |
border-radius: 50%; | |
border-top-color: #fff; | |
animation: spin 1s ease-in-out infinite; | |
animation-play-state: paused; | |
} | |
</style> | |
<script> | |
document.addEventListener("DOMContentLoaded", function() { | |
const smartAnimation= document.querySelector("#loading"); | |
if ("IntersectionObserver" in window) { | |
let smartAnimationObserver = new IntersectionObserver(function(entries, observer) { | |
entries.forEach(function(entry) { | |
if (entry.isIntersecting) { | |
entry.target.style.animationPlayState= "running" | |
console.log('running') | |
}else{ | |
entry.target.style.animationPlayState = "paused" | |
console.log('paused') | |
} | |
}); | |
}); | |
smartAnimationObserver.observe(smartAnimation) | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Reactive CSS Animation</h1> | |
<h4>Test your own implementation<a href="https://audits.digital">here</a></h4> | |
<section class="long"> | |
<div id="loading"></div> | |
</section> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test your reactive animations and much more with Digital Sustainability Audits