这是一款效果非常炫酷的纯css3 tabs选项卡动画特效插件。自从css3被更多的浏览器所支持,以前许多用javascript才能完成的特效都被css3所替代。这款插件就是使用纯css制作出效果极酷的tabs选项特效。
HTML结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | < ul class = "tabs" > < li > < input type = "radio" checked name = "tabs" id = "tab1" > < label for = "tab1" >tab 1 label > < div id = "tab-content1" class = "tab-content animated fadeIn" > ...
div >
li > < li > < input type = "radio" name = "tabs" id = "tab2" > < label for = "tab2" >tab 2 label > < div id = "tab-content2" class = "tab-content animated fadeIn" > ...
div >
li > < li > < input type = "radio" name = "tabs" id = "tab3" > < label for = "tab3" >tab 3 label > < div id = "tab-content3" class = "tab-content animated fadeIn" > ...
div >
li >
ul > |
CSS样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | body, html { height : 100% ; margin : 0 ; -webkit- font-smoothing : antialiased ; font-weight : 100 ; background : #aadfeb ; text-align : center ; font-family : helvetica ; } .tabs input[type=radio] { position : absolute ; top : -9999px ; left : -9999px ; } .tabs { width : 650px ; float : none ; list-style : none ; position : relative ; padding : 0 ; margin : 75px auto ; } .tabs li{ float : left ; } .tabs label { display : block ; padding : 10px 20px ; border-radius : 2px 2px 0 0 ; color : #08C ; font-size : 24px ; font-weight : normal ; font-family : 'Lily Script One' , helveti; background : rgba( 255 , 255 , 255 , 0.2 ); cursor : pointer ; position : relative ; top : 3px ; -webkit- transition : all 0.2 s ease-in-out; -moz- transition : all 0.2 s ease-in-out; -o- transition : all 0.2 s ease-in-out; transition : all 0.2 s ease-in-out; } .tabs label:hover { background : rgba( 255 , 255 , 255 , 0.5 ); top : 0 ; } [id^=tab]:checked + label { background : #08C ; color : white ; top : 0 ; } [id^=tab]:checked ~ [id^=tab-content] { display : block ; } .tab-content{ z-index : 2 ; display : none ; text-align : left ; width : 100% ; font-size : 20px ; line-height : 140% ; padding-top : 10px ; background : #08C ; padding : 15px ; color : white ; position : absolute ; top : 53px ; left : 0 ; box-sizing : border-box; -webkit- animation-duration : 0.5 s; -o- animation-duration : 0.5 s; -moz- animation-duration : 0.5 s; animation-duration : 0.5 s; } |
首先,为了实现javascript的功能,我们需要让CSS知道以后什么时候点击了按钮。如果是使用javascript,我们可以简单的在按钮上添加一个"click" 的class用以操控按钮。在不适用javascript的情况下,我们使用了一个小技巧,隐藏radio按钮,radio按钮通过rel属性链接到label上。当你点击了label的时候,实际上是点击了radio按钮。通过lable来触发radio按钮的“checked”属性,我们可以通过:checked来控制它们。
通过上面的html结构,你可以看到我们将radio按钮、label和选项卡内容放在DOM的同一层上。这样做的原因,是为了方便使用“~”兄弟选择器来触发同一级别上的元素。
在demo中,我们在tabs选项卡内容切换时添加了Dan Eden的CSS animation library 来添加各种动画效果。