Can you tell me what is done by position: numbers % in css animation.
How these numbers are working?
body{
font-family:'Merriweather Sans';
background:white;
}
#test{
background:linear-gradient(270deg, #36bf9c, #368ebf, #df5a5f, #eea965);
color:white;
background-size:600% 600%;
padding:20px 450px;
position:static;
font-size:40px;
font-family:"Merriweather Sans";
animation:gradient 60s ease infinite;
}
#keyframes gradient{
0%{
background-position: 0% 50%;
}
50%{
background-position:100% 50%;
}
100%{
background-position:0% 50%;
}
0% means the CSS will be applied at the start
50% means when half of the specified time will pass ( here 30s)
100% means when the full time passes.
And the animations can't happen instantly so it will go with a lot of transition states but at these times the CSS will be the specified one.
The background-size is 600%, which means 6 times the size of visual area (#test div)it is displayed in. The background is set up as a gradient. By moving this background left to right and back, it makes the colors change in an animated fashion.
I have added an extra div in the example to show the gradient.
body{
font-family:'Merriweather Sans';
background:white;
}
#test{
background:linear-gradient(270deg, #36bf9c, #368ebf, #df5a5f, #eea965);
color:white;
background-size:600% 600%;
padding:20px 450px;
position:static;
font-size:40px;
font-family:"Merriweather Sans";
animation:gradient 60s ease infinite;
}
#backgroundImage {
background:linear-gradient(270deg, #36bf9c, #368ebf, #df5a5f, #eea965);
width:200px;
height:100px;
background-size:100% 100%;
}
#keyframes gradient{
0%{
background-position: 0% 50%;
}
50%{
background-position:100% 50%;
}
100%{
background-position:0% 50%;
}
<div id="test"></div>
<br>
<div id="backgroundImage"></div>
Related
I have an animation that plays in the background of my website
that control different shapes. Each of them have an animation like so:
animation: animShape 50s linear infinite;
but the speed is different depending on the shape.
animShape is:
animShape {
from {
transform: translateY(0px);
}
to {
transform: translateY(-2000px);
}
}
I want to change the speed of each of my shapes (which are divs) based on the position of the scroll.
I was thinking about the best way to do this and I was hoping you could point me to the right direction?
Idea:
Have different animation classes that way I could use something like this:
$(document).on('scroll', '#section-trigger' , function(){
$("#first-shape").removeClass('.first-animation-class');
$("#first-shape").addClass('.second-animation-class');})
$(document).on('scroll', '#another-section-trigger', function() {
$("#first-shape").removeClass('.second-animation-class');
$("#first-shape").addClass('first-animation-class');})
...
But I would have to do that for every shape div (since they all have different speeds) and I don't know if this is the correct way to go about it.
I could have JQuery handle all of the animations but I read that it can't access :after so I was hoping for a work around somebody knows?
Sorry for the formatting if it's wrong -- I get confused sometimes.
Thanks!
I prepared an animation that reacts with scrolling and used a timer to synchronize the execution and end of the animation.
Just make sure that each animation you put is in line with the timer.
For example, I put an animation 2 seconds and twice, that is, it takes
4 seconds to run once.
Of course, with a little change in the same structure, you can run any animation
let timer;
var timerCount = 0;
MyTimer();
var element1 = document.getElementById("myDIV1");
var element2 = document.getElementById("myDIV2");
function doSomething() {
if(!element1.classList.contains('anim1'))
element1.classList.add("anim1");
if(!element2.classList.contains('anim2'))
element2.classList.add("anim2");
}
function MyTimer()
{
timerCount = timerCount + 1;
if(timerCount >= 5)
{
if(element1.classList.contains('anim1'))
element1.classList.remove("anim1");
if(element2.classList.contains('anim2'))
element2.classList.remove("anim2");
timerCount = 0;
}
timer = setTimeout(MyTimer, 1000);
}
document.addEventListener('scroll', function(e) {
doSomething();
});
body{
height:2500px;
}
.shape1{
width: 50px;
height: 50px;
background-color: red;
position: relative;
float:left;
}
.shape2{
width: 50px;
height: 50px;
background-color: red;
position: relative;
float:right;
}
.wrapper{
width;100%;
border:1px solid #ddd;
padding:20px;
margin:150px 20px;
height:150px;
}
.anim1 {
animation-name: example1;
animation-duration: 2s;
animation-iteration-count: 2;
animation-direction: alternate;
}
.anim2 {
animation-name: example2;
animation-duration: 2s;
animation-iteration-count: 2;
animation-direction: alternate;
}
#keyframes example1 {
0% {background-color:red; left:0px; top:0px;}
25% {background-color:yellow; left:100px; top:0px;}
50% {background-color:blue; left:100px; top:100px;}
75% {background-color:green; left:0px; top:100px;}
100% {background-color:red; left:0px; top:0px;}
}
#keyframes example2 {
0% {background-color:red; right:0px; top:0px;}
25% {background-color:yellow; right:100px; top:0px;}
50% {background-color:blue; right:100px; top:100px;}
75% {background-color:green; right:0px; top:100px;}
100% {background-color:red; right:0px; top:0px;}
}
<div class="wrapper">
<div id="myDIV1" class="shape1"></div>
<div id="myDIV2" class="shape2"></div>
</div>
Why this isn't working? What am I doing wrong?
CSS
#-webkit-keyframes test {
0% {
background-image: url('frame-01.png');
}
20% {
background-image: url('frame-02.png');
}
40% {
background-image: url('frame-03.png');
}
60% {
background-image: url('frame-04.png');
}
80% {
background-image: url('frame-05.png');
}
100% {
background-image: url('frame-06.png');
}
}
div {
float: left;
width: 200px;
height: 200px;
-webkit-animation-name: test;
-webkit-animation-duration: 10s;
-webkit-animation-iteration-count: 2;
-webkit-animation-direction: alternate;
-webkit-animation-timing-function: linear;
}
DEMO
http://jsfiddle.net/hAGKv/
Updated for 2020: Yes, it can be done! Here's how.
Snippet demo:
#mydiv{ animation: changeBg 1s infinite; width:143px; height:100px; }
#keyframes changeBg{
0%,100% {background-image: url("https://i.stack.imgur.com/YdrqG.png");}
25% {background-image: url("https://i.stack.imgur.com/2wKWi.png");}
50% {background-image: url("https://i.stack.imgur.com/HobHO.png");}
75% {background-image: url("https://i.stack.imgur.com/3hiHO.png");}
}
<div id='mydiv'></div>
Background image [isn't a property that can be animated][1] - you can't tween the property.
Original Answer: (still a good alternative)
Instead, try laying out all the images on top of each other using position:absolute, then animate the opacity of all of them to 0 except the one you want repeatedly.
It works in Chrome 19.0.1084.41 beta!
So at some point in the future, keyframes could really be... frames!
You are living in the future ;)
Works for me.
Notice the use of background-image for transition.
#poster-img {
background-repeat: no-repeat;
background-position: center;
position: absolute;
overflow: hidden;
-webkit-transition: background-image 1s ease-in-out;
transition: background-image 1s ease-in-out;
}
This is really fast and dirty, but it gets the job done: jsFiddle
#img1, #img2, #img3, #img4 {
width:100%;
height:100%;
position:fixed;
z-index:-1;
animation-name: test;
animation-duration: 5s;
opacity:0;
}
#img2 {
animation-delay:5s;
-webkit-animation-delay:5s
}
#img3 {
animation-delay:10s;
-webkit-animation-delay:10s
}
#img4 {
animation-delay:15s;
-webkit-animation-delay:15s
}
#-webkit-keyframes test {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
}
}
#keyframes test {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
}
}
I'm working on something similar for my site using jQuery, but the transition is triggered when the user scrolls down the page - jsFiddle
I needed to do the same thing as you and landed on your question. I ended up taking finding about the steps function which I read about from here.
JSFiddle of my solution in action (Note it currently works in Firefox, I'll let you add the crossbrowser lines, trying to keep the solution clean of clutter)
First I created a sprite sheet that had two frames. Then I created the div and put that as the background, but my div is only the size of my sprite (100px).
<div id="cyclist"></div>
#cyclist {
animation: cyclist 1s infinite steps(2);
display: block;
width: 100px;
height: 100px;
background-image: url('../images/cyclist-test.png');
background-repeat: no-repeat;
background-position: top left;
}
The animation is set to have 2 steps and have the whole process take 1 second.
#keyframes cyclist {
0% {
background-position: 0 0;
}
100% {
background-position: 0 -202px; //this should be cleaned up, my sprite sheet is 202px by accident, it should be 200px
}
}
Thiago above mentioned the steps function but I thought I'd elaborate more on it. Pretty simple and awesome stuff.
Your code can work well with some adaptations :
div {
background-position: 50% 100%;
background-repeat: no-repeat;
background-size: contain;
animation: animateSectionBackground infinite 240s;
}
#keyframes animateSectionBackground {
00%, 11% { background-image: url(/assets/images/bg-1.jpg); }
12%, 24% { background-image: url(/assets/images/bg-2.jpg); }
25%, 36% { background-image: url(/assets/images/bg-3.jpg); }
37%, 49% { background-image: url(/assets/images/bg-4.jpg); }
50%, 61% { background-image: url(/assets/images/bg-5.jpg); }
62%, 74% { background-image: url(/assets/images/bg-6.jpg); }
75%, 86% { background-image: url(/assets/images/bg-7.jpg); }
87%, 99% { background-image: url(/assets/images/bg-8.jpg); }
}
Here is the explanation of the percentage to suit your situation:
First you need to calculate the "chunks". If you had 8 differents background, you need to do :
100% / 8 = 12.5% (to simplify you can let fall the decimals) => 12%
After that you obtain that :
#keyframes animateSectionBackground {
00% { background-image: url(/assets/images/bg-1.jpg); }
12% { background-image: url(/assets/images/bg-2.jpg); }
25% { background-image: url(/assets/images/bg-3.jpg); }
37% { background-image: url(/assets/images/bg-4.jpg); }
50% { background-image: url(/assets/images/bg-5.jpg); }
62% { background-image: url(/assets/images/bg-6.jpg); }
75% { background-image: url(/assets/images/bg-7.jpg); }
87% { background-image: url(/assets/images/bg-8.jpg); }
}
If you execute this code, you will see the transition will be permanantly. If you want the backgrounds stay fixed while a moment, you can do like this :
#keyframes animateSectionBackground {
00%, 11% { background-image: url(/assets/images/bg-1.jpg); }
12%, 24% { background-image: url(/assets/images/bg-2.jpg); }
25%, 36% { background-image: url(/assets/images/bg-3.jpg); }
37%, 49% { background-image: url(/assets/images/bg-4.jpg); }
50%, 61% { background-image: url(/assets/images/bg-5.jpg); }
62%, 74% { background-image: url(/assets/images/bg-6.jpg); }
75%, 86% { background-image: url(/assets/images/bg-7.jpg); }
87%, 99% { background-image: url(/assets/images/bg-8.jpg); }
}
That mean you want :
bg-1 stay fixed from 00% to 11%
bg-2 stay fixed from 12% to 24%
etc
By putting 11%, the transtion duration will be 1% (12% - 11% = 1%).
1% of 240s (total duration) => 2.4 seconds.
You can adapt according to your needs.
The linear timing function will animate the defined properties linearly. For the background-image it seems to have this fade/resize effect while changing the frames of you animation (not sure if it is standard behavior, I would go with #Chukie B's approach).
If you use the steps function, it will animate discretely. See the timing function documentation on MDN for more detail. For you case, do like this:
-webkit-animation-timing-function: steps(1,end);
animation-timing-function: steps(1,end);
See this jsFiddle.
I'm not sure if it is standard behavior either, but when you say that there will be only one step, it allows you to change the starting point in the #keyframes section. This way you can define each frame of you animation.
Like the above stated, you can't change the background images in the animation. I've found the best solution to be to put your images into one sprite sheet, and then animate by changing the background position, but if you're building for mobile, your sprite sheets are limited to less than 1900x1900 px.
I needed to do the same thing recently. Here's a simple implementation
#wrapper { width:100%; height:100%; position:relative; }
#wrapper img { position:absolute; top:0; left:0; width:100%; height:auto; display:block; }
#wrapper .top { animation:fadeOut 2s ease-in-out; animation-fill-mode:forwards; }
#keyframes fadeOut {
0% { opacity:1; }
100% { opacity:0; }
}
<div id="wrapper">
<img src="img1.jpg" class="top" style="z-index:2;">
<img src="img2.jpg" style="z-index:1;">
</div>
You can use animated background-position property and sprite image.
You can follow by this code:
#cd{
position: relative;
margin: 0 auto;
height: 281px;
width: 450px;
}
#cf img{
left: 0;
position: absolute;
-moz-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#cf img.top:hover{
opacity: 0;
}
<div id="cf">
<img class="button" src="Birdman.jpg" />
<img src="Turtle.jpg" class="top" />
</div>
You can use the jquery-backstretch image which allows for animated slideshows as your background-images!
https://github.com/jquery-backstretch/jquery-backstretch
Scroll down to setup and all of the documentation is there.
Well I can change them in chrome. Its simple and works fine in Chrome using -webkit css properties.
After executing this, background changes too fast and also it is a bit shaking. Help me to slow down this background change and stop background to shake.
HTML
<!-- Banner -->
<section id="banner">
<div class="inner">
<h2>Enhancing Your <br />Ways</h2>
<p>A free platform for schedualing</p>
</div>
</section>
CSS (Animation Delays not working)
<!--The animation-delays not working-->
#keyframes changebackground {
0% {
background-image: url("../Images/4.jpg");
animation-delay:5s;
}
25% {
background-image: url("../Images/1.jpg") ;
animation-delay:5s;
}
50% {
background-image: url("../Images/2.jpg") ;
animation-delay:5s;
}
100% {
background-image: url("../Images/3.jpg");
animation-delay:5s;
}
}
#banner {
margin-top:2.9em;
background-image: url("../Images/4.jpg");
background-position:center;
background-repeat:no-repeat;
padding:22em 0em 8em 0em;
background-size:cover;
width:100%;
float:left;
animation-name:changebackground;
animation-iteration-count:infinite;
animation-duration:2s;
animation-delay:5s;
}
If you need to slow down the animation then the property that needs to be modified is the animation's duration and not the animation delay. Set the animation-duration to a higher value. In the snippet, I have set it as 20s and so the change from each image to the next will take around 5s. If you need a time of 25s between each switch, then set the duration as 100s. animation-delay just adds a time delay before the start of the animation's first iteration but it doesn't really slow it down.
I don't really see a shake and so would need to see a demo of your code in-order to provide solutions. You may want to have a look at preloading all background images to stop it from causing problems.
#keyframes changebackground {
0% {
background-image: url("http://lorempixel.com/200/200/nature/4");
}
25% {
background-image: url("http://lorempixel.com/200/200/nature/1");
}
50% {
background-image: url("http://lorempixel.com/200/200/nature/2");
}
75% {
background-image: url("http://lorempixel.com/200/200/nature/3");
}
}
#banner {
margin-top: 2.9em;
background-image: url("http://lorempixel.com/200/200/nature/4");
background-position: center;
background-repeat: no-repeat;
padding: 22em 0em 8em 0em;
background-size: cover;
width: 100%;
float: left;
animation-name: changebackground;
animation-iteration-count: infinite;
animation-duration: 20s; /* set this */
animation-delay: 5s;
}
<section id="banner">
<div class="inner">
<h2>Enhancing Your <br />Ways</h2>
<p>A free platform for schedualing</p>
</div>
</section>
I'm building a site, and I have an animating image in my header. This image is a coloured bar that animates from right to left on a loop to give the impression of it infinitely moving left.
What I would like to accomplish, is to have a fade in/out effect on the left and the right side of the image, without affecting the animation of it's background image.
This is my HTML code:
<div id="hbaranim"><div id="hbarchild"></div></div>
And my current CSS (with just the animating image):
#hbaranim {
width: 100%;
height: 5px;
overflow-x: hidden;
padding-bottom: 10px;
padding-top: 10px;
}
#hbaranim #hbarchild {
position: relative;
width: 8524px;
height: 5px;
background-image: url("img/colorbartile.png");
background-repeat: repeat-x;
-webkit-animation: hbaranim_roll linear 245s infinite;
}
#-webkit-keyframes hbaranim_roll {
from { right: 0px; }
to { right: 2131px; }
}
JSFiddle
Eddit: Currently using the HTML is using two div tags, but they don't have any content in them, so using just one would probably be better (not sure how to that to be honest...)
Eddit 2: Just to clarify, the animating image is positioned over a gradient background, so just putting another gradient over the image like some of you suggested won't work in this case. It really need's to be transparent at the sides.
Eddit 3: Some of you also suggested using a CSS gradient instead of an image, but the image I use on my actual site contains some other details that would be impossible to replicate in CSS. For this example I used an image that could indeed be replicated with a CSS gradient quite easily.
Eddit 4: Updated the fiddle to include the whole header
You could use absolutely positioned pseudo elements on the parent element with gradient backgrounds to achieve this. You can also achieve the same effect with a single div.
UPDATE: Following on from edit 3 in the original question, the rainbow gradient I've used below can be replaced with an image file, the exact same principles apply.
More information on pseudo elements
More information on gradients
EXAMPLE
*{box-sizing:border:box;}
body{margin:0;}
.header{
background:radial-gradient(ellipse at center,#242424 0%,#141414 100%);
box-shadow:0px 0px 10px 3px rgba(0,0,0,.75);
height:150px;
position:relative;
}
h1{
color:#fff;
font-family:arial;
font-size:48px;
padding:25px 0 0;
margin:0;
text-align:center;
}
h2{
color:#fff;
font-family:arial;
font-size:24px;
line-height:25px;
margin:0;
text-align:center;
}
#hbaranim{
-webkit-animation:hbaranim_roll linear 10s infinite;
animation:hbaranim_roll linear 10s infinite;
background:linear-gradient(90deg,#f00 0%,#ff0 16.667%,#0f0 33.333%,#0ff 50%,#00f 66.667%,#f0f 83.333%,#f00 100%) 0 0 repeat-x;
background-size:200%;
bottom:10px;
height:5px;
position:absolute;
width:100%;
}
#hbaranim::before,#hbaranim::after{
content:"";
display:block;
height:100%;
position:absolute;
top:0;
width:25%;
z-index:1;
}
#hbaranim::before{
background:linear-gradient(90deg,rgba(20,20,20,1),rgba(20,20,20,0));
left:0;
}
#hbaranim::after{
background:linear-gradient(90deg,rgba(20,20,20,0),rgba(20,20,20,1));
right:0;
}
#-webkit-keyframes hbaranim_roll{
0%{
background-position:0 0;
}
100%{
background-position:200% 0;
}
}
#keyframes hbaranim_roll{
0%{
background-position:0 0;
}
100%{
background-position:200% 0;
}
}
<div class="header">
<h1>Name of Site</h1>
<h2>www.site.nl</h2>
<div id="hbaranim"></div>
</div>
If you're feeling adventurous, you can do this without the nested div (Fiddle) or even without the parent div (Fiddle)
The following snippet was provided as an example while awaiting Ties' confirmation that removing the child div was an option and is included below as a matter of record.
#hbaranim{
overflow:hidden;
padding:10px 0;
position:relative;
width:100%;
}
#hbaranim #hbarchild{
background-color:#000;
height:20px;
position:relative;
width:8524px;
z-index:1;
}
#hbaranim::before,#hbaranim::after{
content:"";
display:block;
height:20px;
position:absolute;
top:10px;
width:20%;
z-index:2;
}
#hbaranim::before{
background:linear-gradient(90deg,rgba(255,255,255,1),rgba(255,255,255,0));
left:0;
}
#hbaranim::after{
background:linear-gradient(90deg,rgba(255,255,255,0),rgba(255,255,255,1));
right:0;
}
<div id="hbaranim">
<div id="hbarchild"></div>
</div>
I am new to the world of CSS3 and I've been trying to create a flying twitter bird effect using CSS3 but I am unable to achieve the complete thing which I want. I want to create a few top-down-left-right motion transitions for which I've created 3 .gifs in which the bird is flapping its' wings. Here's the effect which I've created, using CSS3, in which I have just used one of the three images.
Example 1
<a target="_blank" href="javascript:void(0)" id="bird"></a>
#bird {
background-image: url(http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird.gif);
width: 64px;
height: 62px;
top: 0px;
left: 0px;
position: absolute;
}
#bird
{
animation:myfirst 10s;
-webkit-animation:myfirst 10s; /* Safari and Chrome */
animation-delay:5s;
-webkit-animation-delay:5s; /* Safari and Chrome */
}
#keyframes myfirst
{
0% {left:0px; top:0px;}
25% {left:302px; top:95px;}
50% {left:-5px; top:214px;}
100% {left:0px; top:0px;}
}
#-webkit-keyframes myfirst /* Safari and Chrome */
{
0% {left:0px; top:0px;}
25% {left:302px; top:95px;}
50% {left:-5px; top:214px;}
100% {left:0px; top:0px;}
}
Now when I am trying to use all the three images inside the CSS3 animation then the problem which I am facing is that the animation, which I had originally added to the .gif images using Photoshop, stops working. Here's the link of this example:
Example 2
<a target="_blank" href="javascript:void(0)" id="bird"></a>
#bird {
background-image: url(http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird.gif);
width: 64px;
height: 62px;
top: 0px;
left: 0px;
position: absolute;
}
#bird
{
animation:myfirst 10s;
-webkit-animation:myfirst 10s; /* Safari and Chrome */
animation-delay:5s;
-webkit-animation-delay:5s; /* Safari and Chrome */
}
#keyframes myfirst
{
0% {left:0px; top:0px;}
25% {width: 76px; height: 55px; background-image: url(http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird2.gif); left:302px; top:95px;}
50% {width: 76px; height: 55px; background-image: url(http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird3.gif); left:-5px; top:214px;}
100% {left:0px; top:0px;}
}
#-webkit-keyframes myfirst /* Safari and Chrome */
{
0% {left:0px; top:0px;}
25% {width: 76px; height: 55px; background-image: url(http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird2.gif); left:302px; top:95px;}
50% {width: 76px; height: 55px; background-image: url(http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird3.gif); left:-5px; top:214px;}
100% {left:0px; top:0px;}
}
Apart from this, I was wondering if there's any way to add a delay between two transitions? Like using animation-delay:5s;, I was able to add a 5s delay between the the start and the animation at 25% so, similarly, is there any way to add a 5s delay between the animation at 25% and the animation at 50% and same thing for the animation at 50% and the animation at 100%?
Other than this, here are the links to the 3 images which i'd like to use in this animation:
Bird Image 1 http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird.gif
Bird Image 2 http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird2.gif
Bird Image 3 http://allwebutilities.com/pnc/wp-content/uploads/2013/04/bird3.gif
There is a similar question here :
Changing Background Image with CSS3 Animations
However on Chrome the background image does change but the wings don't flap while the CSS animation runs.
You could try using a sprite to combine the images and then change the background-position.