简体   繁体   English

如何水平滚动元素,但允许垂直滚动滚动整个页面?

[英]How to scroll element horizontally, but allow vertical scrolling to scroll the entire page?

I have a bunch of cards arranged horizontally.我有一堆水平排列的卡片。 When the user hovers over them I want them to be able to scroll through the cards horizontally but if they scroll vertically I want the body to scroll.当用户将鼠标悬停在上面时,我希望他们能够水平滚动卡片,但如果他们垂直滚动,我希望正文滚动。

Currently if I hover over the cards I can scroll horizontally through them but if I scroll vertically the cards swallow that event and nothing happens.目前,如果我 hover 在卡片上,我可以水平滚动它们,但如果我垂直滚动,卡片会吞下该事件,并且没有任何反应。 I've tried capturing the event in JS and passing it up but I couldn't quite get it working.我试过在 JS 中捕获事件并将其传递出去,但我无法让它正常工作。

If you look at Apple's Store page, the cards that move left-to-right are an example of what I'm trying to accomplish.如果您查看 Apple 的商店页面,从左到右移动的卡片就是我正在尝试完成的示例。 https://www.apple.com/store If there's a word for this other than "carousel" please let me know. https://www.apple.com/store如果除了“旋转木马”之外还有其他词,请告诉我。 I've looked for npm packages to take care of this for me, but I can't find any that I like by searching "carousel".我已经寻找 npm 包来为我处理这个问题,但我无法通过搜索“旋转木马”找到任何我喜欢的包。

From your question i understood that you need vertical scrollbar but no horizontal scrollbar?从你的问题我了解到你需要垂直滚动条但不需要水平滚动条?

For vertical scrollable bar use the x and y axis.对于垂直滚动条,使用 x 和 y 轴。 Set the overflow-x:hidden;设置溢出-x:隐藏; and overflow-y:auto;和溢出-y:自动; that will automatically hide the horizontal scroll bar and present only vertical scrollbar.这将自动隐藏水平滚动条并仅显示垂直滚动条。

Show your css code for more proper answer显示您的 css 代码以获得更正确的答案

If I understand your question correctly you shouldn't need Javascript at all and should be able to do this with just CSS.如果我正确理解你的问题,你根本不需要 Javascript,应该只需要 CSS 就可以做到这一点。

Here's a demo I made .这是我制作的演示 Let me know how close that is to what you are trying to do.让我知道这与您正在尝试做的事情有多接近。

<section class="container">
  <h2>Carousel</h2>
  <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in lorem vel dui faucibus auctor. Proin sit amet tortor libero. In sed nulla metus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In urna risus, condimentum at nibh mollis, rutrum cursus quam. Sed rhoncus ac ante vitae mollis. Vestibulum maximus lorem non justo dictum ultrices. Etiam interdum, mi ut posuere iaculis, tellus quam placerat felis, sit amet vehicula est magna ut augue.</p>
</section>
<section class="carousel">
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
  <div class="carousel-card"></div>
</section>
<section class="container">
  <h2>Carousel</h2>
  <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in lorem vel dui faucibus auctor. Proin sit amet tortor libero. In sed nulla metus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In urna risus, condimentum at nibh mollis, rutrum cursus quam. Sed rhoncus ac ante vitae mollis. Vestibulum maximus lorem non justo dictum ultrices. Etiam interdum, mi ut posuere iaculis, tellus quam placerat felis, sit amet vehicula est magna ut augue.</p>
</section>
:root {
  --max-width: 960px;
}

html,
body {
  margin: 0;
  padding: 0;
}

section {
  margin-top: 30px;
  margin-bottom: 30px;
}

.container {
  max-width: var(--max-width);
  margin-left: auto;
  margin-right: auto;
}

.carousel {
  display: flex;
  overflow: auto;
}

.carousel:before,
.carousel:after {
  content: '';
  width: calc((100vw - var(--max-width)) / 2);
  flex-shrink: 0;
}

.carousel-card {
  height: 400px;
  width: 300px;
  background-color: gray;
  border-radius: 20px;
  flex-shrink: 0;
}

.carousel-card + .carousel-card {
  margin-left: 20px;
}

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

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