Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Advanced Search
  • 0

Can a CSS animation start based on when a JS animation ends?


CherryTree

Question

Site URL: http://www.cherrytreeinteriordesign.com

I have a js code that animates the header on all but my mobile pages. The code is as follows:

<script>
if (document.documentElement.clientWidth <740) {
 window.location = "/mobile";
}
</script>

<script>
(function(){
    window.addEventListener('load', function() {
    var headerElements = document.querySelectorAll('header a')

    function fadeIn(element, time, delay) {
      var keyframes = {opacity: [0,1]};
      var timing = {
        duration: time,
        fill: 'both',
        easing: 'ease',
        delay: delay
      };
  
     element.animate(keyframes, timing)
    }

    function animateElements(element,animation,time,delay) {
        element.forEach((item,index) => {
            var delays = index*delay;
            item.style.display = 'inline-block';
            animation(item,time,delays)
        })
    };
    //Call animateElements function and set your timing and delay.
    animateElements(headerElements,fadeIn,2000,300);  
    })
})()
</script>

I also have css animation for text that pops up on the home screen. I have it basically timed out to pop up when the header animation is complete, but depending on the load it is sometimes a bit off. (You can see it on my homepage on a desktop). I'm not sure if there is a way to point the css animation code to begin when the js code animation starts. The css code is here:

.fade-in-text {
  display: inline-block;
  font-family: FuturaPT, Helvetica, sans-serif;
  font-weight: 800;
  letter-spacing: 0.4em;
  text-align: center;
  font-size: 6em;
  color: white;
  opacity: 0;
  animation: fadeIn linear 300ms;
  animation-delay: 5650ms;
  -webkit-animation: fadeIn linear 300ms;
  -webkit-animation-delay: 5650ms;
  -webkit-animation-fill-mode: forwards;
  -moz-animation: fadeIn linear 300ms;
  -moz-animation-delay: 5650ms;
  -moz-animation-fill-mode: forwards;
  -o-animation: fadeIn linear 300ms;
  -o-animation-delay: 5650ms;
  -o-animation-fill-mode: forwards;
  -ms-animation: fadeIn linear 300ms;
  -ms-animation-delay: 5650ms;
  -ms-animation-fill-mode: forwards;
}

@keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-moz-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-webkit-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-o-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-ms-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

Anyone have any idea how to do this?

Thanks!

Link to comment

1 answer to this question

Recommended Posts

  • 0
On 7/13/2021 at 12:20 AM, CherryTree said:

Site URL: http://www.cherrytreeinteriordesign.com

I have a js code that animates the header on all but my mobile pages. The code is as follows:

<script>
if (document.documentElement.clientWidth <740) {
 window.location = "/mobile";
}
</script>

<script>
(function(){
    window.addEventListener('load', function() {
    var headerElements = document.querySelectorAll('header a')

    function fadeIn(element, time, delay) {
      var keyframes = {opacity: [0,1]};
      var timing = {
        duration: time,
        fill: 'both',
        easing: 'ease',
        delay: delay
      };
  
     element.animate(keyframes, timing)
    }

    function animateElements(element,animation,time,delay) {
        element.forEach((item,index) => {
            var delays = index*delay;
            item.style.display = 'inline-block';
            animation(item,time,delays)
        })
    };
    //Call animateElements function and set your timing and delay.
    animateElements(headerElements,fadeIn,2000,300);  
    })
})()
</script>

I also have css animation for text that pops up on the home screen. I have it basically timed out to pop up when the header animation is complete, but depending on the load it is sometimes a bit off. (You can see it on my homepage on a desktop). I'm not sure if there is a way to point the css animation code to begin when the js code animation starts. The css code is here:

.fade-in-text {
  display: inline-block;
  font-family: FuturaPT, Helvetica, sans-serif;
  font-weight: 800;
  letter-spacing: 0.4em;
  text-align: center;
  font-size: 6em;
  color: white;
  opacity: 0;
  animation: fadeIn linear 300ms;
  animation-delay: 5650ms;
  -webkit-animation: fadeIn linear 300ms;
  -webkit-animation-delay: 5650ms;
  -webkit-animation-fill-mode: forwards;
  -moz-animation: fadeIn linear 300ms;
  -moz-animation-delay: 5650ms;
  -moz-animation-fill-mode: forwards;
  -o-animation: fadeIn linear 300ms;
  -o-animation-delay: 5650ms;
  -o-animation-fill-mode: forwards;
  -ms-animation: fadeIn linear 300ms;
  -ms-animation-delay: 5650ms;
  -ms-animation-fill-mode: forwards;
}

@keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-moz-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-webkit-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-o-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

@-ms-keyframes fadeIn {
  0% {opacity:0;}
  100% {opacity:1;}
}

Anyone have any idea how to do this?

Thanks!

You can add a custom class on body like .animation-start and enable css keyframe only when the class available

Greeting, it's BaNgan from BeyondSpace, I am Squarespace dev focus on provide solutions to enhance feature that squarespace.com can't provide.
Feel free to check my current Squarespace Plugins Developement: Enable Pinch/Zoom on lightbox, Delivery Date Picker, Portfolio hover Replace Link Style
If you find my answer fit your need, let's leave a like or upvote so others with the same issue can find their solution. Thank you

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...