Skip to content

Instantly share code, notes, and snippets.

@alexandreramosdev
Last active September 4, 2018 19:04
Show Gist options
  • Save alexandreramosdev/7b165ecabd7a30cea08be6370db35d71 to your computer and use it in GitHub Desktop.
Save alexandreramosdev/7b165ecabd7a30cea08be6370db35d71 to your computer and use it in GitHub Desktop.
animação scroll
[data-anime]{
opacity: 0;
transition: all .3s;
}
[data-anime="left"]{
transform: translateX(-5rem);
}
[data-anime="right"]{
transform: translateX(5rem);
}
[data-anime="up"]{
transform: translateY(5rem);
}
[data-anime].animate{
opacity: 1;
transform: translate(0);
}
<div class=".." data-anime="right">
...
</div>
<section class=".." data-anime="left">
...
</section>
<img class=".." data-anime="...">
const debounce = function (func, wait, immediate) {
let timeout
return function (...args) {
const context = this
const later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
const callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
}
const target = document.querySelectorAll('[data-anime]')
function animeScroll () {
const windowTop = window.pageYOffset + ((window.innerHeight * 3) / 4)
target.forEach(element => {
if (windowTop > element.offsetTop) {
element.classList.add('animate')
} else {
element.classList.remove('animate')
}
})
}
animeScroll()
if (target.length) {
window.addEventListener('scroll', debounce(() => {
animeScroll()
}, 200))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment