简体   繁体   English

Material UI React 应用程序中没有滚动条的全高和宽度

[英]Full height & width without scrollbar in Material UI React app

I am trying to render a page for full height.我正在尝试呈现全高页面。 But it adds a scrollbar which is undesirable.但它添加了一个不受欢迎的滚动条。 With 100% height, I mean just the size of the screen. 100% 高度是指屏幕的大小。

Here is the demonstration.这是演示。 The yellow highlighted part is the unwanted height added.黄色突出显示的部分是添加的不需要的高度。 There is also a horizontal scrollbar(not highlighted):还有一个水平滚动条(未突出显示):

在此处输入图像描述

Here is the render method of the page:这是页面的渲染方法:

return (
    <>
        <Box display='flex' flex='1' justifyContent='space-around'>
            <IndexSelector
                id='index'
                value={symbol}
                onChange={this.onSymbolChange}/>
            <SeriesSelector
                id='series'
                seriesList={Form.seriesList}
                onChange={this.onSeriesChange}/>
            <DateRange fromDate={fromDate} toDate={toDate} onChange={this.onDateChange}/>
        </Box>

        <Box height='100%' border='1px solid red' marginTop='50px'>
            <Graph instructions={this.getInstructions()} apiData={this.apiData} />
        </Box>
    </>
)

Here is the index.css:这是 index.css:

html {
  box-sizing: border-box;
}

html, body, #root {
  padding: 0px !important;;
  margin: 0px !important;;
  height: 100vh;
  width: 100vw;
}

*, *:before, *:after {
  box-sizing: inherit;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
    "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
  monospace;
}

How to avoid this extra height & width and make the red border boxed container full height?如何避免这种额外的高度和宽度并使红色边框盒装容器全高?

EDIT : As per @gowatham suggestion, I tried and it didn't work.编辑:根据@gowatham 的建议,我试过了但没有用。 I got the following result:我得到以下结果:

EDIT 2:编辑 2:

HTML: https://pastebin.com/Qu2RFHe7 CSS: https://pastebin.com/1z3Zg5rv HTML: https://pastebin.com/Qu2RFHe7 CSS: https://pastebin.com/1z3Zg5rv

在此处输入图像描述

Don't give up!不要放弃! CSS styling can be tricky when you first started, but it is actually quite straight forward after you plan your page layout. CSS 样式在您刚开始时可能会很棘手,但在您规划页面布局后,它实际上非常简单。

I would do 90vh for the body and set 10vh for the filter box that you have above, so it will always be only 100vh on the page, unless you wanted to change the layout.我会做90vh的身体和一套10vh的过滤箱,你有以上,所以它永远是只100vh页面上,除非你想改变布局。 So I would have something like this:所以我会有这样的事情:

return (
  <div style={{ height: '90vh', margin: 0, padding: 0 }}>
    <Box display='flex' flex='1' justifyContent='space-around' style={{ height: '10vh' }}>
        <IndexSelector
            id='index'
            value={symbol}
            onChange={this.onSymbolChange}/>
        <SeriesSelector
            id='series'
            seriesList={Form.seriesList}
            onChange={this.onSeriesChange}/>
        <DateRange fromDate={fromDate} toDate={toDate} onChange={this.onDateChange}/>
    </Box>

    <Box style={{ maxHeight: "100%", overflow: "auto" }}>
        <Graph instructions={this.getInstructions()} apiData={this.apiData} />
    </Box>
  </div>
)

Note that your border 1px might also increase the 100% and you might see a scrollbar.请注意,您的边框 1px 也可能会增加 100%,您可能会看到一个滚动条。 To force hide the scrollbar, just add overflow: hidden to the parent div.要强制隐藏滚动条,只需将overflow: hidden添加到父 div 即可。 Example on CodeSandbox: CodeSandbox 上的示例:

编辑材料演示

If you set the main container to be 100vh, ie 100% of viewport height, and make it a display its children with a flex column direction, then you can just make the result container take all the remaining space by setting it to flex: 1 and make its content scroll by also setting overflow: auto如果您将主容器设置为 100vh,即视口高度的 100%,并使其以 flex 列方向显示其子项,那么您可以通过将其设置为 flex: 1 来使结果容器占用所有剩余空间并通过设置溢出使其内容滚动:自动

