简体   繁体   English

CSS Calc(100%-100px)在夜间引起颠簸(溢出)

[英]CSS Calc(100% - 100px) causing bumps in the night (overflow)

I'm currently working on my new menu and ran into some problems with some undesirable effects. 我目前正在使用新菜单,但遇到了一些问题,并产生了一些不良影响。 Basically I want a off canvas menu that shifts the page to the right when the menu becomes active, this is simple enough and I've adopted using calc(100% - 100px) which works great but with one snag, its causing bumps (overflow). 基本上我想要一个画布外的菜单,当菜单变为活动状态时,该菜单将页面右移,这很简单,我采用了calc(100% - 100px) ,效果很好,但有一个障碍,导致颠簸(溢出) )。

Using jQuery I have made the script add a active class on the classes which can then define overflow:hidden , however as the menu is closing it of course activates and causes a temporary overflow which looks ugly. 我使用jQuery在脚本上添加了一个活动类,这些活动类随后可以定义overflow:hidden ,但是随着菜单关闭,它当然会被激活并导致看起来很丑陋的临时溢出。

I then came up with the not so bright idea of using a jQuery setTimeout function that adds and removes active to the body of the page like so: 然后,我想到了一个不太聪明的想法,即使用jQuery setTimeout函数来向页面正文添加和删除活动内容,如下所示:

        setTimeout(function() {
            $(document.body).removeClass('bBodyMenuActive');
        }, 800);

This solves one issue but then causes another two issues. 这解决了一个问题,但又导致了另外两个问题。 The page is not scrollable until the class is removed ( 800 delay) and the other issue is that if you spam the menu button within 800 delay it gets confused and removes or adds the class when its either wanted, or not wanted. 在删除该类之前,该页面不可滚动(延迟800 ),另一个问题是,如果您在800延迟内向菜单按钮发送垃圾邮件,则该页面会变得混乱,并在需要或不希望使用时删除或添加该类。

Here is my JavaScript code: 这是我的JavaScript代码:

jQuery(function($) {
    $(".bMenuTrigger").click(function(){menu();});
    function menu(){
        if ($(".bMenuContent").css("marginLeft")=='0px'){$(".bMenuIcon").removeClass("bMenuIconActive");
            $(".bMenuLogo").removeClass("bMenuLogoActive");
            $(".bMenuContent").animate({marginLeft:-$(".bMenuContent").width()},200,function(){});
            $(".bContent").animate({left:'100px'},200,function(){});
            $(".bContent").removeClass("bContentActive");
            setTimeout(function() {
                $(document.body).removeClass('bBodyMenuActive');
            }, 800);
        }else{
            $(document.body).addClass('bBodyMenuActive');
            $(".bMenuIcon").addClass("bMenuIconActive");
            $(".bMenuLogo").addClass("bMenuLogoActive");
            $(".bMenuContent").animate({marginLeft:'0px'},200,function(){});
            $(".bContent").animate({left:$(".bMenuContent").width() + 100},200,function(){});
            $(".bContent").addClass("bContentActive");
        }
    }
    $(".bContent").click(function(){if($(".bContent").hasClass("bContentActive")){menu();}});
});

Here is my HTML code: 这是我的HTML代码:

<div class="bMenuContainer">
    <div class="bMenuTrigger"><a class="bMenuIcon">MENU</a></div>
    <div class="bMenuContent">Content will be filled Here</div>
</div>
<div class="bContent">

</div>

Here's my CSS 这是我的CSS

.bMenuContainer{height:100%;position:fixed;z-index:100;}
.bMenuTrigger{cursor:pointer;float:right;height:100%;width:100px;}
.bMenuTrigger a{color:white;text-decoration:none;margin-top:15px;}
.bMenuContent{float:right;height:100%;margin-left:-270px;width:270px;}
.bContent{height:100%;position:absolute;top:0;width:calc(100% - 100px);left:100px;}
.bContentActive{left:270px;position:absolute;overflow-y:hidden;}
.bBodyActive {overflow:hidden;}

Here's my jsFiddle https://jsfiddle.net/bybe/mybdnvt0/ (please note that adding classes on body doesn't work on fiddle, or least for me). 这是我的jsFiddle https://jsfiddle.net/bybe/mybdnvt0/ (请注意,在主体上添加类对小提琴无效,或者至少对我而言无效)。

There must be an easier way of doing this and I'm sure I've missed something dead simple! 一定有更简单的方法可以做到,而且我敢肯定我错过了一些简单的事情!

How can I use my off canvas menu without triggering overflow horizontally? 如何在不触发水平溢出的情况下使用画布外菜单?

Would this be something your looking for FIDDLE ? 这是您在寻找FIDDLE的东西吗?

I wrapped a container around the clickable menu ( .menu-main ) and the extra content to the left ( .menu-left ). 我在可点击的菜单( .menu-main )周围包装了一个容器,在左侧添加了其他内容( .menu-left )。

<nav class="side-menu">
    <div class="main-left"></div>
    <div class="main-menu"></div>
</nav

I position the menu container ( side.menu ) fixed so and positioned it left equal to the size of menu-left 我将菜单容器( side.menu )固定在side.menu ,并将其定位为等于menu-left的大小

.side-menu {
    height: 100vh;
    position:fixed;
    left: -150px;
    top: 0;
}

Since we set the left to -150px the width of .menu-left equal. 由于我们将left设置为-150px,因此.menu-left的宽度相等。

.menu-left {
    float: left;
    width: 150px;
    height: 100%;
    background-color: pink;
}

This will just hide .menu-left outside of the screen. 这只会将.menu-left隐藏在屏幕外部。
But we need to open it so we add the .main-menu . 但是我们需要打开它,所以我们添加.main-menu

.menu-main {
    float: left;
    width: 100px;
    height: 100%;
}

We add float:left so that the menu item don't display under one an other. 我们添加float:left以便菜单项不会在另一个菜单项下显示。

Under stand where this is going? 在立场下这是怎么回事? now for the javascript/jquery: 现在针对javascript / jquery:

We need to set the left position to 0 when we click on the main menu so: 单击主菜单时,我们需要将左侧位置设置为0,这样:

$('.side-menu').css('left', '0px');

and when we close it we want it set back to the value it was: 当我们关闭它时,我们希望它设置回原来的值:

$('.side-menu').css('left', '-150px');

But we probably want to set push the content of our other elements as well. 但是我们可能还希望设置推送其他元素的内容。
So a <div class="main"> for our "main" content should fit here. 因此,我们的“主要”内容的<div class =“ main”>应该适合此处。 This will have to have a margin-right equal to that of our menu since we dont want our content to be under the menu. 由于我们不希望内容位于菜单下,因此必须具有与菜单相同的页margin-right so: .main {margin-left: 100px;} 因此: .main {margin-left: 100px;}
and if we open the menu we need to set the margin-left to the size of our .side-menu and since its a container it will have the size of its children. 如果我们打开菜单,则需要将左边距设置为.side-menu的大小,由于它是一个容器,因此将具有其子项的大小。

var open = false;
$('.menu-main').click(function () {
    if (open) {
        $('.side-menu').css('left', '-150px');
        $('.main').css('margin-left', '100px');
        open = false;
    } else {
        $('.side-menu').css('left', '0px');
        $('.main').css('margin-left', $('.side-menu').css('width'));
        open = true;
    }
});

This is how I would approach the menu solution you made. 这就是我采用您制作的菜单解决方案的方式。 I hope it help you :) 希望对您有所帮助:)

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

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