CSS計(jì)數(shù)器counter的使用技巧

這篇文章主要介紹“CSS計(jì)數(shù)器counter的使用技巧”,在日常操作中,相信很多人在CSS計(jì)數(shù)器counter的使用技巧問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”CSS計(jì)數(shù)器counter的使用技巧”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

為金平等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及金平網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、金平網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

初始化CSS計(jì)數(shù)器

為了好理解,我們使用<OL> 和 <LI> 元素來做演示。首先我們要重置計(jì)數(shù)器,讓它歸零,并給它指定一個(gè)名稱:

CSS Code復(fù)制內(nèi)容到剪貼板

  1. ol.slides {   

  2.  countercounter-reset: slideNum;   

  3. }  

這個(gè)計(jì)數(shù)器叫slideNum,下面的例子都都要使用它。

CSS計(jì)數(shù)器的自增

為了是計(jì)數(shù)器能夠自增,我們需要使用counter-increment,并把計(jì)數(shù)器的名稱跟到后面:

CSS Code復(fù)制內(nèi)容到剪貼板

  1. ol.slides > li {   

  2.  countercounter-increment: slideNum;   

  3. }  

這樣,在CSS選擇器下,每遇到一個(gè)符合條件li元素,counter-increment就會(huì)被調(diào)用一次,計(jì)數(shù)就是增加1。需要注意的是,這里的CSS選擇器里使用了>符號(hào),這樣是為了濾掉有可能多重嵌套的li元素。否者你的計(jì)數(shù)值就會(huì)不是你想要的。

使用計(jì)數(shù)值

如果只計(jì)數(shù)而無法顯示,那這個(gè)計(jì)數(shù)器也沒多大用處,所以就有了counter()命令來輸出計(jì)數(shù)器里的值,可以用在content屬性里:

CSS Code復(fù)制內(nèi)容到剪貼板

  1. ol.slides li:after {   

  2.  content: "[" counter(slideNum) "]";   

  3. }  

有趣的是,這個(gè)counter()命令還可以接受第二個(gè)參數(shù),當(dāng)作同時(shí)計(jì)算多個(gè)元素時(shí)數(shù)據(jù)的分隔符:

假設(shè)有這樣的HTML:

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <ol class="toc">  

  2.  <li>Intro</li>  

  3.  <li>Topic   

  4.   <ol>  

  5.    <li>Subtopic</li>  

  6.    <li>Subtopic</li>  

  7.    <li>Subtopic</li>  

  8.   </ol>  

  9.  </li>  

  10.  <li>Topic   

  11.   <ol>  

  12.    <li>Subtopic</li>  

  13.    <li>Subtopic</li>  

  14.    <li>Subtopic</li>  

  15.   </ol>  

  16.  </li>  

  17.  <li>Closing</li>     

  18. </ol>  

我們這樣來寫:

CSS Code復(fù)制內(nèi)容到剪貼板

  1. ol.toc, ol.toc ol {   

  2.  countercounter-reset: toc;   

  3. }   

  4. ol li {   

  5.  countercounter-increment: toc;   

  6. }   

  7. .toc li:before {   

  8.  content: "(Item " counters(toc, ".") ")";   

  9. }  

會(huì)輸出下面的結(jié)果

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <ol class="toc">  

  2.  <li>(Item 1)Intro</li>  

  3.  <li>(Item 2)Topic   

  4.   <ol>  

  5.    <li>(Item 2.1)Subtopic</li>  

  6.    <li>(Item 2.2)Subtopic</li>  

  7.    <li>(Item 2.3)Subtopic</li>  

  8.   </ol>  

  9.  </li>  

  10.  <li>(Item 3)Topic   

  11.   <ol>  

  12.    <li>(Item 3.1)Subtopic</li>  

  13.    <li>(Item 3.2)Subtopic</li>  

  14.    <li>(Item 3.3)Subtopic</li>  

  15.   </ol>  

  16.  </li>  

  17.  <li>(Item 4)Closing</li>     

  18. </ol>  

