CSS魔法堂:一塊兒玩透僞元素和Content屬性

前言

 繼上篇《CSS魔法堂:稍稍深刻僞類選擇器》記錄完僞類後,我天然而然要向僞元素伸出「魔掌」的啦^_^。本文講講述僞元素以及功能強大的Contet屬性,讓咱們能夠經過僞元素更好地實現更多的可能!css

初識僞元素

 提及僞元素我第一想到的莫過於::before::after這兩個了,它倆其實就是在其附屬的選擇器命中的元素上插入第一個子節點和追加最後一個子節點。那這時我不由地想問:「直接添加兩個class爲.before和.after不是同樣的嗎?」  其實使用僞元素::before::after如下兩個好處:html

  1. HTML的代碼量減小,對SEO有幫助;
  2. 提升JavaScript查詢元素的效率。  那爲何會這兩好處呢?緣由就是僞元素並不存在於DOM中,而是位於CSSOM,HTML代碼和DOM Tree中均沒有它的身影,量少了天然效率有所提高。但這也引入一個問題——咱們沒辦法經過JavaScript徹底操控僞元素(我將在下面一節爲你們講述)

一大波僞元素來了

除了::before::after外,別漏了如下的哦!編程

  1. :first-line:只能用於塊級元素。用於設置附屬元素的第一個行內容的樣式。可用的CSS屬性爲font,color,background,word-spacing,letter-spacing,text-decoration,vertical-align,text-transform,line-height,clear
  2. :first-letter:只能用於塊級元素。用於設置附屬元素的第一個字母的樣式。可用的CSS屬性爲font,color,background,marin,padding,border,text-decoration,vertical-align,text-transform,line-height,float,clear
  3. ::selection:匹配選中部分的內容。可用的CSS屬性爲background,color

有沒有發現有的僞元素前綴是:有的倒是::呢?::是CSS3的寫法,其實除了::selection外,其餘僞元素既兩種前綴都是能夠的,爲兼容性可選擇使用:,爲容易區分僞元素和僞類則使用::,但我仍是建議使用::來提升可讀性,兼容性就讓postcss等工具幫咱們處理就行了。瀏覽器

::before::after的注意事項

  1. 默認display: inline
  2. 必須設置content屬性,不然一切都是無用功;
  3. 默認user-select: none,就是::before::after的內容沒法被用戶選中的;
  4. 僞元素和僞類結合使用形如:.target:hover::after

JavaScript操做僞元素

 上文提到因爲僞元素僅位於CSSOM中,所以咱們僅能經過操做CSSOM API——window.getComputedStyle來讀取僞元素的樣式信息,注意:咱們能作的就是讀取,沒法設置的哦!bash

{- window.getComputedStyle的類型 -}
data PseudoElement = ":before" | "::before" | ":after" | "::after" | ":first-line" | "::first-line" | ":first-letter" | "::first-letter" | "::selection" | ":backdrop" | "::backdrop" | Null

window.getComputedStyle :: HTMLElement -> PesudoElement -> CSSStyleDeclaration

{- CSSStyleDeclaration實例的方法 -}
data CSSPropertyName = "float" | "backround-color" | ......
data DOMPropertyName = "cssFloat" | "styleFloat" | "backgroundColor" | ......

-- IE9+的方法
CSSStyleDeclaration#getPropertyValue :: CSSPropertyName -> *
-- IE6~8的方法
CSSStyleDeclaration#getAttribute :: CSSPropertyName -> *
-- 鍵值對方式獲取
CSSStyleDeclaration#[DOMPropertyName] -> *
複製代碼

示例:app

.target[title="hello world"]::after{
  display: inline-block;
  content: attr(title);
  background: red;
  text-decoration: underline;
}

const elTarget = document.querySelector(".target")
const computedStyle = window.getComputedStyle(elTarget, "::after")
const content = computedStyle.getPropertyValue("content")
const bg = computedStyle.getAttribute("backgroundColor")
const txtDecoration = computedStyle["text-decoration"]

console.log(content) // "hello world"
console.log(bg)      // red
console.log(txtDecoration) // underline
複製代碼

玩透Content屬性

 到這裏咱們已經能夠利用::before::after實現tooltip等效果了,但其實更爲強大的且更需花時間研究的纔剛要開始呢!那就是Content屬性,不只僅能夠簡單直接地設置一個字符串做爲僞元素的內容,它還具有必定限度的編程能力,就如上面attr(title)那樣,以其附屬元素的title特性做爲content值。下面請容許我爲你們介紹吧!ide

div::after{
    content: "普通字符串";
    content: attr(父元素的html屬性名稱);
    content: url(圖片、音頻、視頻等資源的url);
    /* 使用unicode字符集,採用4位16進制編碼
     * 但不一樣的瀏覽器顯示存在差別,並且移動端識別度更差
     */
    content: "\21e0";
    /* content的多個值能夠任意組合,各部分經過空格分隔 */
    content: "'" attr(title) "'";
    
    /* 自增計數器,用於插入數字/字母/羅馬數字編號
     * counter-reset: [<identifier> <integer>?]+,必選,用於標識自增計數器的做用範圍,<identifier>爲自定義名稱,<integer>爲起始編號默認爲0。
     * counter-increment: [<identifier> <integer>?]+,用於標識計數器與實際關聯的範圍,<identifier>爲counter-reset中的自定義名稱,<integer>爲步長默認爲1。
     * <list-style-type>: disc | circle | square | decimal | decimal-leading-zero | lower-roman | upper-roman | lower-greek | lower-latin | upper-latin | armenian | georgian | lower-alpha | upper-alpha
     */
    content: counter(<identifier>, <list-style-type>);
    
    /* 以父附屬元素的qutoes值做爲content的值
     */
    content: open-quote | close-quote | no-open-quote | no-close-quote;
}
複製代碼