The benefit of this approach is that you can let the filter container be of any/dynamic height and it'll still work.这种方法的好处是您可以让过滤器容器具有任意/动态高度,并且它仍然可以工作。

<Box height="100vh" display="flex" flexDirection="column">
  <Box>Filter</Box>
  <Box flex={1} overflow="auto">
    {Array.from(Array(100)).map((v, i) => (
      <div key={i}>Testing {i}</div>
    ))}
  </Box>
</Box>

https://codesandbox.io/s/material-demo-ntvoj https://codesandbox.io/s/material-demo-ntvoj

Another possible solution, that would probably work in ancient browsers as well, is to just set the filter container to position: fixed and then make sure that element that directly follows the filter container has either margin-top or padding-top that is enough to prevent it's content from appearing behind the filter div when the page is scrolled to the top.另一种可能在古代浏览器中也适用的解决方案是将过滤器容器设置为 position: fixed ,然后确保紧跟过滤器容器的元素具有 margin-top 或 padding-top 足以当页面滚动到顶部时,防止其内容出现在过滤器 div 后面。

<Box
  position="fixed"
  top={0}
  height="60px"
  width="100%"
>
  Filter
</Box>
<Box marginTop="60px">
  {Array.from(Array(100)).map((v, i) => (
    <div key={i}>Testing {i}</div>
  ))}
</Box>

https://codesandbox.io/s/material-demo-4m85i https://codesandbox.io/s/material-demo-4m85i

Try subtracting box1 height from box2 height create a ref to box1:尝试从 box2 高度减去 box1 高度创建一个对 box1 的引用:

ref={(ref) => this.box1 = ref}

Then in box2:然后在box2中:

height={`calc(100% - ${this.box1.clientHeight}px)`}

Like this:像这样:

return (
    <>
        <Box display='flex' flex='1' justifyContent='space-around' ref={(ref) => this.box1 = ref}>
            <IndexSelector
                id='index'
                value={symbol}
                onChange={this.onSymbolChange}/>
            <SeriesSelector
                id='series'
                seriesList={Form.seriesList}
                onChange={this.onSeriesChange}/>
            <DateRange fromDate={fromDate} toDate={toDate} onChange={this.onDateChange}/>
        </Box>

        <Box height={`calc(100% - ${this.box1.clientHeight}px)`} border='1px solid red' marginTop='50px'>
            <Graph instructions={this.getInstructions()} apiData={this.apiData} />
        </Box>
    </>
)

Try to include Flex for the Parent also尝试为父级也包括Flex

<div display='flex' flex='1' height='100%' justifyContent='space-between'> //use flex direction column as per your library     
        <Box display='flex' flex='1' justifyContent='space-around'>
            <IndexSelector
                id='index'
                value={symbol}
                onChange={this.onSymbolChange}/>
            <SeriesSelector
                id='series'
                seriesList={Form.seriesList}
                onChange={this.onSeriesChange}/>
            <DateRange fromDate={fromDate} toDate={toDate} onChange= 
   {this.onDateChange}/>
        </Box>
        <Box>
            <Graph instructions={this.getInstructions()} apiData={this.apiData} />
        </Box>
    </div>

css Use height: '100vh'in parent instead of 100%. css 在父项中使用高度:'100vh' 而不是 100%。 100% take upto max of height of all content. 100% 占据所有内容的最大高度。 That won't enllarge after that.之后就不会变大了。 100vh is used for vertical height of whole window and also check the overflow-y 100vh 用于整个 window 的垂直高度并检查溢出-y

I made the navigation division height:15vh the division following it with height:20vh and the last division with height:65vh adding it to 100 vh ie total device height you can change it as per your wish and alter the height of the divisions.我将导航分区height:15vhheight:20vh和最后一个height:65vh添加到 100 vh,即设备总高度,您可以根据需要更改它并更改分区的高度。 The scroll doesnt appear in the desktop view now滚动现在不会出现在桌面视图中

Here is the link to the codepen It is responsive for me check whether it works.这是代码笔的链接它对我有反应,检查它是否有效。 (Ive used the pastebin code that you have provided with) (我使用了您提供的 pastebin 代码) 图片

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

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