搜尋

首頁  >  問答  >  主體

CSS 問題:水平滾動(overflow-x:scroll)不起作用

我有 3 個“頁面”,想要水平滾動它們。 我已經成功創建了一個水平滾動條,但是當我用滑鼠滾輪向上/向下滾動時什麼也沒有發生。

這就是我的容器的樣子:

1

2

3

4

5

6

7

body .container {

  width: 100%;

  height: 100%;

  scroll-snap-type: x mandatory;

  overflow-x: scroll;

  display: flex;

}

完整的 HTML CSS:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

body {

  width: 100vw;

  height: 100vh;

  margin: 0;

}

 

body .container {

  width: 100%;

  height: 100%;

  scroll-snap-type: x mandatory;

  overflow-x: scroll;

  display: flex;

}

 

body .container section {

  flex: none;

  display: flex;

  justify-content: center;

  align-items: center;

  width: 100vw;

  height: 100vh;

  scroll-snap-align: start;

}

 

body .container section:nth-of-type(1) {

  background-color: rgb(33, 59, 27);

  color: green;

}

 

body .container section:nth-of-type(2) {

  background-color: rgb(45, 42, 39);

  color: rgb(182, 216, 182);

}

 

body .container section:nth-of-type(3) {

  background-color: rgb(52, 41, 33);

  color: rgb(87, 33, 233);

}

 

body .container section h1 {

  font-family: "Courier New", Courier, monospace;

  font-size: 10em;

}

 

body .container section p {

  font-size: 12px;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<!-- main wrapper of the content for the whole webpage -->

<div class="container">

  <!-- sections of the web page -->

  <section>

    <h1>Page1</h1>

    <p>random text</p>

  </section>

  <section>

    <h1>Page2</h1>

  </section>

  <section>

    <h1>Page3</h1>

  </section>

</div>

我嘗試 Google 但沒有找到任何解決方案...我從 YouTube 教程中獲得了所有這些內容。

P粉295616170P粉295616170358 天前579

全部回覆(1)我來回復

  • P粉621033928

    P粉6210339282024-03-30 00:40:36

    這裡需要使用一些 JavaScript,並且必須從容器中刪除寬度和高度屬性

    1

    2

    3

    4

    5

    6

    const scrollContainer = document.querySelector(".container");

     

    scrollContainer.addEventListener("wheel", (evt) => {

        evt.preventDefault();

        scrollContainer.scrollLeft += evt.deltaY;

    });

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    body {

      width: 100vw;

      height: 100vh;

      margin: 0;

    }

     

     

    body .container {

      overflow-x: scroll;

      display: flex;

    }

     

    body .container section {

      flex: none;

      display: flex;

      justify-content: center;

      align-items: center;

      width: 100vw;

      height: 100vh;

      scroll-snap-align: start;

    }

     

    body .container section:nth-of-type(1) {

      background-color: rgb(33, 59, 27);

      color: green;

    }

     

    body .container section:nth-of-type(2) {

      background-color: rgb(45, 42, 39);

      color: rgb(182, 216, 182);

    }

     

    body .container section:nth-of-type(3) {

      background-color: rgb(52, 41, 33);

      color: rgb(87, 33, 233);

    }

     

    body .container section h1 {

      font-family: "Courier New", Courier, monospace;

      font-size: 10em;

    }

     

    body .container section p {

      font-size: 12px;

    }

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    <!-- main wrapper of the content for the whole webpage -->

    <div class="container">

      <!-- sections of the web page -->

      <section>

        <h1>Page1</h1>

        <p>random text</p>

      </section>

      <section>

        <h1>Page2</h1>

      </section>

      <section>

        <h1>Page3</h1>

      </section>

    </div>

    回覆
    0
  • 取消回覆