Have you ever used ordinary accordion plugins in your projects, I believe that yes, and, most of them use javascript to work (or jQuery). Usually we use such plugins (accordions) to build a promo with images, a little photo gallery, or in case if we have to build an advertisement with a list of product features. We did some research and came to the conclusion that sometimes we don’t need to use any script in order to build accordions. We can just use the power of CSS3. We can handle the OnClick event and use custom animation.
I prepared three versions with accordions. For the first demonstration we use jQuery to build an accordion. I selected liteAccordion jQuery plugin (http://nicolahibbert.com/demo/liteAccordion/) as a accordion plugin for out test purposes. Look how it works:
It looks nice, doesn’t it? Let’s review HTML markup of this page:
04 | < meta charset = "utf-8" /> |
05 | < title >How to turn jQuery accordion into pure CSS3 accordion | Script Tutorials</ title > |
07 | < link href = "css/layout.css" rel = "stylesheet" /> |
08 | < link href = "css/liteaccordion.css" rel = "stylesheet" /> |
12 | < script src = "js/jquery.easing.1.3.js" ></ script > |
14 | < script src = "js/liteaccordion.jquery.js" ></ script > |
16 | $(document).ready(function(){ |
17 | $('#js_version').liteAccordion({ |
20 | enumerateSlides : true, |
23 | easing: 'easeInOutSine' |
29 | < h1 >jQuery accordion (liteAccordion) version</ h1 > |
30 | < div id = "js_version" class = "accordion" > |
32 | < li data-slide-name = "slide1" > |
33 | < h2 >< span >Slide One</ span ></ h2 > |
35 | < img src = "images/1.jpg" alt = "Slide One" /> |
38 | < li data-slide-name = "slide2" > |
39 | < h2 >< span >Slide Two</ span ></ h2 > |
41 | < img src = "images/2.jpg" alt = "Slide Two" /> |
44 | < li data-slide-name = "slide3" > |
45 | < h2 >< span >Slide Three</ span ></ h2 > |
47 | < img src = "images/3.jpg" alt = "Slide Three" /> |
50 | < li data-slide-name = "slide4" > |
51 | < h2 >< span >Slide Four</ span ></ h2 > |
53 | < img src = "images/4.jpg" width = "768" alt = "Slide Four" /> |
56 | < li data-slide-name = "slide5" > |
57 | < h2 >< span >Slide Five</ span ></ h2 > |
59 | < img src = "images/5.jpg" alt = "Slide Five" /> |
64 | < p >Please enable JavaScript to get the full experience.</ p > |
In the header we added all the necessary libraries and styles (jQuery, jquery.easing and liteAccordion library) as well as accordion initialization code. In the body section we can see basic accordion structure (OL-LI list) with slides. I think that this is one of usual structures for accordions.
Now, I think that it is the very time to start turning it into pure CSS3 accordion. In the beginning – we have to remove all JS scripts from our page, we can remove liteaccordion.css as well. We are going to prepare our own CSS styles. Also, we have to add <A> links to the headers of our slides (because we should be able to switch between slides). In the result we should get something like that:
04 | < meta charset = "utf-8" /> |
06 | < link href = "css/layout.css" rel = "stylesheet" /> |
07 | < link href = "css/main.css" rel = "stylesheet" /> |
10 | < h1 >Pure CSS3 accordion version without animation</ h1 > |
11 | < div class = "accordion css3accordion" > |
12 | < span id = "slide1" ></ span > |
13 | < span id = "slide2" ></ span > |
14 | < span id = "slide3" ></ span > |
15 | < span id = "slide4" ></ span > |
16 | < span id = "slide5" ></ span > |
19 | < a href = "#slide1" >< h2 >< span >Slide One</ span ></ h2 ></ a > |
21 | < img src = "images/1.jpg" alt = "Slide One" /> |
25 | < a href = "#slide2" >< h2 >< span >Slide Two</ span ></ h2 ></ a > |
27 | < img src = "images/2.jpg" alt = "Slide Two" /> |
31 | < a href = "#slide3" >< h2 >< span >Slide Three</ span ></ h2 ></ a > |
33 | < img src = "images/3.jpg" alt = "Slide Three" /> |
37 | < a href = "#slide4" >< h2 >< span >Slide Four</ span ></ h2 ></ a > |
39 | < img src = "images/4.jpg" alt = "Slide Four" /> |
43 | < a href = "#slide5" >< h2 >< span >Slide Five</ span ></ h2 ></ a > |
45 | < img src = "images/5.jpg" alt = "Slide Five" /> |
As you can see – I added several <span> objects. By default – all of them are hidden, but we have to use them in order to handle onclick events. Now, we are ready to start writing own styles for our CSS3 accordion. Firstly, we have to define styles for our parent object and inner spans:
03 | border : 9px solid #353535 ; |
05 | padding : 5px 5px 6px 0 ; |
10 | -webkit-box-shadow: 0 -1px 0 #555 inset , 0 5px 15px rgba( 0 , 0 , 0 , 0.5 ); |
11 | -moz-box-shadow: 0 -1px 0 #555 inset , 0 5px 15px rgba( 0 , 0 , 0 , 0.5 ); |
12 | -ms-box-shadow: 0 -1px 0 #555 inset , 0 5px 15px rgba( 0 , 0 , 0 , 0.5 ); |
13 | -o-box-shadow: 0 -1px 0 #555 inset , 0 5px 15px rgba( 0 , 0 , 0 , 0.5 ); |
14 | box-shadow: 0 -1px 0 #555 inset , 0 5px 15px rgba( 0 , 0 , 0 , 0.5 ); |
17 | .css 3 accordion > span { |
As I told – they are hidden. Now we can define styles for our slides and headers:
15 | -webkit-transition: all 0.9 s ease-in-out; |
16 | -moz-transition: all 0.9 s ease-in-out; |
17 | -ms-transition: all 0.9 s ease-in-out; |
18 | -o-transition: all 0.9 s ease-in-out; |
19 | transition: all 0.9 s ease-in-out; |
40 | -webkit-backface- visibility : hidden ; |
41 | -webkit-transform: translateX( -100% ) rotate( -90 deg); |
42 | -webkit-transform-origin: right top ; |
43 | -moz-transform: translateX( -100% ) rotate( -90 deg); |
44 | -moz-transform-origin: right top ; |
45 | -ms-transform: translateX( -100% ) rotate( -90 deg); |
46 | -ms-transform-origin: right top ; |
47 | -o-transform: translateX( -100% ) rotate( -90 deg); |
48 | -o-transform-origin: right top ; |
49 | transform: translateX( -100% ) rotate( -90 deg); |
50 | transform-origin: right top ; |
52 | .css 3 accordion h 2 span { |
53 | background-color : #353535 ; |
60 | -webkit-user-select: none ; |
61 | -moz-user-select: none ; |
62 | -ms-user-select: none ; |
66 | .css 3 accordion h 2 span:hover { |
67 | background-color : #353535 ; |
68 | background : -webkit-gradient(linear, left top , right top , color-stop( 0% , #353535 ), color-stop( 100% , #555555 )); |
69 | background : -webkit-linear-gradient( left , #353535 0% , #555555 100% ); |
70 | background : -moz-linear-gradient( left , #353535 0% , #555555 100% ); |
71 | background : -ms-linear-gradient( left , #353535 0% , #555555 100% ); |
72 | background : -o-linear-gradient( left , #353535 0% , #555555 100% ); |
73 | background : linear-gradient( left , #353535 0% , #555555 100% ); |
74 | filter: progid:DXImageTransform.Microsoft.gradient( startColorstr= '#353535' , endColorstr= '#555555' ,GradientType= 1 ); |
77 | .css 3 accordion li div { |
Now I’d like to show you how to display a counter object in every header. I’m going to use :after pseudo element. I hope that you know that :after selector inserts content after the selected element, so we can do something like that:
02 | .css 3 accordion h 2 span:after { |
07 | text-shadow : -1px 1px 0 #555555 ; |
10 | -webkit-transform: rotate( 90 deg); |
11 | -moz-transform: rotate( 90 deg); |
12 | -ms-transform: rotate( 90 deg); |
13 | -o-transform: rotate( 90 deg); |
14 | transform: rotate( 90 deg); |
17 | li.slide 1 h 2 span:after { |
20 | li.slide 2 h 2 span:after { |
23 | li.slide 3 h 2 span:after { |
26 | li.slide 4 h 2 span:after { |
29 | li.slide 5 h 2 span:after { |
Finally, in order to complete our accordion we have to implement onclick behavior:
02 | #slide 1: target ~ ol li.slide 1 , |
03 | #slide 2: target ~ ol li.slide 2 , |
04 | #slide 3: target ~ ol li.slide 3 , |
05 | #slide 4: target ~ ol li.slide 4 , |
06 | #slide 5: target ~ ol li.slide 5 { |
09 | #slide 1: target ~ ol li.slide 1 span, |
10 | #slide 2: target ~ ol li.slide 2 span, |
11 | #slide 3: target ~ ol li.slide 3 span, |
12 | #slide 4: target ~ ol li.slide 4 span, |
13 | #slide 5: target ~ ol li.slide 5 span { |
15 | background : -webkit-gradient(linear, left top , right top , color-stop( 0% , #353535 ), color-stop( 100% , #555555 )); |
16 | background : -webkit-linear-gradient( left , #353535 0% , #555555 100% ); |
17 | background : -moz-linear-gradient( left , #353535 0% , #555555 100% ); |
18 | background : -ms-linear-gradient( left , #353535 0% , #555555 100% ); |
19 | background : -o-linear-gradient( left , #353535 0% , #555555 100% ); |
20 | background : linear-gradient( left , #353535 0% , #555555 100% ); |
21 | filter: progid:DXImageTransform.Microsoft.gradient( startColorstr= '#353535' , endColorstr= '#555555' ,GradientType= 1 ); |
That’s all, our own CSS3-based accordion is complete! You can check it in action:
But that’s not all for today, as a bonus, I prepared third demo for you with animated accordion.
In order to do it I recommend disable onclick behavior, and apply new animation:
03 | -webkit-animation-name: anim_slides; |
04 | -webkit-animation-duration: 25.0 s; |
05 | -webkit-animation-timing-function: ease-in-out; |
06 | -webkit-animation-iteration-count: infinite; |
07 | -webkit-animation- direction : normal ; |
08 | -webkit-animation-delay: 0 ; |
09 | -webkit-animation-play-state: running; |
10 | -webkit-animation-fill-mode: forwards; |
11 | -moz-animation-name: anim_slides; |
12 | -moz-animation-duration: 25.0 s; |
13 | -moz-animation-timing-function: ease-in-out; |
14 | -moz-animation-iteration-count: infinite; |
15 | -moz-animation- direction : normal ; |
16 | -moz-animation-delay: 0 ; |
17 | -moz-animation-play-state: running; |
18 | -moz-animation-fill-mode: forwards; |
20 | .css 3 accordion li:nth-child( 2 ) { |
21 | -webkit-animation-delay: 5.0 s; |
22 | -moz-animation-delay: 5.0 s; |
24 | .css 3 accordion li:nth-child( 3 ) { |
25 | -webkit-animation-delay: 10.0 s; |
26 | -moz-animation-delay: 10.0 s; |
28 | .css 3 accordion li:nth-child( 4 ) { |
29 | -webkit-animation-delay: 15.0 s; |
30 | -moz-animation-delay: 15.0 s; |
32 | .css 3 accordion li:nth-child( 5 ) { |
33 | -webkit-animation-delay: 20.0 s; |
34 | -moz-animation-delay: 20.0 s; |
36 | @-webkit-keyframes anim_slides { |
50 | @-moz-keyframes anim_slides { |
[sociallocker]
[/sociallocker]
Conclusion
Now that is all for today. We have just created three different demos: the first one with jQuery, the second and the third – with pure CSS3. I hope that you like it. Good luck!