How to make preload animation disappear in 3s in Javascript - javascript

I have this preload animation that I want to incorporate into my website. All I want to do is play the animation for 3 seconds upon the page loading and then make it fade out. I've already successfully made it fade out by assigning transition property to CSS. All I need is the piece of code that delays it for 3s. I know it's something to do with the setTimeout() method but I think I'm not using it right.
This is what I've got so far:
<div class="spinner-wrapper" id="fds">
<div class="spinner"></div>
</div>
<script>
var preloader = document.getElementById("fds");
function fadeOut() {
preloader.style.opacity = "0.0";
setTimeout(fadeOut(), 5000);
}
</script>
I know the CSS is somewhat irrelevant but anyway, here it is:
.spinner-wrapper {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ff6347;
z-index: 999999;
transition: 2s;
}
.spinner {
width: 40px;
height: 40px;
background-color: #333;
position: absolute;
top: 48%;
left: 48%;
-webkit-animation: sk-rotateplane 1.2s infinite ease-in-out;
animation: sk-rotateplane 1.2s infinite ease-in-out;
}
#-webkit-keyframes sk-rotateplane {
0% { -webkit-transform: perspective(120px) }
50% { -webkit-transform: perspective(120px) rotateY(180deg) }
100% { -webkit-transform: perspective(120px) rotateY(180deg) rotateX(180deg) }
}
#keyframes sk-rotateplane {
0% {
transform: perspective(120px) rotateX(0deg) rotateY(0deg);
-webkit-transform: perspective(120px) rotateX(0deg) rotateY(0deg)
} 50% {
transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg);
-webkit-transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg)
} 100% {
transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg);
-webkit-transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg);
}
}

All I need is the piece of code that delays it for 3s
Set animation-delay to 3s at CSS

Related

How to play different css animations one after another?

