Last active
September 4, 2018 19:04
-
-
Save alexandreramosdev/7b165ecabd7a30cea08be6370db35d71 to your computer and use it in GitHub Desktop.
animação scroll
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
[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); | |
} |
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
<div class=".." data-anime="right"> | |
... | |
</div> | |
<section class=".." data-anime="left"> | |
... | |
</section> | |
<img class=".." data-anime="..."> | |
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
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