亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

固定在頁面底部的可滾動元素

固定在頁面底部的可滾動元素

MMTTMM 2023-08-24 17:55:49
我需要在頁面底部有一個可滾動元素。我想我會簡單地設置position: fixed和bottom:0。問題是,這是一個水平的項目列表,我應該可以在 X 軸上滾動。根據我收集的信息,我似乎無法在position: fixed元素上滾動。但通過刪除它,我就無法將其放在頁面底部。怎么解決這個問題呢?這是我的應用程序當前的示例: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>    )}
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

這是一個簡單的修復!


將可能比視口更寬的元素設置為 時position: fixed,請務必指定left和right屬性。

如果沒有這些,固定位置元素就不受寬度限制,因此瀏覽器認為它不需要可滾動。


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>


查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號