網(wǎng)頁設(shè)計中的計數(shù)器及其使用

  • 2019-05-30 17:04:03
  • 閱讀次數(shù):
  • 作者:盈嵐科技小編
  • 來源:http://www.iy5y368.cn

計數(shù)器

我們都已經(jīng)對計數(shù)器很熟悉了,例如,有序列表中的列表項標志就是計數(shù)器。在網(wǎng)頁建設(shè)中,沒有辦法影響這些計數(shù)器很大程度上是因為沒有這個必要:HTML為有序列表定義了自己的計數(shù)行為,這就足夠了。隨著XML的出現(xiàn),現(xiàn)在需要提供一種定義計釋器的方法,這很重要。不過,CSS2沒有滿足于只是提供HTML中的簡單計數(shù)。它增加了兩個屬性和兩個content值,從而可以定義幾乎所有計數(shù)格式,包括采用多種樣式的小節(jié)計數(shù)器,如“VII.2.C”。

使用計數(shù)器

不過,要在網(wǎng)頁設(shè)計中具體顯示計數(shù)器,還需要結(jié)合使用content屬性和一個與計數(shù)器有關(guān)的值。要了解這是如何做到的,下面以一個基于XML的有序列表為例,如下:


<list type="ordered">

<item>First item</item>

<item>Xtem two</item>

<item>The third itera</item>

</list>

向采用此結(jié)構(gòu)的XML應(yīng)用以下規(guī)則,可以得到如圖12-22所示的結(jié)果:


list[type="ordered"]{counter-reset: ordered;}/* defaults to 0 */

list[type="ordered"] item {display: block;}

list[type="ordeired"] item:before {counter-increment: ordered; content: counter(ordered)";margin: 0.25em 0;}

注意,與平常一樣,生成內(nèi)容作為行內(nèi)內(nèi)容放在相關(guān)元素的開始位置。因此,其效果類似于聲明了list-style-position: inside;的HTML列表。


還要注意,item元素是生成塊級框的普通元素,這說明計數(shù)器并不僅限于display為 list-itexn的元素。實際上,任何元素都可以利用計數(shù)器??紤]以下規(guī)則:


h1:before (counter-reset: section subsec;

counter-increment: chapter;

content: counter(chapter)".";}

h2:before {counter-reset: subsec;

counter-increment: section;

content: counter(chapter )"." counter(section)}.

h3:before {counter-increment: subsec;

content: counter(chapter)"." counter(section)"." counter(subsec)".";}

注意h1元素如何使用計數(shù)器chapter,該計數(shù)器默認為0,但在元素文本前卻顯示了一個“1.”。計數(shù)器由同一個元素遞增和使用時,遞增發(fā)生在計數(shù)器顯示之前。類似地,如果計數(shù)器在同一個元素中重置和顯示,重置也在計數(shù)器顯示之前發(fā)生。考慮以下規(guī)則:


h1:before, h2:before, h3:before {

content: counter(chapter)"." counter(section)"." counter(subsec)".";}

h1 {counter-reset: section subsec;

counter-increment: chapter;}

文檔中第一個h1元素前面有文本"1.0.0.”,因為計數(shù)器section和subsec都重置,但沒有遞增。這意味著如果希望一個遞增計數(shù)器第一次顯示0,只需將該計數(shù)器重置為-1,如下:

body {counter-reset: chapter -1;}

h1:before {counter-increment: chapter; content: counter(chapter)".";}

對計數(shù)器還可以做一些有意思的事情??紤]以下XML:

<code type="BASIC">

<line>PRINT "Hello world!"</line>

<line>REM This is what Che kids are calling a "comment"</line>

<line>G0T0 10</line>

</code>

可以用以下規(guī)則改寫B(tài)ASIC程序清單的傳統(tǒng)格式:

code[type="BASIC"]{counter-reset: linenum;

font-family: monospace;}

code[type="BASIC"] line {display: block;}

code[type:"BASIC"] line:before {counter-increment: Xinenum; content: counter(linenum 10)":";}

還可以為每個計數(shù)器定義一個列表樣式,作為counter ()格式的一部分。為此可以在計數(shù)器的標識符后面增加一個list-style-type關(guān)鍵字,用逗號分隔。

注意,沒有為計數(shù)器section指定樣式關(guān)鍵字,所以它默認為decimal計數(shù)樣式。如果愿意,甚至可以將計數(shù)器設(shè)置為使用樣式disc、circle、square和none,

有意思的是,在網(wǎng)頁設(shè)計中,即使規(guī)則看上去會讓計數(shù)器遞增,但實際上display為none的元素并不會遞增計數(shù)器。相反,visibility為hidden的元素確實會遞增計數(shù)器:

.suppress {counter-increment: entr; display: none;}

/*'cntr' is NOT incremented */

.invisible {counter-increment: cntr; visibility: hidden;}

/*'cntr' IS incremented */


當前文章標題:網(wǎng)頁設(shè)計中的計數(shù)器及其使用

當前URL:http://www.iy5y368.cn/news/wzzz/3035.html

上一篇:網(wǎng)頁設(shè)計中的生成引號

下一篇:網(wǎng)頁設(shè)計中的重置和遞增

網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷咨詢專線:181-8386-5875(點擊可一鍵撥號)