简体   繁体   English

使用jquery / css隐藏元素

[英]Hide an element using jquery/css

I'm trying to hide the sidebar to a negative right (-205px) so I can get an effect where the content wrapper and sidebar move at the same time. 我试图将侧边栏隐藏到负向右(-205px),以便获得内容包装和侧边栏同时移动的效果。

The problem is that the sidebar is still being shown on the right? 问题是侧边栏仍在右侧显示? I though I could hide it sending it "outside" the web page. 我虽然可以隐藏它,但将其发送到网页“外部”。 I can't simply use display block none and block on the sidebar because it will not make a smooth animation 我不能简单地使用无显示块并在侧边栏上进行块显示,因为它不会使动画流畅

  var rightSidebarOpen = false; $('#toggle-right-sidebar').click(function () { if(rightSidebarOpen) { $('#sidebar-right').css('right', '-205px'); $('.content-wrapper').css('margin-right', "0"); $('.full-page-wrapper').css('margin-right', "0"); } else { $('#sidebar-right').css('right', '0'); $('.content-wrapper').css('margin-right', "205px"); $('.full-page-wrapper').css('margin-right', "205px"); } rightSidebarOpen = !rightSidebarOpen; }); 
 .content-wrapper { background: #fff; min-height: 100vh; padding: 1rem 1.5rem 4rem; transition: all .7s ease; position: relative; background: black; } #sidebar-right { background: #fafafa; border-left: 1px solid #e5e5e5; width: 200px; height: 100%; position: absolute; top: 0; right: -205px; overflow-x: hidden; transition: left 1s ease; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="sidebar-right" class="visible"> <ul class="sidebarList"> <li> </li> </ul> </div> <div class="content-wrapper"> <button id="toggle-right-sidebar">CLICK ME</button> </div> 

You need to transition the right CSS property on the sidebar, because that's the one you're changing. 您需要在边栏上转换right CSS属性,因为这是您要更改的属性。

#sidebar-right {
    transition: right 1s ease;
}

Adding correct type of transition with equal time for both #sidebar-right and .content-wrapper would solve your issue. #sidebar-right.content-wrapper添加相等时间的正确过渡类型将解决您的问题。 Try this code. 试试这个代码。

.content-wrapper {
    background: #fff;
    min-height: 100vh;
    padding: 1rem 1.5rem 4rem;
    transition: all .7s ease;
    position: relative;
    background: black;
}

#sidebar-right {
    background: #fafafa;
    border-left: 1px solid #e5e5e5;
    width: 200px;
    height: 100%;
    position: absolute;
    top: 0;
    right: -205px;
    overflow-x: hidden;
    background: red;
    transition: all 0.7s ease;
}
#sidebar-right {
    transition: all 1s ease;
}

'all' enable to use transition on all property 'all'允许对所有属性使用过渡

overflow-x:hidden到您的外部容器中

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

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