移除水平滚动条

3
在下面的jsfiddle中,有两个按钮可以打开屏幕左侧或右侧的菜单div。
当菜单被打开时,网站的其余部分(我使用了html div)也会随之移动。
我的问题是,当左菜单被打开时,html div变成可左右滚动的,而当右菜单被打开时则不会。
我不确定为什么会这样,但我想尽可能地去掉从左到右的滚动条,而不添加overflow:hidden,因为这样我也会失去上下滚动的能力。

https://jsfiddle.net/8nj5y4t1/62/

我的代码如下:

HTML:

<header class="header">
  <span id="button-one"></span>
  <span id="button-two"></span>
  <div class="push-menu-one"></div>
  <div class="push-menu-two"></div>
  <div class="overlay"></div>
</header>

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

<footer class="footer"></footer>

CSS:

html {
  position:relative;
  height:100%;
  left:0;
  right:0;
  background-color:pink;
  -webkit-transition: all .6s cubic-bezier(.645,.045,.355,1);
  transition: all .6s cubic-bezier(.645,.045,.355,1); 
}

body {
  min-height:100%;
  margin:0; 
  padding:0; 

  display:-webkit-box;
  display:-webkit-flex;
  display:-ms-flexbox;
    display:flex;

  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
    flex-direction: column;
}

.header {
  height:70px;
  width:100%;
  background-color:white;
}

.content {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
    flex: 1;

  width:85%;
  margin-top:50px;
  margin-left:auto;
  margin-right:auto;
}

.footer {
  display:-webkit-box;
  display:-webkit-flex;
  display:-ms-flexbox;
    display:flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
    flex-direction: column;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
    align-items: center;

  height: auto;
  width: 100%;      
  padding: 10px 0 10px 0;
  background-color: #efefef;
 }

/* PUSH MENUS */

#button-one {
  display:inline-block;
  width:30px;
  height:30px;
  margin:20px;
  background-color:green;
  cursor:pointer;
}

#button-two {
  display:inline-block;
  float:right;
  width:30px;
  height:30px;
  margin:20px;
  background-color:orange;
  cursor:pointer;
}

.push-menu-one {
  position:fixed;
  top:0px;
  left:-295px;
  width:295px;
  height:100%;
  margin:0;
  padding:0;
  background-color:wheat;
  -webkit-transition: all .6s cubic-bezier(.645,.045,.355,1);
  transition: all .6s cubic-bezier(.645,.045,.355,1);
}

.push-menu-two {
   position:fixed;
   top:0px;
   right:-295px;
   width:295px;
   height:100%;
   margin:0;
   padding:0;
   background-color:darkred;
   -webkit-transition: all .6s cubic-bezier(.645,.045,.355,1);
   transition: all .6s cubic-bezier(.645,.045,.355,1);
}

.overlay {
   position:fixed;
   z-index:9;
   top:0px;
   left:0px;
   width:0px;
   height:0px;
   background-color:#000000;
   opacity:0;

   transition: opacity 1s, width 0s ease 1s, height 0s ease 1s;
 }

.overlay.open-right,
.overlay.open-left {
   width:100%;
   height:100%;
   opacity:0.4;

   transition: opacity 1s;
}
/* TOGGLE CLASSES */

html.open-left {
  left:295px;
}

.push-menu-one.open-left {
  left:0;
}

html.open-right {
  left:-295px;
}

.push-menu-two.open-right {
  right:0;
}

jQuery:

jQuery(document).ready(function($) {    

    $('#button-one').click(function() {
        $('html, .overlay, .push-menu-one').toggleClass('open-left');

 });

 $('#button-two').click(function() {
        $('html, .overlay, .push-menu-two').toggleClass('open-right');

});

$('.overlay').click(function() {
        $('html, .overlay, .push-menu-one, .push-menu-two').removeClass('open-left');

$('html, .overlay, .push-menu-one, .push-menu-two').removeClass('open-right');

});

});
1个回答

5
html, body {
    overflow-x: hidden;
}

编辑

来自评论:

你知道为什么左侧菜单打开时滚动条只影响html/body吗?看起来奇怪,右侧没有同样的问题。

考虑内容的流动方向。

在从左到右的语言模式下,内容会向右溢出,而不会向左溢出。

因此,滚动(CSS中的overflow函数)不适用于左侧,因为没有溢出。

在从右到左的语言中,则相反。

您可以将内容切换到RTL模式-使用CSS的direction属性或HTML的dir属性-以启用LTR语言中的RTL滚动(但这是一种hack方法,可能会变得混乱)。

来自规范:

11.1.1 Overflow: the overflow property

该属性指定当块容器元素的内容溢出元素的框时是否剪裁该内容。

同样,在LTR阅读/写作模式下,内容不会向左溢出。

一般来说,通常会调用overflow属性以向视口左侧滚动,仅因为overflow通常与滚动条相关联。但实际上这样的请求是针对专业滚动的,与overflow无关。考虑JS / jQuery。


1
谢谢Michael,这个方法很有效。你知道为什么左侧菜单打开时滚动只影响了html/body吗?似乎右侧没有发生同样的情况,这很奇怪。 - Jack Averill
1
@Jack1991,左侧菜单出现水平滚动条的原因是打开左侧菜单会将页面内容推出页面。页面从左到右阅读,因此当您超出右边距时,overflow-x滚动条会出现以允许用户查看该内容,但将内容向左推则没有同样的效果 :) - Halfpint
我刚意识到这在Chrome上有效但在Safari上有些问题...滚动条不再出现,但在水平滚动时内容仍然会移动 :/ - Jack Averill
尝试添加以下规则中的一个或两个:overflow-y: automax-width: 100% - Michael Benjamin
1
也许这里有一些可以帮助你的东西。否则,请考虑发布另一个针对此特定问题的问题。 - Michael Benjamin
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接