I'm trying to play different css animations one after another but I can't figure out how to.
Basically what I'm trying to do is play one Animation, have it on screen for 15 seconds, then play the next one, show it for 15 seconds and on to the next one and when the last one has been played, it should start again from the top.
Here's an example of the first one it should play, show for 15 seconds and then move on to the next one and do the same.
<style> #animated-example {
width: 300px;
height: 200px;
border: solid 1px #1A7404;
position: absolute;
background-color: #62A80A;
}
.animated {
-webkit-animation-duration: 2s;
animation-duration: 2s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
#-webkit-keyframes bounceInLeft {
0% {
opacity: 0;
-webkit-transform: translateX(-2000px);
}
60% {
opacity: 1;
-webkit-transform: translateX(30px);
}
80% {
-webkit-transform: translateX(-10px);
}
100% {
-webkit-transform: translateX(0);
}
}
#keyframes bounceInLeft {
0% {
opacity: 0;
transform: translateX(-2000px);
}
60% {
opacity: 1;
transform: translateX(30px);
}
80% {
transform: translateX(-10px);
}
100% {
transform: translateX(0);
}
}
.bounceInLeft {
-webkit-animation-name: bounceInLeft;
animation-name: bounceInLeft;
}
</style>
<img id="animated-example" class="animated bounceInLeft" src="http://webmarketingtoday.com/wp-content/uploads/Screen-Shot-2012-05-24-at-7.31.54-AM-288x216.png">
And then run another one, show it for 15 seconds and move on.
<style> #animated-example {
width: 300px;
height: 200px;
border: solid 1px #1A7404;
position: absolute;
background-color: #62A80A;
}
.animated {
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
#-webkit-keyframes bounceInDown {
0% {
opacity: 0;
-webkit-transform: translateY(-2000px);
}
60% {
opacity: 1;
-webkit-transform: translateY(30px);
}
80% {
-webkit-transform: translateY(-10px);
}
100% {
-webkit-transform: translateY(0);
}
}
#keyframes bounceInDown {
0% {
opacity: 0;
transform: translateY(-2000px);
}
60% {
opacity: 1;
transform: translateY(30px);
}
80% {
transform: translateY(-10px);
}
100% {
transform: translateY(0);
}
}
.bounceInDown {
-webkit-animation-name: bounceInDown;
animation-name: bounceInDown;
}
</style>
<img id="animated-example" class="animated bounceInDown" src="https://www.facebookbrand.com/img/fb-art.jpg">
The only way to achieve that in pure CSS is to run all the animations at the same time and do some calculations:
the length of each animation should be the same and equal to the total length of desired animations (meaning if you want two 15-second animations, the CSS animations should be set to length of 30 seconds, no delays)
to control the start/end point of each animation, you need to modify the percentages accordingly - in the above case, it means that the first animation ends at 50% and that's when the second animation starts. Also, all in-between values need to be interpolated. It's easy for two animations, but you might need to use a calculator as the total number of animations increases. This is if we don't take the delays into account - the numbers change when we have a 15-second animation that will finish animation after 5 seconds, which now equals 33%, etc...
It will be more clear once you see it in action here:
.animated-example {
width: 300px;
height: 200px;
border: solid 1px #1A7404;
position: absolute;
background-color: #62A80A;
}
.animated {
animation-duration: 20s;
animation-fill-mode: both;
animation-iteration-count: infinite;
}
.bounceInLeft {
-webkit-animation-name: bounceInLeft;
animation-name: bounceInLeft;
}
.bounceInDown {
-webkit-animation-name: bounceInDown;
animation-name: bounceInDown;
}
#keyframes bounceInLeft {
0% {
opacity: 0;
transform: translateX(-2000px);
}
6% {
opacity: 1;
transform: translateX(30px);
}
8% {
transform: translateX(-10px);
}
10% {
transform: translateX(0);
}
40% {
opacity: 1;
transform: translateX(0);
}
42% {
opacity: 1;
transform: translateX(30px);
}
55% {
opacity: 0;
transform: translateX(-2000px);
}
100% {
opacity: 0;
transform: translateX(-2000px);
}
}
#keyframes bounceInDown {
0% {
opacity: 0;
transform: translateY(-2000px);
}
50% {
opacity: 0;
transform: translateY(-2000px);
}
56% {
opacity: 1;
transform: translateY(30px);
}
58% {
transform: translateY(-10px);
}
60% {
transform: translateY(0);
}
90% {
transform: translateY(0);
}
92% {
opacity: 1;
transform: translateY(30px);
}
100% {
opacity: 0;
transform: translateY(-2000px);
}
}
<img class="animated-example animated bounceInLeft" src="http://webmarketingtoday.com/wp-content/uploads/Screen-Shot-2012-05-24-at-7.31.54-AM-288x216.png">
<img class="animated-example animated bounceInDown" src="https://www.facebookbrand.com/img/fb-art.jpg">
Using animation-delay.
animation: a, b;
animation-duration: 2s, 2s;
animation-delay: 0s, 4s;
The animation b will start after 4s while animation a will start without any delay.
animation-delay would do exactly what you're looking for except for the fact that you want the animations to repeat after the last one has been completed; unfortunately there is (currently) no way to specify a delay between iterations of a looping animation.
You could, however, achieve what you're looking to do using a little bit of JavaScript, like the following. To add more animations, simply add their class names to the animations array at the start of the code.
var animations=["bounceInLeft","bounceInDown"],
count=animations.length,
classlist=document.querySelector("img").classList,
holder=document.createElement("div"),
style=window.getComputedStyle(holder),
delay=15,
current,wait,x;
holder.style.display="none";
document.body.appendChild(holder);
animate();
function animate(){
wait=0;
x=0;
while(x<count){
setTimeout(function(a){
classlist.remove(current);
classlist.add(a);
current=a;
},wait*1000,animations[x]);
holder.className=animations[x];
wait+=delay+parseInt(style.getPropertyValue("animation-duration"));
x++;
}
setTimeout(animate,wait*1000);
};
img{
animation-fill-mode:both;
height:200px;
width:300px;
}
.bounceInDown{
animation-duration:1s;
animation-name:bounceInDown;
}
.bounceInLeft{
animation-duration:2s;
animation-name:bounceInLeft;
}
#keyframes bounceInDown{
0%{
opacity:0;
transform:translateY(-2000px);
}
60%{
opacity:1;
transform:translateY(30px);
}
80%{
transform:translateY(-10px);
}
100%{
transform:translateY(0);
}
}
#keyframes bounceInLeft{
0%{
opacity:0;
transform:translateX(-2000px);
}
60%{
opacity:1;
transform:translateX(30px);
}
80%{
transform:translateX(-10px);
}
100%{
transform:translateX(0);
}
}
<img src="http://webmarketingtoday.com/wp-content/uploads/Screen-Shot-2012-05-24-at-7.31.54-AM-288x216.png">
I have managed to achieve something similar by adapting this concept by Noah Addy: http://digitalfio.github.io/Stagger.css/
You will need to work on the timings a bit to get the 15sec delay you want, but other than that it should be fairly straightforward.

replace css animation without changing the position of the div

