做web的经常会遇到类似排行榜的需求, 特别是要求前n名的样式和后面人不一样. 通常大多数人对于这个需求的做法都是在后端处理好排名名次, 在前端填入内容, 然后针对前n名做特殊的样式处理. 但是这样有时候觉得很烦, 逻辑又长又啰嗦, 那有什么更好的办法呢? 这个时候我们其实可以用到CSS计数器.
CSS计数器
先看个例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<style>
body{counter-reset:num;}
div:after{content: counter(num);counter-increment:num;}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAABuCAIAAADu5AdXAAABlElEQVRoge2awZHDIBAEiYuAiIc4LgBel8MFQDC+B5IlJMHoNbtUTZdffnUBZnYXh5/fP1ef8HGGhBASQkgIISGEhBDLCdUcQyoUlcZcqKQQghehJuNFqKQQQsz1U5IPoS8SkhBCQggJIZYLVzoSQkgIsYxQzTF8ibnaCjWbTWNTY92OT0I1x17g9gVZ6E5JtH17JVQSb89eCH0Lfgpv0p6a91Mh7to0hkLdT5/Is1CzoRZmO8N7iL82jQehLjXo+bFMuJohIYSEEOsInW4j6h05iY49OYjV2UjoosCsYF+dIWdC5KjFBZqHvuwCdY3WbIOYx/pV58qcND6vUL9FNUcHfdnxOsWt9tcJVyskhJAQYkEhbsH4cmDlRsigIJoJtQzLXrZsj1QnZ+hIeBdC54LDgVBf/9gLDUZ6hiVsj/0KXZCQMySEkBBCQoiR0LmTprbTAyHqfKFj8tdTkwfFST1k4zOeD8V4FCHmQ8/r83hJDgZWHeYjvSvGQ887xFtgoSnsScDFodYUdkNCCAkhJISQEOIfwDvaFcClJCkAAAAASUVORK5CYII=" alt="" />
计数器主角:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsgAAABxCAIAAABHk8K1AAARVElEQVR4nO2cPW7ryBJGmdrazCSKlMwmLiYVYG9hgLcCAcqUDDChY0OJo4m9A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7TNbUcrm9T+A5ZQ8RKK24iS2S7BIAyEEVFs/r2bcqziMPeivknG6lt0La3u2Gl21qx6L7RobwVsidrEsE28KzQicnodO/OaJXjg2DifDsWLSKgOiKj+RmgyFKtB40G2512W03t23WKo8YK2hr6CejWrBo2hoCwM1RoujzOoxn821d8Jc3oSSZwiKZEbt1gspizrMTsHYwDs2AQTY7k7dnXkmrgjralxJ/BIDpqDB3J4RF8GzjD/w3/1fyo7D/8mZ84Rtcx8fVtAtlrURr3u3HU2cEyYYem281llYzWPxW1xPaijnHBYBxfD1hATApeB0AQA4VRlGEBZQErwMAyKHCKIqwgJLgdQAAOVQYRREWUBK8DgAghwqjKMICSoLXAQDkUGEURVhASfA6AIAcKoyiCAsoCV4HAJBDhVG0JywAAAAAMmHHAoqB1wEA5FBhFD0Ki/8DtYVLCdJrMhgAAAAASUVORK5CYII=" alt="" />
详细解释:
1、counter-reset 属性设置某个选择器出现次数的计数器的值。默认为 0。利用这个属性,计数器可以设置或重置为任何值,可以是正值或负值。如果没有提供 number,则默认为 0。
2、counter-increment 属性设置某个选取器每次出现的计数器增量。默认增量是 1。number 可以是正数、零或者负数.
3、counter(name, style),注意跟上面的content不一样,作用单纯的显示计数。
style:设置递增递减显示的文字,可以是英文字母,或者罗马文。跟ol列表支持的 list-style-type 值一样
list-style-type:disc | circle | square | decimal | lower-roman | upper-roman |
lower-alpha | upper-alpha | none | armenian | cjk-ideographic |
georgian | lower-greek | hebrew | hiragana | hiragana-iroha | katakana | katakana-iroha | lower-latin | upper-latin
counter还支持级联。也就是一个content
属性值可以有多个counter()
方法
counters(name, string),string
参数为字符串(需要引号包围的)(必须参数),表示子序号的连接字符串。例如1.1
的string
就是'.'
, 1-1
就是'-'
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<style>
.list { padding-left: 20px; counter-reset: tjq; line-height: 1.6; color: #666; }
.h1:before { content: counters(tjq, '-') '. '; counter-increment: tjq; font-family: arial black; }
</style>
</head>
<body>
<div class="list">
<div class="h1">a
<div class="list">
<div class="h1">a-1</div>
<div class="h1">a-2</div>
<div class="h1">a-3</div>
</div>
</div>
<div class="h1">b
<div class="list">
<div class="h1">b-1</div>
<div class="h1">b-2</div>
<div class="h1">b-3</div>
<div class="h1">b-4</div>
</div>
</div>
<div class="h1">c
<div class="list">
<div class="h1">c-1</div>
<div class="h1">c-2</div>
</div>
</div>
</div>
</body>
</html>
效果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMEAAAFXCAIAAACUaL0hAAAKxElEQVR4nO3d25XiyhJF0bYWPzBEbuAGZmCG+gMk5UNARmxSFQlr/p1bUKoxWFc8WkH8mwHNv7/+AzA8GoKKhqCiIahoCCoagoqGoKIhqGgIKhqCioag6t7QdTqdTqfTabr2PhL+SOeGloJo6It1bWgriIa+WK+GbpfzKWNriPoG0qWhKiBbCddpu/ntcj6dzpdbjz8Tn9Gxoema1tTe0HVKo7ldzpyKYuv8mtrTUII3dSOI2FDyVDhdOQ+FF6+hIhoaCu8vGsrOM9Vd0lfUj/+kodDiNZSdeB4viHhjFlm8hrKfny+3e0dUFBf/5goVDUFFQ1DREFQ0BBUNQUVDUNEQVDQEFQ1BRUNQ0RBUNAQVDUEVuiHr5dQHXX59u5y5Ki4RuCHrjOxBM7X3w9DQJmxD1inFQ6YaGf3eE7Eh64zsQTO16/WUxRXfPy9cQ9YZ2T+YqaWhXNCG2mdk/2CmloZy4Rra7DWRPu+UD+RhM7U0lPv1hjwztTSU++2Gns/Upi+zypdINJQbrCHz7XvM1NJQ7rcb8s3U0lDutxvyzdTSUC5wQxgEDUFFQ1DREFQ0BBUNQUVDUNEQVDQEFQ1BRUNQ0RBUNAQVDUFFQ1CFbqj1Mvn8ClnbBdUG2RgSX9y/CtxQ21Dp3nxZj8f4fpzHb30clAvR5nkO3FDj9Ol2s8fD22mcuRr5YCfWKmJDhtnnnetf14haz0TOSf3rxBPaXbiGxNln63nIvX+YvWqroA05Z5/TAFsTcu0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UNEQVDQEFQ1BRUNQ0RBUNAQVDUFFQ1DREFQ0BBUNQUVDUMVsyDkju1y21PNikdvlzMUouXgNuWdkjxiFvB+DhjLRGnLPyKbp9XmMGdd9IlxDjhnZF5dMNh2y5ZrJ6/T4o64TDRWiNVR4P1W0tybG8BibZ2ppqBK3ocZLqu83m66usX7HDAkNVQZpaHmkiz2K1/072L4awlYEDVXiNvSQP5t1aMg4ykZDlWANvZuRPeY8VCxzrf5CGsoEa+jtjOwLuw19fKaWhirRGno3I/uCpyH7TC0NVeI1NHs/8HE1VB7t7ZFoqBKyIQyFhqCiIahoCCoagoqGoKIhqGgIKhqCioagoiGoaAgqGoKKhqCK2ZBxzrW65KjDxRnZn8QqiFS8hoxzrnuzQR9/mO8HyVdFcBHRIlpD1jnX7eePR7jHNGp1eSM7RFLhGrLNue78eI2o9Uzk2Q3CbqNEtIYK5l2w1vOQb3fsdeLJbBO3Ic/pIb1P011c55N19h7zPA/TUMuca/aztuicc66cgzJxG3pom3PNg+s258oZaE+whlxzrv6Pbixzrtn7eySCNWSfc33zsulDc67bt4ugEq0h45zrs08Y12g+Muea3wiFeA3NhjnXlwk1NVT+kr0bPTkKUT2EbAhDoSGoaAgqGoKKhqCiIahoCCoagoqGoKIhqGgIKhqCioagoiGoaAgqGoKKhqCiIahoCCoagoqGoKIhqGgIKhqCioagoiGoaAgqGoKKhqCiIahoCCoaguo/XWCN7Gjz81oAAAAASUVORK5CYII=" alt="" />
具体的小伙伴们可以敲敲代码试试,自然就懂了其中的逻辑的。说其逻辑还真不知道怎么说。具体的可以看看:
http://www.zhangxinxu.com/wordpress/2014/12/css-counters-pseudo-class-checked-numbers/
http://www.zhangxinxu.com/wordpress/2014/08/css-counters-automatic-number-content/
兼容性:
1、相比传统的ol
,ul
列表计数,CSS计数器的优势就在于灵活与强大,不足就是IE6/IE7不支持。
2、一个元素,如果设置了counter-increment
, 但是其display
的属性值是none
或者含有hidden
属性(针对支持浏览器),则此计数值是不会增加的。
总结:
css计数器还是有一定的便利的,毕竟现在ie6 7,很多都不去兼容,放可以大胆使用,相对于前面所说的写死的方法灵活多了。
彩蛋:
有时排行榜前几位会有不同的样式,大可以用css3(:nth-child) 直接实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<style>
body {
counter-reset: num;
} div:after {
content: counter(num);
counter-increment: num;
}
//小于等于3的内容字体颜色设置红色,且加粗
div:nth-child(-n+3) {
color:red;
font-weight:bold;
}
//大于等于4的内容改变背景颜色#090
div:nth-child(n+4) {
background: #090;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>