Javascript styling toggle doesn't work first click

后端 未结 4 1410
暖寄归人
暖寄归人 2020-12-12 07:00

I\'m making this little html app with a sidebar menu that pops up on click. It works fine except that it only starts to work after the second click. On the first click nothi

4条回答
  •  旧巷少年郎
    2020-12-12 07:34

    With this line

    if(el.style.marginLeft=="-260px") {
    

    You are checking if the inline style of the element is a specific value. However, this style is set in the css. I recommend adding a className to the menu to expand it. You can check for this classname and add/remove it accordingly:

      if ( el.className == "expanded" ) {
         el.className = "";
      } else {
         el.className = "expanded";
      }
    

    With this addition to the css:

    #menubalk.expanded {
        margin-left:0;
    }
    

提交回复
热议问题