How to create an on/off switch with Javascript/CSS?

前端 未结 7 480
长情又很酷
长情又很酷 2020-12-04 09:41

I want to have a sliding switch. On the left would be Off and on the right would be On. When the user toggles the switch, I want the \'slider\' portion to slide to the other

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 10:08

    Using plain javascript

    
    
      
    
         
         
    
         
         
      
    
      
         
         
      
    
    
    

    Using jQuery

    If you use jQuery, you can do it using the toggle function, or using the toggleClass function inside click event handler, like this:

    $(document).ready(function(){
        $('a#myButton').click(function(){
            $(this).toggleClass("btnClicked");
        });
    });
    

    Using jQuery UI effects, you can animate transitions: http://jqueryui.com/demos/toggleClass/

提交回复
热议问题