佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 使用CSS計(jì)數(shù)器美化數(shù)字有序列表的實(shí)現(xiàn)方法

使用CSS計(jì)數(shù)器美化數(shù)字有序列表的實(shí)現(xiàn)方法

熱門標(biāo)簽:科大訊飛語音識(shí)別系統(tǒng) 網(wǎng)站排名優(yōu)化 太平洋壽險(xiǎn)電話營銷 企業(yè)做大做強(qiáng) 國美全國運(yùn)營中心 網(wǎng)站文章發(fā)布 百度競價(jià)排名 團(tuán)購網(wǎng)站

在web設(shè)計(jì)中,使用一種井井有條的方法來展示數(shù)據(jù)是十分重要的,這樣用戶就可以很清晰的理解網(wǎng)站所展示的數(shù)據(jù)結(jié)構(gòu)和內(nèi)容,使用有序列表就是實(shí)現(xiàn)數(shù)據(jù)有組織的展示的一種簡單方法。

如果你需要更加深入地控制有序列表數(shù)字的樣式,你可能會(huì)覺得必須通過增加更多的 html DOM 結(jié)構(gòu)或者通過 JavaScript 才能做到。幸運(yùn)的是,使用 CSS計(jì)數(shù)器 可以更加容易的解決這個(gè)問題。

在這篇教程中,我們將學(xué)習(xí)到什么是 CSS計(jì)數(shù)器 和一些使用案例。

有序列表的問題

當(dāng)你寫了一個(gè)如下的有序列表,瀏覽器會(huì)自動(dòng)在列表項(xiàng)前面加上數(shù)字

 

<ol>
  <li>My First Item</li>
  <li>My Second Item</li>
  <li>My Third Item</li>
</ol>

這看起來很好,但是它不允許你對(duì)數(shù)字進(jìn)行樣式調(diào)整。假如,你需要把列表前的數(shù)字放進(jìn)一個(gè)圓圈里來修飾列表,你該怎么做呢?

一種方法是完全刪除列表,并自己手動(dòng)添加數(shù)字。

<div>
  <span>1</span> My First Item
</div>
<div>
  <span>2</span> My Second Item
</div>
<div>
  <span>3</span> My Third Item
</div>
div {
  margin-bottom:10px;
}
div span {
  display:inline-flex;
  align-items:center;
  justify-content:center;
  width:25px;
  height:25px;
  border-radius:50%;
  background-color:#000;
  color:#fff;
}

這確實(shí)是我們想要做的效果,但是也有一些缺點(diǎn)。首先,手動(dòng)添加數(shù)字是很麻煩的。如果你需要更改一個(gè)編號(hào),你必須一個(gè)接一個(gè)地改變它們。面對(duì)這種情況,你可以使用 JavaScript 動(dòng)態(tài)添加 <span> 標(biāo)簽來解決這些問題,但這會(huì)為 DOM 添加更多的節(jié)點(diǎn),從而導(dǎo)致大量內(nèi)存占用。

在大多數(shù)情況下,最好使用CSS計(jì)數(shù)器。讓我們來看看原因。

CSS計(jì)數(shù)器簡介

CSS計(jì)數(shù)器是網(wǎng)頁范圍變量,其值可以使用 CSS 規(guī)則更改。

首先,使用 counter-reset 屬性設(shè)置計(jì)數(shù)器。list-number 是此處使用的變量名。

div.list {
  counter-reset: list-number;
}

接著,使用 counter-increment屬性來增加計(jì)數(shù)器的值。

div.list div {
  counter-increment: list-number;
}

現(xiàn)在,每次出現(xiàn) div.listdiv 元素時(shí),list-number 變量都會(huì)增加一。

最后,使用含有設(shè)置 content屬性和 counter()函數(shù)的 :before 偽元素來展示數(shù)字。

div.list div:before {
  content: counter(list-number);
}

這里是完整代碼:

