Toggle class on HTML element without jQuery

后端 未结 7 1433
时光说笑
时光说笑 2020-12-08 14:27

I have a section on my website which holds all the content, but I want a \"sidebar\" with hidden content to smoothly appear from the left at the push of an external button.<

相关标签:
7条回答
  • 2020-12-08 15:11

    You can implement it only by CSS3:

    <label for="showblock">Show Block</label>
    <input type="checkbox" id="showblock" />
    
    <div id="block">
        Hello World
    </div>
    

    And the CSS part:

    #block {
        background: yellow;
        height: 0;
        overflow: hidden;
        transition: height 300ms linear;
    }
    
    label {
        cursor: pointer;
    }
    
    #showblock {
        display: none;
    }
    
    #showblock:checked + #block {
        height: 40px;
    }
    

    The magic is the hidden checkbox and the :checked selector in CSS.

    Working jsFiddle Demo.

    0 讨论(0)
提交回复
热议问题