您的位置:首页 > Web前端 > CSS

深入理解CSS伪类

2016-05-23 17:33 627 查看
×
目录
[1]锚点 [2]UI元素 [3]结构伪类[4]其他

前面的话

  伪类经常与伪元素混淆,伪元素的效果类似于通过添加一个实际的元素才能达到,而伪类的效果类似于通过添加一个实际的类来达到。实际上css3为了区分两者,已经明确规定了伪类用一个冒号来表示,而伪元素则用两个冒号来表示。本文将详细介绍伪类的详细知识

锚点

  关于锚点<a>,有常见的5个伪类,分别是:link,:hover,:active,:focus,:visited

a:link{background-color:pink;}/*品红,未访问*/


a:hover{background-color:lightblue;}/*浅蓝,鼠标悬停*/


a:active{background-color:lightgreen;}/*浅绿,正被点击*/


a:focus{background-color:lightgrey;}/*浅灰,拥有焦点*/


a:visited{color:orange;}/*字体颜色为橙色,已被访问*/
/*[注意]visited伪类只能设置字体颜色的样式*/


伪类顺序

  对于伪类顺序,有一个口诀是love-hate,代表着伪类的顺序是link、visited、focus、hover、active。但是否伪类的顺序只能如此呢?为什么是这个顺序呢?

  CSS层叠中有一条法则十分重要,就是后面覆盖前面,所以伪类的顺序是需要精心考虑的。

  【1】link和visited必须在最前面,且没有先后顺序,否则link或visited的效果将被覆盖

  [注意]link和visited称为静态伪类,只能应用于超链接

  【2】hover、active、focus这三个伪类必须是focus、hover、active的顺序,因为在focus状态下,也需要触发hover和active,而要触发active一定要先触发hover,所以active要放在hover后面

  [注意]hover、active、focus称为动态伪类,可应用于任何元素,但IE7-浏览器不支持:focus,:hover和:active在IE6-浏览器下只支持给<a>设置

  所以最终的顺序只有两种:link、visited、focus、hover、active或visited、link、focus、hover、active

a:link{background-color:pink;}/*品红,未访问*/
a:visited{color:orange;}/*字体颜色为橙色,已被访问*/
a:focus{background-color:lightgrey;}/*浅灰,拥有焦点*/
a:hover{background-color:lightblue;}/*浅蓝,鼠标悬停*/
a:active{background-color:lightgreen;}/*浅绿,正被点击*/


UI元素伪类

  UI元素伪类包括:enabled、:disabled、:checked三个,主要针对于HTML中的form元素,IE8-浏览器不支持

:enabled    可用状态
:disabled   不可用状态
:checked    选中状态


input:enabled{
border: 1px solid black;
background-color: transparent;
}
input:disabled{
border: none;
background-color: gray;
}
input:checked{
outline: 2px solid lightblue;
}


<button onclick = "btn.disabled = false;">按钮可用</button>
<button onclick = "btn.disabled = true;">按钮不可用</button>
<input type="button" id="btn" value="按钮">
<br>
<label>Male<input type="radio" name="sex" /></label>
<label>Female<input type="radio" name="sex"  /></label>


  [注意]input和:和enabled之间都不可以有空格

结构伪类

  结构伪类可分为以下3种情况,IE8-浏览器不支持

  //以下情况都是E为父元素,F为子元素

【1】:nth-child(n)、:nth-last-child(n)、first-child、last-child、:only-child

E F:nth-child(n)           选择父元素的第n个子元素
E F:nth-last-child(n)      选择父元素的倒数第n个子元素
E F:first-child            父元素的第一个子元素,与E F:nth-child(1)等同
E F:last-child             父元素的最后一个子元素,与E F:nth-last-child(1)等同
E F:only-child             选择父元素中只包含一个子元素


  [注意]:firsr-child和:last-child只有IE6-浏览器不支持

  n可以是整数(从1开始),也可以是公式,也可以是关键字(even、odd)

p:first-child       代表的并不是<p>的第一个子元素,而是<p>元素是某元素的第一个子元素
p > i:first-child    匹配所有<p>元素中的第一个<i>元素
p:first-child i     匹配所有作为第一个子元素的<p>元素中的所有<i>元素


li:nth-child(odd){color: red;}
li:nth-last-child(3){color: green;}
li:first-child{color: blue;}
li:last-child{color: yellow;}
div:only-child{background-color:lightgrey;}


<ul>
<li><div>第一个DIV</div></li>
<li><div>第二个DIV</div></li>
<li><div>第三个DIV</div></li>
<li><div>第四个DIV</div></li>
<li><div>第五个DIV</div></li>
<li><div>第六个DIV</div></li>
</ul>