This is my code
https://jsfiddle.net/sameh0/hgk2uLfk/2/
I want to change the animation of rightleft when hovering over the div. However when I do that, the position of the div gets back first and then changes the animation which creates a bad interaction experience.
That being said I'm open to add any JS/JQuery code.
Currently when the div is hovered it's stopped in its initial position and the new animation bubble takes place.
MY GOAL IS :
to make the div stop at it's current position and the new animation bubble starts while the div is stopped .
and this is the code
HTML
<div class="circle"></div>
CSS
.circle{
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
border-radius: 100%;
position: absolute;
z-index: 100;
cursor: pointer;
top:25%;
left :28%;
width: 90px;
height: 90px;
-webkit-animation: rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955) , bubble 2s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955);
-moz-animation: rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955),bubble 2s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955);
animation: rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955),bubble 2s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955) ;
background-color:blue;
background-size: 80%;
}
.circle:hover{
background-color:red;
-webkit-animation: bubble, 0.8s infinite alternate cubic-bezier(0.46, 0.03, 1, 0.21) !important;
-moz-animation: bubble 0.8s infinite alternate cubic-bezier(0.46, 0.03, 1, 0.21) !important;
animation: bubble 0.8s infinite alternate cubic-bezier(0.46, 0.03, 1, 0.21) !important;
}
#-webkit-keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#-moz-keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#-ms-keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#-webkit-keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
#-moz-keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
#-ms-keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
#keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
How about the following code?
.circle{
background-color:blue;
animation:
rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955),
bubble 2s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955) ;
}
.circle:hover{
background-color:red;
animation:
rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955),
bubble 0.8s infinite alternate cubic-bezier(0.46, 0.03, 1, 0.21);
animation-play-state: paused, running;
}
#keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -100px;
margin-top: -100px;
}
}
#keyframes bubble {
0% {
transform: scale(0.9);
}
100% {
transform: scale(1.1);
}
}
DEMO
https://jsfiddle.net/hgk2uLfk/10/
Updated
<div class="circle">
<div class="bubble"></div>
</div>
.circle{
position: absolute;
z-index: 100;
top:25%;
left :28%;
-webkit-animation: rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955) , bubble 2s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955);
-moz-animation: rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955),bubble 2s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955);
animation: rightleft 2.5s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955),bubble 2s infinite alternate cubic-bezier(0.455, 0.03, 0.515, 0.955) ;
}
.circle:hover {
-webkit-animation-play-state: paused;
-moz-animation-play-state: paused;
-o-animation-play-state: paused;
animation-play-state: paused;
}
.bubble {
width: 90px;
height: 90px;
cursor: pointer;
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
border-radius: 100%;
background-color:blue;
background-size: 80%;
}
.bubble:hover{
background-color:red;
-webkit-animation: bubble, 0.8s infinite alternate cubic-bezier(0.46, 0.03, 1, 0.21) !important;
-moz-animation: bubble 0.8s infinite alternate cubic-bezier(0.46, 0.03, 1, 0.21) !important;
animation: bubble 0.8s infinite alternate cubic-bezier(0.46, 0.03, 1, 0.21) !important;
}
#-webkit-keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#-moz-keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#-ms-keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#keyframes rightleft {
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-left: -30px;
margin-top: -30px;
}
}
#-webkit-keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
#-moz-keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
#-ms-keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
#keyframes bubble {
0% {
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
transform: scale(0.9);
}
100% {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
transform: scale(1.1);
}
}
add this code to the previous code and it will work
.circle:hover {
animation-name: rightleft;
animation-play-state: paused;
}

Seamlessly transition back and forth with only one animation class?