<div class="list">
  <div>My first item</div>
  <div>My second item</div>
  <div>My third item</div>
</div>
div.list {
  counter-reset: list-number;
}
/** 可以在:before 為元素中使用 counter-increment **/
div.list div:before {
  counter-increment: list-number;
  content: counter(list-number);
}

現(xiàn)在我們還沒有完全達(dá)到目標(biāo)。讓我們對(duì) :before 偽元素進(jìn)行樣式設(shè)計(jì),使其看起來更好。

div.list div:before {
  counter-increment: list-number;
  content: counter(list-number);
  
  margin-right: 10px;
  margin-bottom:10px;
  width:35px;
  height:35px;
  display:inline-flex;
  align-items:center;
  justify-content: center;
  font-size:16px;
  background-color:#d7385e;
  border-radius:50%;
  color:#fff;
}

修改起始數(shù)字

默認(rèn)情況下,counter-reset 會(huì)將計(jì)數(shù)器設(shè)置為 0。當(dāng)?shù)谝粋€(gè) counter-increment 被調(diào)用后它的起始變?yōu)? 可以通過將一個(gè)整數(shù)作為 counter-reset 函數(shù)的第二個(gè)參數(shù)來設(shè)置初始值。

div.list {
  counter-reset: list-number 1;
}

如果你想從 0 開始,可以將初始值設(shè)置為 -1。

div.list {
  counter-reset: list-number -1;
}

更改增量值

默認(rèn)情況下,counter-increment 會(huì)使計(jì)數(shù)器的值增加一。就像 counter-reset 一樣,你可以定義 counter-increment 屬性的偏移值。

在此示例中,counter-resetlist-number 設(shè)置為 0。每次調(diào)用 counter-increment 時(shí),list-number 數(shù)值都會(huì)增加 2,因此,你將會(huì)看到列表序?yàn)?2、46

div.list {
  counter-reset: list-number;
}
div.list div:before {
  counter-increment: list-number 2;
  // other styles
}

計(jì)數(shù)器格式

counter() 函數(shù)可以有兩個(gè)參數(shù):counter-namecounter-format。對(duì)于第二個(gè)參數(shù),你可以使用任何有效的列表類型值,包括:

  • decimal (e.g., 1, 2, 3…)
  • lower-latin (e.g., a, b, c…)
  • lower-roman (e.g., i, ii, iii…)

默認(rèn)值為數(shù)字。

例如,如果你像我一樣科學(xué),你可以使用 lower-greek 小寫希臘字母作為編號(hào)的值。

div.list div:before {
  counter-increment: list-number;
  content: counter(list-number, lower-greek);
  // ... other styles
}

計(jì)數(shù)器嵌套

使用嵌套訂單列表時(shí),始終以這種格式顯示編號(hào):

如果您需要子列表項(xiàng)目的數(shù)字編號(hào)(例如,1.1),您可以使用具有 counters() 功能的 CSS計(jì)數(shù)器

<ol>
  <li>
     My First Item
    <ol>
      <li>My Nested First Item</li>
      <li>My Nested Second Item</li>
    </ol>
  </li>
  <li>My Second Item</li>
</ol>
ol {
  list-style-type:none;
  counter-reset:list;
}
ol li:before {
    counter-increment:list;
    content: counters(list, ".") ". ";
}

注意,我們使用的是 counters() 函數(shù),而不是 counter() 函數(shù)。

counters() 函數(shù)的第二個(gè)參數(shù)是連接字符串。它還可以有第三個(gè)參數(shù)來設(shè)置格式(例如,希臘數(shù)字或羅馬數(shù)字)。

帶標(biāo)題的嵌套計(jì)數(shù)器

元素,如 <h1>,<h2> 不嵌套在文檔中。它們以不同的元素出現(xiàn),但仍代表一種層次結(jié)構(gòu)。下面介紹如何將嵌套數(shù)字設(shè)置到標(biāo)題中:

