we want to share a simple parallax content slider with you. Using CSS animations, we’ll control the animation of each single element in the slider and create a parallax effect by animating the background of the slider itself.
The idea for this comes from the slider of the Kendo UI homepage, a framework for modern HTML UI. After we got some requests and questions about how to do something like that, we decided to recreate the effect.
How it works
The slider contains several slides and each one will have an h2 element, a paragraph, a link and a division with an image:
01 | < div id = "da-slider" class = "da-slider" > |
03 | < div class = "da-slide" > |
04 | < h2 >Some headline</ h2 > |
05 | < p >Some description</ p > |
06 | < a href = "#" class = "da-link" >Read more</ a > |
08 | < img src = "images/1.png" alt = "image01" /> |
12 | < div class = "da-slide" > |
18 | < nav class = "da-arrows" > |
19 | < span class = "da-arrows-prev" ></ span > |
20 | < span class = "da-arrows-next" ></ span > |
The core of this slider is the animations for each one of the elements. We’ll control the behavior of the elements by giving a “direction class” to the respective slide. For example, when we slide the current slide to the right, we will give it the class “da-slide-toright”. There will be four different classes for each of the possible slide directions and origins:
- .da-slide-fromright
- .da-slide-fromleft
- .da-slide-toright
- .da-slide-toleft
Given these classes, we can control the animation of each element:
02 | .da-slide-fromright h 2 { |
03 | animation: fromRightAnim 1 0.6 s ease-in 0.8 s both ; |
06 | animation: fromRightAnim 2 0.6 s ease-in 0.8 s both ; |
08 | .da-slide-fromright .da-link{ |
09 | animation: fromRightAnim 3 0.4 s ease-in 1.2 s both ; |
11 | .da-slide-fromright .da-img{ |
12 | animation: fromRightAnim 4 0.6 s ease-in 0.8 s both ; |
16 | @keyframes fromRightAnim 1 { |
17 | 0% { left : 110% ; opacity: 0 ; } |
18 | 100% { left : 10% ; opacity: 1 ; } |
20 | @keyframes fromRightAnim 2 { |
21 | 0% { left : 110% ; opacity: 0 ; } |
22 | 100% { left : 10% ; opacity: 1 ; } |
24 | @keyframes fromRightAnim 3 { |
25 | 0% { left : 110% ; opacity: 0 ; } |
26 | 1% { left : 10% ; opacity: 0 ; } |
27 | 100% { left : 10% ; opacity: 1 ; } |
29 | @keyframes fromRightAnim 4 { |
30 | 0% { left : 110% ; opacity: 0 ; } |
31 | 100% { left : 60% ; opacity: 1 ; } |