简体   繁体   English

jQuery侧边栏菜单背景不会隐藏外部单击

[英]jQuery sidebar menu backdrop wont hide click outside

My jQuery sidebar menu show on click got something problem after sidebar hide, the overlay (.backdrop) wont hide. 侧边栏隐藏后,单击显示我的jQuery侧边栏菜单时出现了问题,叠加层(.backdrop)不会隐藏。

Here is the fiddle 这是小提琴

HTML 的HTML

<li class="topHeaderMenu leftMenu listMenu" id="open"><div   class="listMenuDiv"><i class="glyphicon glyphicon-align-justify"></i></div></li>

<div class="backdrop"></div>

<div class="sidebar">
  content
</div>

jQuery jQuery的

$('#open').click(function()
{
    $(this).find('i').toggleClass('glyphicon-align-justify').toggleClass('glyphicon-remove');
    $('.sidebar').toggleClass('active');
$(".backdrop").toggle();
})

$(document).click(function(e)
{
    var sidebar = $(".sidebar, #open");

    if(!sidebar.is(e.target) && sidebar.has(e.target).length === 0)
    {
        $('#open').find('i').toggleClass('glyphicon-align-justify').toggleClass('glyphicon-remove');
        sidebar.removeClass('active');
    }
});

CSS 的CSS

p {
text-align: center;
}

ul
{
  list-style: none;
}

.sidebar
{
    position: fixed;
    transform: translateX(-120%);
    display: inline-block;
    height: 100vh;
    background: lightblue;
    transition: all 0.3s ease-in;
    width: 200px;
    z-index: 8;
}

.active
{
    transform: translateX(0);
}

.backdrop
{
height: 100vh;
width: 100%;
z-index: 7;
position: fixed;
top: 0;
left: 0;
display: none;
background-color: #000;
opacity: 0.2;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}

So the problem is, after sidebar hide the .backdrop not hide. 所以问题是,在侧边栏隐藏后,.backdrop不能隐藏。 What I want is hide when click outside sidebar. 我想要的是在侧边栏上单击时隐藏。

you are removing the 'active' class but not hiding your backdrop div. 您正在删除“活动”类,但未隐藏背景div。 Please use below code 请使用下面的代码

$('#open').click(function()
    {
        $(this).find('i').toggleClass('glyphicon-align-justify').toggleClass('glyphicon-remove');
        $('.sidebar').toggleClass('actives');
        $(".backdrop").toggle();
    })

    $(document).click(function(e)
    {
        if($(".sidebar.actives").length > 0)
        {
            var sidebar = $(".sidebar, #open");

            if(!sidebar.is(e.target) && sidebar.has(e.target).length === 0)
            {
                $('#open').find('i').toggleClass('glyphicon-align-justify').toggleClass('glyphicon-remove');
                sidebar.removeClass('actives');
        $(".backdrop").hide();
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM