Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use javascript in order to set the current percentage of a css animation

I use a css animation to represent the sun cycle for a day (from 6AM to 6PM). Unfortunatly, I want also to be able to set the current percentage depending on the time of the day (for example, if the current time is 12AM, I want to put the sun at 50% of the animation.

Here is my jsfiddle : http://jsfiddle.net/vyhjt6mu/3/

and Here is my code :

/* Chrome, Safari, Opera */

@-webkit-keyframes sunAnimation {
  0% {
    left: -100px;
    top: 30%;
  }
  25% {
    left: calc(25% - 25px);
    top: 20%;
  }
  50% {
    left: calc(50% - 40px);
    top: 10%;
  }
  75% {
    left: calc(75% + 25px);
    top: 20%;
  }
  100% {
    left: calc(100% + 100px);
    top: 30%;
  }
}
@keyframes sunAnimation {
  0% {
    left: -100px;
    top: 30%;
  }
  25% {
    left: calc(25% - 25px);
    top: 20%;
  }
  50% {
    left: calc(50% - 40px);
    top: 10%;
  }
  75% {
    left: calc(75% + 25px);
    top: 20%;
  }
  100% {
    left: calc(100% + 100px);
    top: 30%;
  }
}
.sun {
  width: 100px;
  height: 100px;
  position: absolute;
  animation-name: sunAnimation;
  animation-duration: 60s;
  animation-timing-function: linear;
  -webkit-animation-name: sunAnimation;
  /* Chrome, Safari, Opera */
  -webkit-animation-duration: 60s;
  /* Chrome, Safari, Opera */
  -webkit-animation-timing-function: linear;
  /* Chrome, Safari, Opera */
}

How can I simply set the current percentage of a css3 animation ? If it's very complex to do it, does a library exist for that ?

Thanks for your help.

It's not a duplicate because the needs are different.

like image 697
I_Am_Rototo Avatar asked Apr 08 '15 11:04

I_Am_Rototo


People also ask

Does CSS animation use JavaScript?

CSS allows animation of HTML elements without using JavaScript or Flash! In this chapter you will learn about the following properties: @keyframes.

Is CSS animation faster than JavaScript?

The fact is that, in most cases, the performance of CSS-based animations is almost the same as JavaScripted animations — in Firefox at least. Some JavaScript-based animation libraries, like GSAP and Velocity. JS, even claim that they are able to achieve better performance than native CSS transitions/animations.

Can JavaScript develop some animation effects?

By using JavaScript animation libraries, you can program website elements to 'whoosh', 'fade', or 'bounce'. There are many types of JavaScript animations, like: Content boxes that slide in on page load. Hilarious text animations, and more.

How does CSS measure animation performance?

Open the Rendering tab of Chrome DevTools. Enable the FPS meter checkbox. Watch the values as your animation runs.


1 Answers

You can specify a negative animation-delay property.

Example: http://jsfiddle.net/vyhjt6mu/4/

In that example I've set animation-delay: -30s so the animation will start from the middle point


For this task you could set 24 different classes in CSS (one for each hour) like so

.h00 {
  animation-delay: 0s; 
  -webkit-animation-delay: 0s; 
}

.h01 {
  animation-delay: -2.5s; 
  -webkit-animation-delay: -2.5s; 
}

.h02 {
  animation-delay: -5s; 
  -webkit-animation-delay: -5s; 
}

...

.h22 {
  animation-delay: -55s; 
  -webkit-animation-delay: -55s; 
}

.h23 {
  animation-delay: -57.5s; 
  -webkit-animation-delay: -57.5s; 
}

where the difference of delay between each hour is 2.5 seconds (60s/24); then, via JS, get the current hour via getHours() method and apply the right class name to your element

like image 126
Fabrizio Calderan Avatar answered Sep 28 '22 10:09

Fabrizio Calderan