html 溢出滚动而不隐藏它的溢出内容

djp7away  于 11个月前  发布在  其他
关注(0)|答案(5)|浏览(173)

我试图创建一个“滑块”,用户可以水平滚动到特定的“滑块项目”,我试图用纯css创建它,然而,我似乎不能让它正常工作。

开始:

结束:

好吧,让我解释一下图片。在'Window'中的所有内容都是可见的。这也意味着无序列表的溢出。我想要的是让用户能够在容器中水平滚动以移动无序列表。然而,我不能在'容器'上使用overflow: hiddenoverflow: scroll,因为它会隐藏所有溢出的内容,这是我不想要的。
我如何才能实现这一点,或者它甚至可以用纯CSS实现?
我的当前代码:https://jsfiddle.net/f0exzxkw/2/

6rvt4ljy

6rvt4ljy1#

我开始使用SwiperJs作为@Karl提到(演示:https://swiper-demo-13-centered-1j4bnx.stackblitz.io/),但在那之后我开始尝试没有它。有很多技巧,其中很多可以不同和改进。但是,要正确对齐滚动条,你必须使用自定义的。主要思想是在.scroll-container上设置一个负边距,使用CSS变量来计算元素之间的空间。

:root {
    --WINDOW-X-SPACE: 20px;
    --CONTAINER-X-SPACE: 30px;
}

* {
    box-sizing: border-box;
}
html, body {
    height: 100vh;
}
body {
    background: teal;
    margin: 0;
    padding: 20px var(--WINDOW-X-SPACE);
    overflow-x: hidden;
}
main {
    padding: 10px var(--CONTAINER-X-SPACE);
    background: purple;
}
.scroll-container {
    height: 200px;
    margin: 0 calc(-1 * calc(var(--WINDOW-X-SPACE) + var(--CONTAINER-X-SPACE)));
    padding: 0 var(--WINDOW-X-SPACE);
    display: flex;
    overflow: auto;
}
.scroll-container::-webkit-scrollbar {
    all: unset;
}
 
.scroll-container::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    border-radius: 10px;
    margin: 0 var(--WINDOW-X-SPACE);
}
 
.scroll-container::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); 
    background-color: white;
}
.list-wrapper {
    background: orange;
    width: fit-content;
    height: 100%;
    display: flex;
    margin: 0;
    padding: 0;
    padding-top: 40px;
    position: relative;
}
.list-wrapper:before {
    content: "Unordened list";
    display: block;
    position: absolute;
    left: 15px;
    top: 12px;
}
.list-item {
    text-align: center;
    font-size: 18px;
    background: rgba(255, 255, 255, .5);
    border: solid 1px orange;
    width: 100px;
    flex-shrink: 0;
    height: 100%;

    /* Center slide text vertically */
    display: flex;
    justify-content: center;
    align-items: center;
}

.fix-offset {
    width: var(--WINDOW-X-SPACE);
    flex-shrink: 0;
}

个字符
标签:https://codepen.io/Alynva/full/gOPaGVX

erhoui1w

erhoui1w2#

我们的想法是在一个固定的元素上设置背景,并将列表放在它的顶部。

Jsfiddle Example

body {
  background: teal;
  margin: 0;
}
.background {
  background: purple;
  width: 80vw;
  height: 80vh;
  position: fixed;
  left: 10vw;
  top: 10vh;
}
.scrollable {
  list-style-type: none;
  position: relative;
  display: flex;
  padding: 0;
  margin: 20vh 0 0 10vw;
  height: 60vh;
}
.scrollable li {
  padding: 10px;
  background: orange;
  height: 100%;
  flex: 0 0 50vw;
  border: 1px solid darkorange;
  box-sizing: border-box;
}

个字符

tkclm6bt

tkclm6bt3#

试试这个(调整特定的值/单位以适应):

html, body {
  margin: 0;
  padding: 0;
  background: #12969D;
}

.container {
  height: 90vh;
  width: 90vw;
  padding: 40px 0;
  box-sizing: border-box;
  border: 1px solid black;
  background: #6B00BE;
  margin: 5vh auto;
}

ul {
  height: 100%;
  width: calc(100% + 75px);
  padding: 0;
  background: #FFBD37;
  list-style-type: none;
  box-sizing: border-box;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space:nowrap;
}

li {
  padding: 10px;
  display: inline-block;
  background: #FFD787;
  height: 100%;
  width: 50vw;
  border: 1px solid black;
}

个字符

3qpi33ja

3qpi33ja4#

请试试这个:
联系我们

<div id="project-slider">
   <div class="container">
      <ul class="items-holder">
         <li class="item" style="background: blue;"></li>
         <li class="item" style="background: red;"></li>
         <li class="item" style="background: green;"></li>
      </div>
   </div>
</div>

<div>
  <p>
    Just to show that currently the window is scrolling instead of the container.
  </p>
</div>

字符串
中文(简体)

html, body {
  margin: 0;
  padding: 0;
  background: #12969D;
}

.container {
  height: 90vh;
  width: 90vw;
  padding: 40px 0;
  box-sizing: border-box;
  border: 1px solid black;
  background: #6B00BE;
  margin: 5vh auto;
}

ul {
  height: 100%;
  width: calc(100% + 75px);
  padding: 0;
  background: #FFBD37;
  list-style-type: none;
  box-sizing: border-box;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space:nowrap;
}

li {
  padding: 10px;
  display: inline-block;
  background: #FFD787;
  height: 100%;
  width: 50vw;
  border: 1px solid black;
}


DEMO HERE

gxwragnw

gxwragnw5#

$container-width: 1160px;
$responsive-padding: 16px;

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  max-width: $container-width;
  margin: 0 auto;

  padding: 0 $responsive-padding;
  box-sizing: content-box;
}

.slider {
  background: rgb(210, 208, 255);
  padding: 80px 0;
  min-height: 100vh;
  

  &__track {
    margin-top: 30px;
    display: flex;
    gap: 20px;
    overflow: scroll;
    padding: 10px 0;

    /* Hide scrollbar for IE, Edge and Firefox */
    -ms-overflow-style: none; /* IE and Edge */
    scrollbar-width: none;
  }

  /* Hide scrollbar for Chrome, Safari and Opera */
  &__track::-webkit-scrollbar {
    display: none;
  }

  &__item {
    min-width: 600px;
    min-height: 180px;
    background: rgb(255, 255, 255);
    border-radius: 20px;

    /* CORE LOGIC */
    &:first-child {
      margin-left: calc((100vw - $container-width) / 2);
    }
    &:last-child {
      margin-right: calc((100vw - $container-width) / 2);
    }
  }
}

/* CORE LOGIC (responsive) @optional */
@media (max-width: calc($container-width + $responsive-padding)) {
  .slider {
    &__item {
      &:first-child {
        margin-left: $responsive-padding;
      }
      &:last-child {
        margin-right: $responsive-padding;
      }
    }
  }
}

个字符

相关问题