body {
  counter-reset:h1;
}
h1 {
  counter-reset:h2;
}
h1:before {
  counter-increment: h1;
  content: counter(h1) ". ";
}
h2:before {
  counter-increment:h2;
  content: counter(h1) "." counter(h2) ". ";
}

每次找到<h1>時(shí),<h2>計(jì)數(shù)器都會(huì)重置。<h2> 在文檔中獲得的編號(hào)和 <h1> 相關(guān)。

Browser support

值得慶幸的是,CSS 計(jì)數(shù)器自與 CSS2 一起推出以來,得到了瀏覽器的廣泛支持。雖然在內(nèi)容以外的屬性中使用 counter() 函數(shù)仍然是實(shí)驗(yàn)性的,但你可以毫不猶豫地執(zhí)行本教程中涵蓋的所有例子。

一個(gè)簡單挑戰(zhàn)

您準(zhǔn)備好迎接涉及CSS計(jì)數(shù)器的簡單挑戰(zhàn)了嗎?

使用 CSS計(jì)數(shù)器10 行代碼中顯示 11000 及其羅馬字符。

如果你被難倒了,下面是你如何做到這一點(diǎn):

要?jiǎng)?chuàng)建 1000 個(gè) div 元素,可以使用以下內(nèi)容。

for (var i = 0; i < 1000; i++) {
  document.body.appendChild( document.createElement("div") );  
}

CSS計(jì)數(shù)器:

body {
  counter-reset:number;
}
div:before {
  counter-increment:number;
  content: counter(number) " => " counter(number, lower-roman);
}

結(jié)論

CSS 計(jì)數(shù)器在 CSS 中是一個(gè)鮮為人知的功能,但您會(huì)驚訝于它們派上用場(chǎng)的頻率。在此教程中,我們討論了如何以及何時(shí)使用 CSS 計(jì)數(shù)器,并展示了一些示例。

以下是我們使用的屬性列表。  

屬性 用法
counter-reset 重置(或創(chuàng)建)給定值計(jì)數(shù)器(默認(rèn)0)
counter-increment 通過給定偏移增加給定計(jì)數(shù)器(默認(rèn)值 1)
counter(counter-name, counter-format) 從給定格式獲取計(jì)數(shù)器的價(jià)值
counters(counter-name, counter-string, counter-format) 從給定格式獲取嵌套計(jì)數(shù)器的價(jià)值

 CSS計(jì)數(shù)器 雖然很酷。但有一件事需要明白的是,所有計(jì)數(shù)器都是全局性的。如果你在一個(gè)有很多 CSS 文件的大型項(xiàng)目中使用,你可能無法找到它們的創(chuàng)建、重置和增量位置。不要過度使用它們,一定要使用描述性名稱的計(jì)數(shù)器,以避免沖突。

