争怎路由网:是一个主要分享无线路由器安装设置经验的网站,汇总WiFi常见问题的解决方法。

CSS伪元素与Content属性的详细区分(代码示例)

时间:2024/2/21作者:未知来源:争怎路由网人气:

no-close-quote; }

换行符:HTML实体为&#010,CSS为\A,JS为\uA。

?可以看到Content接受6种类型,和一种组合方式。其中最后两种比较复杂,我们后面逐一说明。

自定义计数器

?HTML为我们提供ulolli来实现列表,但如果我们希望实现更为可性化的列表,那么该如何处理呢?content属性的counter类型值就能帮到我们。

<!-- 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) " ";
  }
}

87877522-5ba0ab31a71d7_articlex.png

通过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]   

关键词:CSS伪元素与Content属性的详细区分(代码示例)




Copyright © 2012-2018 争怎路由网(http://www.zhengzen.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版