Firstly:
Your JSFiddle was a little over-complicated and animations were on wrong elements etc.
I'd removed unnecessary elements in the HTML for clarity.
Here is an updated JSFiddle that shows your desired effect https://jsfiddle.net/6nstfftn/4/
Read up on CSS Animations!:
I would highly recommend looking into CSS animations. There's a guy I follow on YouTube and he's super-awesome. He has some great tutorials - well worth a watch.
Find him at : https://www.youtube.com/devtipsfordesigners
He's done a series very recently with some videos on CSS transition/animation/effects etc. Check it out.
Steps to take:
- Make your
#index
element's position: relative;
- Add pseudo-elements for
:before
and :after
, setting their position: absolute;
and their top
, bottom
, left
and right
to 0
(basically making them fill the width and height of their "parent" element). Make sure to set the content: ''
, otherwise they will have no size!
- Then, for the pseudo-elements, set the
background-image
, and set the z-index
to layer them properly - you can have them in whatever order you like
- Set the "default" style for each "background" - so, if you want it to start at
0
opacity (to fade in), do that; likewise if you want it to move, move it to the "start" position: TL;DR - you may end up seeing the elements appear and flash before the animation has started if you set them to the "end" position - also, if the attribute is not defined in the CSS, the animation might not know what it was to begin with
- Create the animations using
@keyframes animationName
, using from
and to
to set the starting and ending properties of the animation
- Apply the
transition
attribute to each of the "backgrounds" to allow smoothness
- Apply the
animation
attribute to each of the backgrounds, setting the name of each @keyframes
to use. Set a duration, and use forwards
to keep the end-state of the animation
- Sit back and enjoy a rewarding hot beverage
Here's a snippet of the CSS:
#intro {
position: relative;
}
#intro:after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url("http://i65.tinypic.com/2woc2o5.png") no-repeat top;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
z-index: -2;
transition: all 0.5s ease-in-out;
animation: background 0.5s forwards;
opacity: 0;
}
#intro:before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url("http://i65.tinypic.com/2mn0w8j.png") no-repeat top;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
z-index: -1;
transition: all 0.5s ease-in-out;
animation: andy 1s forwards;
transform: translateX(-100%);
}
@keyframes andy{
from { transform: translateX(-100%); }
to { transform: translateX(0%); }
}
@keyframes background {
from { opacity: 0; }
to { opacity: 1; }
}
In my example I've not included the vendor-specific extensions for transition
, animation
, transform
etc. Obviously, you can add these under your own steam for compatibility.
I would highly recommend looking into CSS Animations to get the desired effect.
There are literally hundreds (if not thousands) of them out there.
Any questions, just ask!
Hope this helps :)