Removing an element added by ::before pseudo selector

前端 未结 3 785
谎友^
谎友^ 2021-01-13 20:14

I have the following case: (styling is done in SASS and unnecessary stylings are omitted.)

.header {
  ...
  &::before {
    ...
    position: absolute;         


        
3条回答
  •  失恋的感觉
    2021-01-13 20:53

    Only CSS can remove pseudo element, so you need to have an other class that display:none; the before. First declare that class in the CSS :

    .header {
      ...
      &::before {
        ...
        position: absolute;
        height: 0.5rem;
        ...
      }
    
      &.no-before::before{
        display:none;
      }
    }
    

    Then, when you want to remove it :

    $('.header').addClass('no-before'); //Remove before
    $('.header').removeClass('no-before'); //Re-add before
    

提交回复
热议问题