Hide menu sidebar when clicking outside the bar or the button

前端 未结 5 1666
粉色の甜心
粉色の甜心 2020-12-31 22:29

I am trying to make a menu like the semantic UI but I only achieved to click the menu button and open the menu and vice versa. I use toggle class to show the sidebar but I d

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 22:49

    Suppose if your sidebar width is 270px,check the mouse click coordinate.If it's greater give its style left attribute as -270px;

    function handleMousePos(event) {
          var mouseClickWidth = event.clientX;
          if(mouseClickWidth>=270){
                document.getElementById("mySidenav").style.left='-270px'
               }
    }
    
    document.addEventListener("click", handleMousePos);
    

    Here is my sample:https://codepen.io/johncy/pen/oMyzZr

提交回复
热议问题