简体   繁体   English

固定在页面底部的可滚动元素

[英]Scrollable element fixed on the bottom of the page

I need to have a scrollable element at the bottom of my page.我需要在页面底部有一个可滚动元素。

I thought I woul simply would set position: fixed and bottom:0 .我以为我会简单地设置position: fixedbottom:0 The issue is, this is to be a horizontal list of items, which I should be allowed to scroll through on the X axis.问题是,这是一个水平的项目列表,应该允许我在 X 轴上滚动。

From what I've gathered, it seems I can't scroll on a position: fixed element.从我收集到的信息来看,我似乎无法在一个position: fixed上滚动position: fixed元素。 But by removing that, I can't have it at the bottom of my page.但是通过删除它,我不能将它放在我的页面底部。 How to solve this?如何解决这个问题?

Here is an example of what my app currently looks like:以下是我的应用程序当前外观的示例:

 const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10', 'item11', 'item12', 'item13', 'item14', 'item15', 'item16', 'item17', 'item18'] const App = () => { return ( <div> My app... Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore <h3>The following are the two versions of my components:</h3> <p style={{ color: 'blue' }}>Without position fixed (blue one): on the wrong spot but can scroll</p> <p style={{ color: 'red' }}>With position fixed (red one): On the bottom of the screen as I want to, but cant scroll...</p> <div style={{ display: 'flex', bottom: 0, position: 'fixed', overflowX: 'scroll', backgroundColor: 'red' }}> {items.map((item, index) => ( <div key={index} style={{ overflowX: 'visible', margin: '0 30px', height: '100%' }}> <h2>{item}</h2> </div> ))} </div> <div style={{ display: 'flex', bottom: 0, overflowX: 'scroll', backgroundColor: 'blue' }}> {items.map((item, index) => ( <div key={index} style={{ overflowX: 'visible', margin: '0 30px', height: '100%' }}> <h2>{item}</h2> </div> ))} </div> </div> ) } ReactDOM.render( <App />, document.getElementById('app') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

It's an easy fix!这是一个简单的修复!

When setting a possibly-wider-than-the-viewport element to position: fixed , be sure to specify left and right properties.将可能比视口更宽的元素设置为position: fixed ,请务必指定leftright属性。

Without those, the fixed-position element isn't width-constrained, and therefore the browser doesn't think it needs to be scrollable.没有这些,固定位置元素不受宽度限制,因此浏览器不认为它需要可滚动。

You could also use width: 100% in some cases.在某些情况下,您还可以使用width: 100%

 const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10', 'item11', 'item12', 'item13', 'item14', 'item15', 'item16', 'item17', 'item18'] const App = () => { return ( <div> My app... Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore <h3>The following are the two versions of my components:</h3> <p style={{ color: 'blue' }}>Without position fixed (blue one): on the wrong spot but can scroll</p> <p style={{ color: 'red' }}>With position fixed (red one): On the bottom of the screen as I want to, but cant scroll...</p> <div style={{ display: 'flex', bottom: 0, left: 0, // <-- right: 0, // <-- position: 'fixed', overflowX: 'scroll', backgroundColor: 'red' }}> {items.map((item, index) => ( <div key={index} style={{ overflowX: 'visible', margin: '0 30px', height: '100%' }}> <h2>{item}</h2> </div> ))} </div> <div style={{ display: 'flex', bottom: 0, overflowX: 'scroll', backgroundColor: 'blue' }}> {items.map((item, index) => ( <div key={index} style={{ overflowX: 'visible', margin: '0 30px', height: '100%' }}> <h2>{item}</h2> </div> ))} </div> </div> ) } ReactDOM.render( <App />, document.getElementById('app') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

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

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