How can I reset a CSS-counter to the start-attribute of the given list

前端 未结 6 1215
青春惊慌失措
青春惊慌失措 2020-11-30 06:50

I am using a self-styled, numbered list. How can I read the start-attribute and add it to the counter with CSS?

HTML

  1. Number
6条回答
  •  长情又很酷
    2020-11-30 07:36

    You may just use the attribute start as a filter :

    ol[start="10"] {
       counter-reset: lis 9;
    }
    

    Demo , but this will only apply for this ol attribute. You would need some javaScript in order to retrieve attribute value to apply, generate the correct counter-reset.


    
    

    see this : DEMO to generate 100 rules from these lines :

    @for $i from 1 through 100 {
      .ol[start="#{$i}"] {
        counter-reset: lis $i ;
      }
    }
    

    Then just copy paste the rules generated if Scss is not avalaible on your hosting .

    
    

    :

    A jQuery solution can be easily set up : DEMO

    $( "ol" ).each(function() {
      var   val=1;
        if ( $(this).attr("start")){
      val =  $(this).attr("start");
        }
      val=val-1;
     val= 'lis '+ val;
    $(this ).css('counter-increment',val );
    });
    

    Notice that : $(this ).css('counter-reset',val ); works too :)

    .

提交回复
热议问题