今天(昨天)又發現一個知識盲區
css3的:target標簽,之前學習的時候就是一眼掃過,說是認識了,但其實也就記了三分鍾,合上書就全忘光了。
直到昨天,遇到一個有意思的題目,用css3新特性做一個類似tab標簽的小效果,才讓我又重新認識了 :target 選擇器
w3c上對於target選擇器的解釋是:
試一下他的效果就能對target的作用明白了:http://www.w3school.com.cn/tiy/t.asp?f=css_sel_target
原理: 也就是給一個元素A設定id,另一個元素B指定跳轉到這個id,然后就向 a:hover 那樣,在css中設定 “元素:target”並改變樣式,那么點擊B元素,就會根據你的設定改變A的樣式。
以下就是我根據原理做出來的一個樣式
很明顯,就是一個tab切換效果,css制作。
代碼如下:
html
1 <div class="swiper-box">
2 <div class="swiper-cont">
3 <div class="swiper1" id="swiper1">
4 </div>
5 <div class="swiper2" id="swiper2">
6 </div>
7 <div class="swiper3" id="swiper3">
8 </div>
9 </div>
10 <div class="swiper-num">
11 <a href="#swiper1">1</a>
12 <a href="#swiper2">2</a>
13 <a href="#swiper3">3</a>
14 </div>
15 </div>
css
1 .swiper-box{
2 position: relative;
3 width: 500px;
4 height: 300px;
5 margin: 20px auto;
6 background: #f1f1f1;
7 }
8 .swiper-cont div,.swiper1,.swiper2,.swiper3{
9 width: 0%;
10 height: 300px;
11 position: absolute;
12 top: 0;
13 left: 0;
14 -webkit-transition: width .5s linear;
15 -moz-transition: width .5s linear;
16 -ms-transition: width .5s linear;
17 -o-transition: width .5s linear;
18 transition: width .5s linear;
19 }
20 .swiper1{
21 background: -webkit-linear-gradient(bottom, #fba555, #ffed6c 75%);
22 background: -moz-linear-gradient(bottom, #fba555, #ffed6c 75%);
23 background: -ms-linear-gradient(bottom, #fba555, #ffed6c 75%);
24 background: -o-linear-gradient(bottom, #fba555, #ffed6c 75%);
25 background: linear-gradient(to top, #fba555, #ffed6c 75%);
26 }
27 .swiper2{
28 background: -webkit-linear-gradient(right, #55d5fb, #fd74a7 75%);
29 background: -moz-linear-gradient(right, #55d5fb, #fd74a7 75%);
30 background: -ms-linear-gradient(right, #55d5fb, #fd74a7 75%);
31 background: -o-linear-gradient(right, #55d5fb, #fd74a7 75%);
32 background: linear-gradient(to left, #55d5fb, #fd74a7 75%);
33 }
34 .swiper3{
35 background: -webkit-linear-gradient(bottom right, #55fb69, #6cfff1 75%);
36 background: -moz-linear-gradient(bottom right, #55fb69, #6cfff1 75%);
37 background: -ms-linear-gradient(bottom right, #55fb69, #6cfff1 75%);
38 background: -o-linear-gradient(bottom right, #55fb69, #6cfff1 75%);
39 background: linear-gradient(to top left, #55fb69, #6cfff1 75%);
40 }
41 .swiper-num{
42 position: absolute;
43 bottom: 0;
44 right: 0;
45 display: inline-block;
46 z-index: 9;
47 }
48 .swiper-num a{
49 display: inline-block;
50 margin-left: 10px;
51 padding: 10px 20px;
52 color: #333;
53 font-size: 14px;
54 text-decoration: none;
55 font-weight: bold;
56 background: rgba(255,255,255,.45);
57 }
58 .swiper-num a:hover,.swiper-num a:active{
59 color: red;
60 cursor: pointer;
61 background: rgba(255,255,255,.95);
62 }
63 .swiper-box :target{
64 width: 100%;
65 -webkit-transition: width .5s linear;
66 -moz-transition: width .5s linear;
67 -ms-transition: width .5s linear;
68 -o-transition: width .5s linear;
69 transition: width .5s linear;
70 }
71 .in-cont{
72 height: 60px;
73 }
核心關鍵點我覺得除了第63行的:target選擇器以外,還有就是,所謂的指定target目標id的元素,也就是使用了(href=“#xxx”)屬性的元素,一定要是a鏈接,(比如我div.swiper-num里邊的a鏈接就是zhongdian!!!)
曾經我用span,然后搗鼓到了晚上八點最后明白需要a后才下班。。。
難道href是a的御用嗎
更多的技巧這篇文章做的很仔細:http://www.css88.com/archives/6256