Open and close side menu with same button

前端 未结 4 866
耶瑟儿~
耶瑟儿~ 2021-01-24 22:24

Currently I use one button to display a side menu and another to close it



        
4条回答
  •  情书的邮戳
    2021-01-24 23:10

    Here's what you can do, I call this "trick" a custom toggle :

    $(document).ready(function(){
      $(/*selector for the button*/)
        .click(function(){
          const $this = $(this);
          let prop = $this.prop("opened");
    
          if(prop === undefined){
            $this.prop("opened", false);
            prop = $this.prop("opened");
          }
    
          if(prop === false){
            /*handle opening here*/
          }
    
          if(prop === true){
            /*handle closing here*/
          }
    
          $this.prop("opened", !prop);
        });
    });
    

提交回复
热议问题