I want to use one class to trigger an animation, and upon removal of that class redo that animation in reverse.
It's hard to visualize, so I've created a CodePen of where I'm at currently.
You'll notice that when .zoom is removed from #box, the #box just vanishes. It doesn't do the animation in reverse, which is ultimately the goal.
How can I seamlessly transition back and forth, with only one animation class? Normally I might use transitions, but you can't transition with transforms.
Try adding .zoomout class , css animations , utilizing .removeClass() , second class at .toggleClass()
window.onclick = function() {
if (!$("#box").is(".zoom")) {
$("#box").removeClass("zoomout")
.toggleClass("zoom");
} else {
$("#box").toggleClass("zoom zoomout");
}
};
#box {
width: 256px;
height: 256px;
background: black;
opacity: 0;
display: block;
transform: scale(1.15, 1.15);
margin: 16px 0px;
}
.zoom {
animation: zoom 500ms;
animation-fill-mode: both;
-moz-animation: zoom 500ms;
-moz-animation-fill-mode: both;
-webkit-animation: zoom 500ms;
-webkit-animation-fill-mode: both;
}
.zoomout {
animation: zoomout 500ms;
animation-fill-mode: both;
-moz-animation: zoomout 500ms;
-moz-animation-fill-mode: both;
-webkit-animation: zoomout 500ms;
-webkit-animation-fill-mode: both;
}
#keyframes zoom {
0% {
opacity: 0;
transform: scale(1.15);
}
100% {
opacity: 1;
transform: scale(1);
}
}
#-moz-keyframes zoom {
0% {
opacity: 0;
transform: scale(1.15);
}
100% {
opacity: 1;
transform: scale(1);
}
}
#-webkit-keyframes zoom {
0% {
opacity: 0;
transform: scale(1.15);
}
100% {
opacity: 1;
transform: scale(1);
}
}
#keyframes zoomout {
0% {
opacity: 1;
transform: scale(1.15);
}
100% {
opacity: 0;
transform: scale(1);
}
}
#-moz-keyframes zoomout {
0% {
opacity: 1;
transform: scale(1.15);
}
100% {
opacity: 0;
transform: scale(1);
}
}
#-webkit-keyframes zoomout {
0% {
opacity: 1;
transform: scale(1.15);
}
100% {
opacity: 0;
transform: scale(1);
}
}
body {
margin: 0;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -60%);
-moz-transform: translate(-50%, -60%);
-webkit-transform: translate(-50%, -60%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div id="box"></div>
Click the document to toggle the box.
codepen http://codepen.io/anon/pen/vOxxKE

Load css element already rotated

I'm trying to create an animation using CSS that will pop up and rotate when the page is loaded. The issue I'm having with is, I need the element to be rotated BEFORE the animation begins.
JSFiddle: http://jsfiddle.net/4o1w01q5/
I can't tell if the issue is with this CSS snippet:
.container {
background:red;
background-image: url(img/2012-04-12_14-06-35_758-1.jpg);
background-position: center;
background-repeat: no-repeat;
padding:240px;
padding-left: 300px;
padding-right: 300px;
padding-top:10px;
-webkit-animation: logo-appear 0.6s, logo-rotate 1.6s;
-moz-animation: logo-appear 0.6s, logo-rotate 1.6s;
animation: logo-appear 0.6s, logo-rotate 1.6s;
}
Or if there is a way to set the rotated positiong with jQuery. Any suggestions?
Edit: To clarify, what I'm trying to do is
Have a div load, rotated 45 degrees
Pop the div (logo-appear)
Rotate the div 45 degrees down (logo-rotate)
Why do I want it like this? I want a diamond-shaped element load and the rotate it to make it a square.
Figured it out. It was a matter of having the rotate happen along with the popping up sequence.
JSFiddle: http://jsfiddle.net/s8hae5dz/ with additional animation
Previous example:
#-webkit-keyframes logo-appear{
0% {
opacity: 0;
-webkit-transform: scale(0.5);
}
20% {
opacity: 1;
-webkit-transform: scale(1.2);
}
60% {
opacity: 1;
-webkit-transform: scale(1.2);
}
100% {
-webkit-transform: scale(1);
}
}
Currently:
#-webkit-keyframes logo-appear{
0% {
opacity: 0;
-webkit-transform: scale(0.5) rotate(-45deg);
}
20% {
opacity: 1;
-webkit-transform: scale(1.2) rotate(-45deg);
}
60% {
opacity: 1;
-webkit-transform: scale(1.2) rotate(-45deg);
}
100% {
-webkit-transform: scale(1) rotate(-45deg);
}
}

My javascript call is being ignored but why?

To break down what im trying to achieve. As you scroll down to a certain div the animation is called and takes place. The CSS is working fine so it slides in nicely as it should. The js is being ignored no matter where I place it so the trigger happens as the page loads and not when it hits the div.
So here is my code...
<script>
$(window).scroll(function() {
$('#thedevimage').each(function(){
var imagePos = $(this).offset().top;
var topOfWindow = $(window).scrollTop();
if (imagePos < topOfWindow+1) {
$(this).addClass("slideRight");
}
});
});
</script>
Here is the html...
<div class="thedev">
<div class="left">
<div id="thedevimage" class="slideRight"><img src="images/xcode.png" style="width:100%;" /></div>
</div></div>
And the CSS...
#thedevimage{
width: 80%;
visibility: hidden;
}
.slideRight{
animation-name: slideRight;
-webkit-animation-name: slideRight;
animation-duration: 1s;
-webkit-animation-duration: 1s;
animation-timing-function: ease-in-out;
-webkit-animation-timing-function: ease-in-out;
visibility: visible !important;
}
#keyframes slideRight {
0% {
transform: translateX(-150%);
}
50%{
transform: translateX(8%);
}
65%{
transform: translateX(-4%);
}
80%{
transform: translateX(4%);
}
95%{
transform: translateX(-2%);
}
100% {
transform: translateX(0%);
}
}
#-webkit-keyframes slideRight {
0% {
-webkit-transform: translateX(-150%);
}
50%{
-webkit-transform: translateX(8%);
}
65%{
-webkit-transform: translateX(-4%);
}
80%{
-webkit-transform: translateX(4%);
}
95%{
-webkit-transform: translateX(-2%);
}
100% {
-webkit-transform: translateX(0%);
}
}

Categories