一些實(shí)戰(zhàn)例子

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS計(jì)數(shù)器</title>
  <style>
    html {
      box-sizing: border-box;
      font-size: 62.5%;
    }

    *,
    *::before,
    *:after {
      box-sizing: inherit;
    }

    body {
      font-family: Rambla, sans-serif;
      font-size: 2rem;
      line-height: 1.5;
      color: #03c03c;
    }

    h1 {
      text-align: center;
    }

    .wrapper {
      margin: 0 auto;
      width: 85%;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-justify-content: space-around;
      -ms-flex-pack: distribute;
      justify-content: space-around;
    }

    @media (max-width: 1100px) {
      .wrapper {
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        -webkit-flex-direction: column;
        -ms-flex-direction: column;
        flex-direction: column;
        -webkit-box-align: center;
        -webkit-align-items: center;
        -ms-flex-align: center;
        align-items: center;
      }
    }

    ol {
      counter-reset: li;
      margin: 20px 0;
      padding-left: 0;
    }

    ol>li {
      position: relative;
      margin: 0 0 25px 2em;
      padding: 4px 8px 4px 20px;
      list-style: none;
    }

    ol>li::before {
      content: counter(li);
      counter-increment: li;
      position: absolute;
      top: -2px;
      left: -2em;
      width: 2em;
      margin-right: 8px;
      padding: 4px;
      font-weight: bold;
      text-align: center;
    }

    li ol,
    li ul {
      margin-top: 6px;
    }

    ol ol li:last-child {
      margin-bottom: 0;
    }

    .disc>li::before {
      color: white;
      background-color: #03c03c;
      border-radius: 50%;
    }

    .circle>li::before {
      color: #03c03c;
      border: solid 2px #03c03c;
      border-radius: 50%;
    }

    .angle>li::before {
      color: #03c03c;
      border-right: solid 3px #03c03c;
      border-bottom: solid 3px #03c03c;
    }

    .shadow>li::before {
      color: white;
      background: #03c03c;
      box-shadow: 5px 5px 0 0 greenyellow;
    }

    .rombo>li {
      margin-bottom: 25px;
    }

    .rombo>li::before {
      color: white;
      z-index: 2;
    }

    .rombo>li::after {
      position: absolute;
      top: -2px;
      left: -2em;
      width: 2em;
      margin-right: 8px;
      padding: 4px;
      background-color: #03c03c;
      height: 2em;
      -webkit-transform: rotate(45deg);
      -ms-transform: rotate(45deg);
      transform: rotate(45deg);
      content: '';
      z-index: 1;
    }

    .underline>li::before {
      border-bottom: solid 3px #03c03c;
    }
  </style>
</head>

<body>
  <h1>Styling Ordered List Numbers</h1>
  <div class="wrapper">
    <ol class="disc">
      <li>Tomato</li>
      <li>Cucumber</li>
      <li>Onion</li>
      <li>Pepper</li>
    </ol>
    <ol class="circle">
      <li>Tomato</li>
      <li>Cucumber</li>
      <li>Onion</li>
      <li>Pepper</li>
    </ol>
    <ol class="angle">
      <li>Tomato</li>
      <li>Cucumber</li>
      <li>Onion</li>
      <li>Pepper</li>
    </ol>
    <ol class="shadow">
      <li>Tomato</li>
      <li>Cucumber</li>
      <li>Onion</li>
      <li>Pepper</li>
    </ol>
    <ol class="rombo">
      <li>Tomato</li>
      <li>Cucumber</li>
      <li>Onion</li>
      <li>Pepper</li>
    </ol>
    <ol class="underline">
      <li>Tomato</li>
      <li>Cucumber</li>
      <li>Onion</li>
      <li>Pepper</li>
    </ol>
  </div>
  <a href="https://css-tricks.com/custom-list-number-styling/">更多例子</a>
</body>
</html>

更多優(yōu)秀案例

https://css-tricks.com/custom-list-number-styling/

到此這篇關(guān)于使用CSS計(jì)數(shù)器美化數(shù)字有序列表的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)CSS計(jì)數(shù)器數(shù)字有序列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

到此這篇關(guān)于使用CSS計(jì)數(shù)器美化數(shù)字有序列表的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)CSS計(jì)數(shù)器數(shù)字有序列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:萍鄉(xiāng) 林芝 泰州 大同 赤峰 保定 延邊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用CSS計(jì)數(shù)器美化數(shù)字有序列表的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    乌恰县| 永嘉县| 天气| 南汇区| 新龙县| 都江堰市| 晋城| 岳阳市| 锦州市| 大宁县| 屏山县| 花莲县| 兴和县| 塔城市| 安顺市| 顺平县| 清镇市| 巧家县| 读书| 北川| 石柱| 延边| 陈巴尔虎旗| 澄迈县| 清原| 富源县| 绥阳县| 宝兴县| 滨州市| 金湖县| 新野县| 广元市| 敦煌市| 蓬溪县| 穆棱市| 林芝县| 砚山县| 元谋县| 宁乡县| 丹棱县| 丹巴县|