換行符:HTML實體爲&#010,CSS爲\A,JS爲\uA工具

 能夠看到Content接受6種類型,和一種組合方式。其中最後兩種比較複雜,咱們後面逐一說明。post

自定義計數器

 HTML爲咱們提供ulolli來實現列表,但若是咱們但願實現更爲可性化的列表,那麼該如何處理呢?content屬性的counter類型值就能幫到咱們。ui

<!-- HTML 部分-->
.dl
 .dt{chapter1}
 .dd{text11}
 .dd{text12}
 .dt{chapter2}
 .dd{text21}
 
/* CSS部分 */
.dl {
  counter-reset: dt 0; /* 表示解析到.dl時,重置dt計數器爲0 */
  
  & .dt {
    counter-reset: dd 0; /* 表示解析到.dt時,重置dd計數器爲0 */
    
    &::before{
        counter-increment: dt 1; /* 表示解析到.dt時,dt計數器自增1 */
        content: counter(dt, lower-roman) " ";
    }
  }
  
  & .dd::before {
    counter-increment: dd 1; /* 表示解析到.dd時,dd計數器自增1 */
    content: counter(dd) " ";
  }
}
複製代碼

經過counter-reset來定義和重置計數器,經過counter-increment來增長計數器的值,而後經過counter來決定使用哪一個計數器,並指定使用哪一種樣式。  若是用JavaScript來表示應該是這樣的

const globalCounters = {"__temp":{}}

function resetCounter(name, value){
  globalCounters[name] = value
}
function incrementCounter(name, step){
  const oVal = globalCounters[name]
  if (oVal){
    globalCounters[name] = oVal + step
  }
  else{
    globalCounters.__temp[name] = step
  }
}
function counter(name, style){
    return globalCounters[name] || globalCounters.__temp[name]
}

function applyCSS(mount){
    const clz = mount.className
    if (clz == "dl"){
        resetCounter("dt", 0)
        const children = mount.children
        for (let i = 0; i < children.length; ++i){
          applyCSS(children[i])
        }
    }
    else if (clz == "dt"){
        resetCounter("dd", 0)
        incrementCounter("dt", 1)
        const elAsBefore = document.createElement("span")
        elAsBefore.textContent = counter("dt", "lower-roman") + " "
        mount.insertBefore(mount.firstChild)
    }
    else if (clz == "dd"){
        incrementCounter("dd", 1)
        const elAsBefore = document.createElement("span")
        elAsBefore.textContent = counter("dd", "lower-roman") + " "
        mount.insertBefore(mount.firstChild)
    }
}
複製代碼

嵌套計數器

 對於多層嵌套計數器咱們可使用counters(<identifier>, <separator>, <list-style-type>?)

.ol
  .li
    .ol
      .li{a}
      .li{b}
  .li
    .ol
      .li{c}
複製代碼
.ol {
    counter-reset: ol;
    & .li::before {
        counter-increment: ol;
        content: counters(ol, ".");
    }
}
複製代碼

Content的限制

  1. IE8+才支持Content屬性;
  2. 除了Opera9.5+中全部元素均支持外,其餘瀏覽器僅能用於:before,:after內使用;
  3. 沒法經過JS獲取Counter和Counters的運算結果。獲得的就只能是"counter(mycouonter) \" \""

自定義引號

 引號這個平時不多在乎的符號,其實在不一樣的文化中使用的引號將不盡相同,如簡體中文地區使用的"",而日本則使用「」。那咱們根據需求自定義引號呢?答案是確定的。  經過open-quote,close-quote,no-open-quoteno-close-quote便可實現,下面咱們經過例子來理解。  <q>會根據父元素的lang屬性自動建立::before::after來實現插入quotation marks。

p[lang=en]>q{英語}
p[lang=no]>q{挪威語}
p[lang=zh]>q{漢語}
p[lang=en]>q.no-quote{英語2}
div[lang=no]>.quote{挪威語2}
複製代碼

CSS片斷:

p[lang=en] > q{
  quotes: "<!--" "-->"; /* 定義引號 */
}
p[lang=en] > q.no-quote::before{
  content: no-open-quote;
  /*或者 content: none;*/
}
div[lang=no] > .quote {
  quotes: "<<-" "->>";
}
div[lang=no] > .quote::before {
  content: open-quote;
}
div[lang=no] > .quote::after {
  content: close-quote;
}
複製代碼

示例

分割線

p.sep{or}
複製代碼
.sep {
  position: relative;
  text-align: center;
  
  &::before,
  &::after {
    content: "";
    box-sizing: border-box;
    height: 1px;
    width: 50%;
    border-left: 3em solid transparent;
    border-right: 3em solid transparent;
    position: absolute;
    top: 50%;
  }
  
  &::before {
    left: 0;
  }
  
  &::after {
    right: 0;
  }
}
複製代碼

只讀效果(經過遮罩原來的元素實現)

.input-group {
  position: relative;
  
  &.readonly::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
  }
}
複製代碼

計數器

.selections>input[type=checkbox]{option1}+input[type=checkbox]{option2}
.selection-count
複製代碼
.selections{
  counter-reset: selection-count;
  
  & input:checked {
    counter-increment: selection-count;
  }
}
.selection-count::before {
  content: counter(selection-count);
}
複製代碼

最後

 尊重原創,轉載請註明來自:www.cnblogs.com/fsjohnhuang… 肥仔John^_^

參考

www.wozhuye.com/compatible/… dev.opera.com/articles/cs…

相關文章
相關標籤/搜索