How do you create a toggle button?

后端 未结 15 3004
名媛妹妹
名媛妹妹 2020-11-27 11:46

I want to create a toggle button in html using css. I want it so that when you click on it , it stays pushed in and than when you click it on it again it pops out.

15条回答
  •  孤街浪徒
    2020-11-27 12:18

    In combination with this answer, you can also use this kind of style that is like mobile settings toggler.

    togglers

    HTML

     
     
     
    

    CSS

    a.toggler {
        background: green;
        cursor: pointer;
        border: 2px solid black;
        border-right-width: 15px;
        padding: 0 5px;
        border-radius: 5px;
        text-decoration: none;
        transition: all .5s ease;
    }
    
    a.toggler.off {
        background: red;
        border-right-width: 2px;
        border-left-width: 15px;
    }
    

    jQuery

    $(document).ready(function(){
        $('a.toggler').click(function(){
            $(this).toggleClass('off');
        });
    });
    

    Could be much prettier, but gives the idea.
    One advantage is that it can be animated with CSS
    Fiddler

提交回复
热议问题