Problème CSS : le défilement horizontal (overflow-x:scroll) ne fonctionne pas
P粉295616170
P粉295616170 2024-03-29 16:06:18
0
1
410

J'ai 3 "pages" et je souhaite les faire défiler horizontalement. J'ai réussi à créer une barre de défilement horizontale, mais rien ne se passe lorsque je fais défiler vers le haut/bas avec la molette de ma souris.

Voici à quoi ressemble mon conteneur :

body .container {
  width: 100%;
  height: 100%;
  scroll-snap-type: x mandatory;
  overflow-x: scroll;
  display: flex;
}

HTML + CSS complet :

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;
}
<!-- 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>

J'ai essayé Google mais je n'ai trouvé aucune solution... J'ai obtenu tout cela grâce aux tutoriels YouTube.

P粉295616170
P粉295616170

répondre à tous(1)
P粉621033928

Certains JavaScript sont requis ici et les attributs width et height doivent être supprimés du conteneur

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

scrollContainer.addEventListener("wheel", (evt) => {
    evt.preventDefault();
    scrollContainer.scrollLeft += evt.deltaY;
});
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;
}

Page1

random text

Page2

Page3

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!