首頁  >  文章  >  web前端  >  在CSS中建立水平可滾動的部分

在CSS中建立水平可滾動的部分

WBOY
WBOY轉載
2023-08-29 09:29:091035瀏覽

在CSS中建立水平可滾動的部分

水平可捲動的部分是常見的網頁設計模式,用於顯示超出視窗寬度的內容。這種設計模式允許使用者水平滾動,提供了一種獨特而吸引人的方式來展示大型圖像、畫廊、時間軸、地圖和其他內容。這是透過使用CSS屬性,如overflow−x: auto或overflow−x: scroll來實現的。

這使用本機瀏覽器功能進行水平捲動,並且跨裝置回應。允許輕鬆導航和探索內容。它不需要任何額外的庫或插件。

演算法

  • 使用「container」類別定義容器元素。

  • 將容器的「overflow−x」屬性設為「auto」以啟用水平捲動。

  • 將容器的“white−space”屬性設為“nowrap”,以防止各部分換行到下一行。

  • 使用class為"section"定義section元素。

  • 將每個部分的“display”屬性設為“inline−block”,使它們並排顯示。

  • 將每個部分的“width”屬性設為“100vw”,以將每個部分的寬度設為完整視口寬度。

  • 將每個部分的“height”屬性設為“80vh”,以將每個部分的高度設為視口高度的 80%。

  • 使用「margin−right」屬性在每個部分的右側新增邊距。

  • 使用「vertical−align」屬性將每個部分的頂部與容器的頂部對齊。

  • 將section元素放置在容器元素內。

範例

 <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Horizontal Scrollable Sections</title>

    <!------------------------- CSS ----------------------->

    <style>

      .container {
        overflow-x: auto; /* Enables horizontal scrolling for the container */
        white-space: nowrap; /* Prevents the sections from wrapping to the next line */
      }

      .section {
        display: inline-block; /* Makes the sections display side-by-side */
        width: 100vw; /* Sets the width of each section to the full viewport width */
        height: 80vh; /* Sets the height of each section to 80% of the viewport height */
        margin-right: 20px; /* Adds a 20px margin to the right of each section */
        vertical-align: top; /* Aligns the top of each section with the top of the container */
      }


    </style>

  </head>
  <body>
    <!-- This is the container that holds the sections -->
    <div class="container">
       <!-- Each section is a div with the "section" class -->
       <div class="section">
        <h2>Section 1</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
      <div class="section">
        <h2>Section 2</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
      <div class="section">
        <h2>Section 3</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
      <div class="section">
        <h2>Section 4</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
      <div class="section">
        <h2>Section 5</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
    </div>
  </body>
  </html>

這也可以透過傳統的垂直滾動與分頁或選項卡來實現,將內容分成不同的部分。使用網格或彈性盒佈局以響應式和視覺上吸引人的方式顯示內容,而不依賴水平滾動。

結論

在設計時請記住以下準則:

  • 保持簡單:避免將每個區域塞滿訊息。將注意力放在簡潔明了地陳述關鍵要點上。

  • 使用引人注目的視覺效果:為了吸引觀眾並使您的版塊更具娛樂性,請使用一流的照片、影片或動畫。

  • 使用一致的設計:為了產生無縫的整體外觀和感覺,請確保每個區域都具有一致的設計。

  • 提供導覽:讓使用者可以輕鬆地在水平滾動頁面的各個部分之間移動。要使它們移動,您可以添加箭頭、點和導航連接。

以上是在CSS中建立水平可滾動的部分的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除