【2】:nth-of-type(n)、:nth-last-of-type(n)、:first-of-type、:last-of-type、:only-of-type

E F:nth-of-type(n)          选择父元素的具有指定类型的第n个子元素
E F:nth-last-of-type(n)     选择父元素的具有指定类型的倒数第n个子元素
E F:first-of-type           选择父元素中具有指定类型的第1个子元素,与E F:nth-of-type(1)相同
E F:last-of-type            选择父元素中具有指定类型的最后1个子元素,与E F:nth-last-of-type(1)相同
E F:only-of-type           选择父元素中只包含一个同类型的子元素


.box div:nth-of-type(even){color: red;}
.box p:nth-last-of-type(3){color: green;}
.box div:first-of-type{color: blue;}
.box p:last-of-type{color: yellow;}
.box div:only-of-type{color: pink;}


<div class="box">
<div>第一个div</div>
<p>第一个p</p>
<div>第二个div</div>
<p>第二个p</p>
<div class="in">第三个div</div>
<p>第三个p</p>
</div>
<div class="box">
<div>第四个div</div>
</div>


【3】:root、:not、:empty、:target

:root         选择文档的根元素,即<html>元素
:not          选择除某个元素之外的所有元素
:empty         选择没有子元素的元素,而且该元素也不包含任何文本节点
:target       匹配锚点对应的目标元素


  [注意]:not选择器只有safari9+及ios9.2+浏览器支持

:root{color:red;}
div:not{background-color: lightgrey;}
p:empty{height:30px;width:30px;background:pink;}
:target{color:blue;}


<body>
<a href="#test">测试</a>
<div>第一个div</div>
<p>第一个p</p>
<div id="test">第二个div</div>
<p>第二个p</p>
<p></p>
</body>


其他

【1】:lang()   匹配某个语言,IE7-浏览器不支持

p:lang(en) 匹配语言为"en"的<p>


【2】不仅可以使用单一伪类,也可以伪类结合使用

  [注意]顺序无关

div:hover:first-child{background-color: lightgreen;}
div:last-of-type:active{background-color: lightblue;}


<div>第一个div</div>
<div>第二个div</div>


// var all = document.getElementById('cnblogs_post_body').children;
var select = [];

for(var i = 1; i < all.length; i++){
if(all[i].getAttribute('id')){
if(all[i].getAttribute('id').match(/anchor\d+$/)){
select.push(all[i]);
}
}

}
var wheel = function(e){
e = e || event;
var data;
if(e.wheelDelta){
data = e.wheelDelta;
}else{
data = -e.detail * 40;
}
for(var i = 0; i < select.length; i++){
if(select[i].getBoundingClientRect().top > 0){
return;
}
if(select[i].getBoundingClientRect().top <= 0 && select[i+1]){
if(select[i+1].getBoundingClientRect().top > 0){
change(oCon.children[i+2])
}
}else{
change(oCon.children[select.length+1])
}
}

}
document.body.onmousewheel = wheel;
document.body.addEventListener('DOMMouseScroll',wheel,false);

var oCon = document.getElementById("content");
var close = oCon.getElementsByTagName('span')[0];
close.onclick = function(){
if(this.innerHTML == '显示目录'){
this.innerHTML = '×';
this.style.background = '';
oCon.style.border = '2px solid #ccc';
oCon.style.width = '';
oCon.style.height = '';
oCon.style.overflow = '';
oCon.style.lineHeight = '30px';
}else{
this.innerHTML = '显示目录';
this.style.background = '#3399ff';
oCon.style.border = 'none';
oCon.style.width = '60px';
oCon.style.height = '30px';
oCon.style.overflow = 'hidden';
oCon.style.lineHeight = '';
}
}
for(var i = 2; i < oCon.children.length; i++){
oCon.children[i].onmouseover = function(){
this.style.color = '#3399ff';
}
oCon.children[i].onmouseout = function(){
this.style.color = 'inherit';
if(this.mark){
this.style.color = '#3399ff';
}

}
oCon.children[i].onclick = function(){
change(this);
}
}

function change(_this){
for(var i = 2; i < oCon.children.length; i++){
oCon.children[i].mark = false;
oCon.children[i].style.color = 'inherit';
oCon.children[i].style.textDecoration = 'none';
oCon.children[i].style.borderColor = 'transparent';
}
_this.mark = true;
_this.style.color = '#3399ff';
_this.style.textDecoration = 'underline';
_this.style.borderColor = '#2175bc';
}
// ]]>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: