在我的包装器或正文容器中出现了不必要的垂直滚动条,ReactJS 和 CSS。

huangapple go评论72阅读模式
英文:

There is an unwanted vertical scroll in my wrapper or body container ReactJS and CSS

问题

I'm here to provide the translated content as requested. Here is the translated text:

我正在使用React和CSS创建仪表板布局。我有一个标题、侧边栏和一个中间区域,其中包含所有内容,例如图表和图形等。在标题中有一个菜单图标,用于切换侧边栏(从可见到隐藏,反之亦然)。当屏幕宽度小于768px时,默认情况下侧边栏是隐藏的,到目前为止一切都正常,但是当我点击菜单图标以在移动屏幕上显示侧边栏时,我希望包装容器出现水平溢出,以便用户可以完全看到中间区域的内容。水平溢出是有效的,但它也在包装容器外生成了一个垂直溢出滚动条,尽管包装容器外没有任何内容。此行为仅在Windows Chrome和Android Chrome中可见。包装容器的高度是自动的,而仪表板的高度(包装容器的子元素,也是标题和内容容器(内容容器包括侧边栏和中间区域)的父元素)为100vh。

我在Chrome浏览器中遇到了这个问题(在Edge浏览器上没有问题),与溢出-y和高度有关。只有在侧边栏可见时才会出现问题。我不希望我的应用程序出现垂直溢出,但它确实出现了。

这是我想要的行为(http://webapplayers.com/homer_admin-v2.0/index.html)

这是我的应用程序(https://donatenow-crm.web.app/)

使用电子邮件:anwarhamza919@gmail.com

使用密码:hamzaanwar123$

我有一个简单的React JSX布局,在Edge上运行良好,但在Chrome上不起作用。

// 侧边栏切换
const [sidebarToggled, setSidebarToggled] = useState(false);

// sidebarToggled(false) = 侧边栏可见
// sidebarToggled(true) = 侧边栏隐藏

这是CSS

.wrapper{
width: 100%;
overflow: hidden;
}

@media(max-width: 540px){
// 如果我注释掉这段代码,溢出不会引发问题,包装器和包装器.toggled存在问题
.wrapper.toggled{
width: fit-content;
overflow-x: auto;
overflow-y: hidden;
}
}

.dashboard{
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}

.content-wrapper{
width: 100%;
height: 100%;
display: flex;
align-items: stretch;
}

header {
background-color: #ffffff;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0;
border-bottom: 1px solid #eaeaea;
}

.sidebar{
min-width: 180px;
max-width: 180px;
height: 100%;
background-color: #f7f9fa;
display: flex;
flex-direction: column;
transition: all 0.3s ease;
}

.sidebar.hidden{
margin-left: -180px;
}

.middle-area {
width: 100%;
background-color: #f1f3f6;
border-left: 1px solid #eaeaea;
padding: 25px 40px 40px 40px;
min-width: 320px;
}

.middle-content {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}

英文:

I am working on a dashboard layout with React and CSS. I have a header, sidebar, and a middle-area where all content resides for example charts graphs etc. I have a menu icon in the header that toggles the sidebar (from visible to hidden and vice versa). When I am on screen less than 768px, the sidebar is hidden by default, everything works up until now but when I click the menu icon to make the sidebar visible on mobile screens then I want the wrapper to overflow-x so that the user can see the middle-area content to the fullest. overflow-x is working but It is also generating an overflow-y scroll outside of the wrapper even though there is nothing outside the wrapper. This behavior is only seen with windows chrome and android chrome. The height of the wrapper is auto and the height of the dashboard (child of the wrapper and parent of the header and content-wrapper (content-wrapper includes sidebar and middle-area)) is 100vh.

I am facing this problem in Chrome (works fine on Edge) with overflow-y and height. The problem only occurs when the sidebar is visible. I do not my app to overflow-y but it does.

在我的包装器或正文容器中出现了不必要的垂直滚动条,ReactJS 和 CSS。

This is the behavior that I want (http://webapplayers.com/homer_admin-v2.0/index.html)

and

This is my app (https://donatenow-crm.web.app/)

use email: anwarhamza919@gmail.com

use password: hamzaanwar123$

I have a simple React JSX layout that works fine on Edge but not on chrome.

// sidebar toggled
const [sidebarToggled, setSidebarToggled] = useState(false);

// sidebarToggled(false) = sidebar visible
// sidebarToggled(true) = sidebar hidden

<div className={sidebarToggled?'wrapper':'wrapper toggled'}>
  <div className="dashboard">
     <Header />
    <div className="content-wrapper">
     <Sidebar />
     <div className="middle-area">
        <div className="middle-content"></div>
     </div>
    </div>
  </div>
</div>

Here is the CSS

.wrapper{
  width: 100%;
  overflow: hidden;
}

@media(max-width: 540px){  
//If I comment out this code, overflow won't cause the issue, something is wrong with the wrapper and wrapper.toggled
  .wrapper.toggled{
    width: fit-content;
    overflow-x: auto;
    overflow-y: hidden;
  }
}

.dashboard{
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.content-wrapper{
  width: 100%;
  height: 100%;
  display: flex;
  align-items: stretch;
}

header {
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0;
  border-bottom: 1px solid #eaeaea;
}

.sidebar{
  min-width: 180px;
  max-width: 180px;
  height: 100%;
  background-color: #f7f9fa;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
}

.sidebar.hidden{
  margin-left: -180px;
}

.middle-area {
  width: 100%;
  background-color: #f1f3f6;
  border-left: 1px solid #eaeaea;
  padding: 25px 40px 40px 40px;
  min-width: 320px;
}
  
.middle-content {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

答案1

得分: 0

看起来你面临的问题与在较小屏幕的媒体查询中的 .wrapper.toggled 类上同时使用 overflow-x: auto; 和 overflow-y: hidden; 有关。这个组合导致在Chrome浏览器上出现不希望的垂直滚动条。

要解决这个问题,你可以按照以下方式修改你的CSS:

@media (max-width: 540px) {
  .wrapper.toggled {
    width: calc(100% + 17px); /* 根据你的需要调整值 */
    overflow: hidden;
  }
}

通过使用 width: calc(100% + 17px);,你创建了一个偏移,以适应垂直滚动条的宽度,防止它影响布局。根据你的具体情况,可能需要调整17px的值。

此外,需要注意的是 .dashboard 类上的 height: 100vh; 也可能导致垂直滚动条的出现。如果你的目的是让 .dashboard 元素占据整个视口高度,你可以尝试使用 height: calc(100vh - ); 并设置适当的值来排除头部的高度计算。

英文:

It seems that the issue you're facing is related to the combination of overflow-x: auto; and overflow-y: hidden; on the .wrapper.toggled class in the media query for smaller screens. This combination is causing an unwanted vertical scrollbar to appear on Chrome browsers.

To address this issue, you can modify your CSS as follows:

 @media (max-width: 540px) {
  .wrapper.toggled {
    width: calc(100% + 17px); /* Adjust the value as per your needs */
    overflow: hidden;
  }
}

By using width: calc(100% + 17px);, you create an offset that accommodates the width of the vertical scrollbar, preventing it from affecting the layout. You may need to adjust the value 17px based on your specific circumstances.

Additionally, it's important to note that height: 100vh; on the .dashboard class could also contribute to the appearance of the vertical scrollbar. If your intention is to make the .dashboard element take up the full viewport height, you can try using height: calc(100vh - <header-height>); and set the appropriate value for <header-height> to exclude the header's height from the calculation.

答案2

得分: 0

问题在于您在切换选项中使用了这段代码,您告诉浏览器使overflow-x生效

@media (max-width: 540px)
.wrapper.toggled {
overflow-x: auto;
width: -webkit-fit-content;
width: -moz-fit-content;
width: fit-content;
}

这导致了这个不必要的溢出,只需删除这段代码,一切都会正常。

英文:

the problem is that you use this code in the toggle option you told the browser to make an overflow-x

@media (max-width: 540px)
    .wrapper.toggled {
        overflow-x: auto;
        width: -webkit-fit-content;
        width: -moz-fit-content;
        width: fit-content;
    } 

and this cause this overflow you don't need this code just remove this code and everything will be okay

答案3

得分: 0

我通过从包装器中移除切换类,并在特定条件满足时与仪表板类一起添加它来解决了这个问题。

.wrapper{
  width: 100%;
  overflow: hidden;
}

.dashboard{
  width: 100%;
  height: 100vh;
  height: var(--doc-height);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

@media(max-width: 540px){
  .wrapper{
    overflow-x: auto;
  }

  .dashboard.toggled{
    width: fit-content;
  }
}
英文:

I solved the problem by removing the toggle class from the wrapper and adding it alongside the dashboard class if the specific condition met

.wrapper{
  width: 100%;
  overflow: hidden;
}

.dashboard{
  width: 100%;
  height: 100vh;
  height: var(--doc-height);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

@media(max-width: 540px){
  .wrapper{
    overflow-x: auto;
  }

  .dashboard.toggled{
    width: fit-content;
  }
}

huangapple
  • 本文由 发表于 2023年5月29日 15:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定