Javascript styling toggle doesn't work first click

后端 未结 4 1408
暖寄归人
暖寄归人 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:30

    style.marginLeft is looking at your inline styles. As you haven't defined any inline style initially style.marginLeft is undefined.

    To fix this, you could simply reverse your if/else statement:

    if ( el.style.marginLeft=="0px" )
    

提交回复
热议问题