你可以發(fā)現(xiàn),當(dāng)需要顯示這種聯(lián)級(jí)嵌套序號(hào)時(shí),這種技術(shù)是非常的有用的。很像微軟WORD里面文檔的多重序號(hào)。

在 counter-reset 屬性中,定義了一個(gè)隨機(jī) ID,其默認(rèn)值是0。你可以在 counter-increment 屬性中定義另外一個(gè)數(shù)字,作為計(jì)數(shù)的步長(zhǎng)。

例如:counter-increment: i 2 將值顯示偶數(shù)。

大多時(shí)候,CSS計(jì)數(shù)器都是配合:after和:before偽元素使用,我曾看到過有人在幻燈片、視頻頁(yè)面和文檔里用過CSS計(jì)數(shù)器。相信你會(huì)找到其它可以使用它的地方。

CSS 計(jì)數(shù)器進(jìn)階
利用 CSS 計(jì)數(shù)器,能統(tǒng)計(jì)被用戶選擇的復(fù)選框個(gè)數(shù):

CSS Code復(fù)制內(nèi)容到剪貼板

  1. <div class="languages">     

  2.   <input id="c" type="checkbox"><label for="c">C</label>   

  3.   <input id="C++" type="checkbox"><label for="C++">C++</label>   

  4.   <input id="C#" type="checkbox"><label for="C#">C#</label>   

  5.   <input id="Java" type="checkbox"><label for="Java">Java</label>   

  6.   <input id="JavaScript" type="checkbox"><label for="JavaScript">JavaScript</label>   

  7.   <input id="PHP" type="checkbox"><label for="PHP">PHP</label>   

  8.   <input id="Python" type="checkbox"><label for="Python">Python</label>   

  9.   <input id="Ruby" type="checkbox"><label for="Ruby">Ruby</label>   

  10. </div>     

  11. <p class="total">     

  12.   Total selected:   

  13. </p>     

  14. .languages {   

  15.   countercounter-reset: characters;   

  16. }   

  17. input:checked {     

  18.   countercounter-increment: characters;   

  19. }   

  20. .total:after {   

  21.   content: counter(characters);   

  22. }   

在這個(gè)示例中,我們會(huì)增加 input:checked 的數(shù)量并打印出來。
CSS計(jì)數(shù)器counter的使用技巧

你還能創(chuàng)建一個(gè)小型計(jì)算器:

CSS Code復(fù)制內(nèi)容到剪貼板

  1. <div class="numbers">     

  2.   <input id="one" type="checkbox"><label for="one">1</label>   

  3.   <input id="two" type="checkbox"><label for="two">2</label>   

  4.   <input id="three" type="checkbox"><label for="three">3</label>   

  5.   <input id="four" type="checkbox"><label for="four">4</label>   

  6.   <input id="five" type="checkbox"><label for="five">5</label>   

  7.   <input id="one-hundred" type="checkbox"><label for="one-hundred">100</label>   

  8. </div>     

  9. <p class="sum">     

  10.   Sum    

  11. </p>    

  12. .numbers {   

  13.   countercounter-reset: sum;   

  14. }   

  15.   

  16. #one:checked { countercounter-increment: sum 1; }   

  17. #two:checked { countercounter-increment: sum 2; }   

  18. #three:checked { countercounter-increment: sum 3; }   

  19. #four:checked { countercounter-increment: sum 4; }   

  20. #five:checked { countercounter-increment: sum 5; }   

  21. #one-hundred:checked { countercounter-increment: sum 100; }   

  22.   

  23. .sum::after {   

  24.   content: '= ' counter(sum);   

  25. }   

CSS計(jì)數(shù)器counter的使用技巧

到此,關(guān)于“CSS計(jì)數(shù)器counter的使用技巧”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

分享題目:CSS計(jì)數(shù)器counter的使用技巧
新聞來源:http://muchs.cn/article8/pissop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、定制開發(fā)、App開發(fā)、品牌網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)