Created
June 17, 2019 20:32
-
-
Save davidlagace/40467c149b00a88a3836a1635529b8a0 to your computer and use it in GitHub Desktop.
Detection in viewport element to animate.
This file contains hidden or 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
| /* ***************************************************************************** | |
| * | |
| * Global animation setup | |
| * | |
| * ***************************************************************************** */ | |
| @keyframes fade-in-viewport { | |
| 0% { | |
| opacity: 0 | |
| } | |
| 100% { | |
| opacity: 1; | |
| } | |
| } | |
| @keyframes fadeslide-in-viewport { | |
| 0% { | |
| transform: translateY(2rem); | |
| opacity: 0 | |
| } | |
| 80% { | |
| opacity: 1; | |
| } | |
| 100% { | |
| transform: translateY(0rem); | |
| opacity: 1; | |
| } | |
| } | |
| @keyframes slide-up-in-viewport { | |
| 0% { | |
| transform: translateY(1rem); | |
| } | |
| 100% { | |
| transform: translateY(0); | |
| } | |
| } | |
| // --------------------------------------------- | |
| [data-animate] { | |
| opacity: 0; | |
| transition: none; | |
| //will-change: transform, opacity; | |
| animation-delay: 0s; | |
| animation-duration: .8s; | |
| animation-timing-function: ease-out; | |
| animation-fill-mode: forwards; | |
| // default anim | |
| &.fadeslide-in { | |
| //opacity: 0; | |
| animation-name: fadeslide-in-viewport; | |
| animation-fill-mode: forwards; | |
| } | |
| &.fade-in { | |
| //opacity: 0; | |
| animation-name: fade-in-viewport; | |
| animation-fill-mode: forwards; | |
| } | |
| &.slide-up { | |
| animation-name: slide-up-in-viewport; | |
| animation-fill-mode: forwards; | |
| opacity: 1; | |
| } | |
| // create delay in siblings animation | |
| $i: 2; | |
| @while ($i < 11) { | |
| [data-animate-row] > &:nth-child(#{$i}) { | |
| animation-delay: ($i - 1) * .1s; | |
| } | |
| $i: $i+1; | |
| } | |
| &.a--already-animated { | |
| opacity: 1!important; | |
| } | |
| } |
This file contains hidden or 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
| import $ from 'jquery'; | |
| import {isInViewport} from '../utils/window'; | |
| $(function () { | |
| const animateObjs = $('[data-animate]'); | |
| const onScrollAnimation = () => { | |
| animateObjs.each(function (i, e) { | |
| let elem = e; | |
| let animationClassStyle = e.dataset.animate || 'fadeslide-in'; | |
| if (isInViewport(elem)) { | |
| if(!elem.classList.contains(animationClassStyle)){ | |
| elem.classList.add(animationClassStyle); | |
| elem.addEventListener("webkitAnimationEnd", () => { | |
| elem.classList.add('a--already-animated'); | |
| }); | |
| } | |
| } | |
| }); | |
| }; | |
| onScrollAnimation(); | |
| window.addEventListener('scroll', onScrollAnimation, false); | |
| }); |
This file contains hidden or 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
| import $ from "jquery"; | |
| window.addEventListener("scroll", function(event) { | |
| currentWindowScroll.top = this.scrollY; | |
| currentWindowScroll.left = this.scrollX; | |
| }, false); | |
| export const currentWindowScroll = { | |
| top: 0, | |
| left: 0, | |
| }; | |
| export let _window = { | |
| freezePos:0, | |
| freeze(){ | |
| this.freezePos = currentWindowScroll.top; | |
| $('body').css("top", `-${this.freezePos}px`); | |
| $('body').addClass('js-vui--window-freeze'); | |
| }, | |
| unfreeze(){ | |
| $('body').removeClass('js-vui--window-freeze'); | |
| if(this.freezePos != 0){ | |
| window.scrollTo(0, this.freezePos); | |
| this.freezePos = 0; | |
| } | |
| } | |
| }; | |
| export let isInViewport = function (elem) { | |
| const bounding = elem.getBoundingClientRect(); | |
| return ( | |
| bounding.top >= 0 && | |
| bounding.left >= 0 && | |
| (bounding.bottom - bounding.height*0.6) <= (window.innerHeight || document.documentElement.clientHeight) && | |
| bounding.right <= (window.innerWidth || document.documentElement.clientWidth) | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment