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

djp7away  于 2023-11-15  发布在  其他
关注(0)|答案(5)|浏览(214)

我试图创建一个“滑块”,用户可以水平滚动到特定的“滑块项目”,我试图用纯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变量来计算元素之间的空间。

  1. :root {
  2. --WINDOW-X-SPACE: 20px;
  3. --CONTAINER-X-SPACE: 30px;
  4. }
  5. * {
  6. box-sizing: border-box;
  7. }
  8. html, body {
  9. height: 100vh;
  10. }
  11. body {
  12. background: teal;
  13. margin: 0;
  14. padding: 20px var(--WINDOW-X-SPACE);
  15. overflow-x: hidden;
  16. }
  17. main {
  18. padding: 10px var(--CONTAINER-X-SPACE);
  19. background: purple;
  20. }
  21. .scroll-container {
  22. height: 200px;
  23. margin: 0 calc(-1 * calc(var(--WINDOW-X-SPACE) + var(--CONTAINER-X-SPACE)));
  24. padding: 0 var(--WINDOW-X-SPACE);
  25. display: flex;
  26. overflow: auto;
  27. }
  28. .scroll-container::-webkit-scrollbar {
  29. all: unset;
  30. }
  31. .scroll-container::-webkit-scrollbar-track {
  32. -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
  33. border-radius: 10px;
  34. margin: 0 var(--WINDOW-X-SPACE);
  35. }
  36. .scroll-container::-webkit-scrollbar-thumb {
  37. border-radius: 10px;
  38. -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
  39. background-color: white;
  40. }
  41. .list-wrapper {
  42. background: orange;
  43. width: fit-content;
  44. height: 100%;
  45. display: flex;
  46. margin: 0;
  47. padding: 0;
  48. padding-top: 40px;
  49. position: relative;
  50. }
  51. .list-wrapper:before {
  52. content: "Unordened list";
  53. display: block;
  54. position: absolute;
  55. left: 15px;
  56. top: 12px;
  57. }
  58. .list-item {
  59. text-align: center;
  60. font-size: 18px;
  61. background: rgba(255, 255, 255, .5);
  62. border: solid 1px orange;
  63. width: 100px;
  64. flex-shrink: 0;
  65. height: 100%;
  66. /* Center slide text vertically */
  67. display: flex;
  68. justify-content: center;
  69. align-items: center;
  70. }
  71. .fix-offset {
  72. width: var(--WINDOW-X-SPACE);
  73. flex-shrink: 0;
  74. }

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

展开查看全部
erhoui1w

erhoui1w2#

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

Jsfiddle Example

  1. body {
  2. background: teal;
  3. margin: 0;
  4. }
  5. .background {
  6. background: purple;
  7. width: 80vw;
  8. height: 80vh;
  9. position: fixed;
  10. left: 10vw;
  11. top: 10vh;
  12. }
  13. .scrollable {
  14. list-style-type: none;
  15. position: relative;
  16. display: flex;
  17. padding: 0;
  18. margin: 20vh 0 0 10vw;
  19. height: 60vh;
  20. }
  21. .scrollable li {
  22. padding: 10px;
  23. background: orange;
  24. height: 100%;
  25. flex: 0 0 50vw;
  26. border: 1px solid darkorange;
  27. box-sizing: border-box;
  28. }

个字符

展开查看全部
tkclm6bt

tkclm6bt3#

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

  1. html, body {
  2. margin: 0;
  3. padding: 0;
  4. background: #12969D;
  5. }
  6. .container {
  7. height: 90vh;
  8. width: 90vw;
  9. padding: 40px 0;
  10. box-sizing: border-box;
  11. border: 1px solid black;
  12. background: #6B00BE;
  13. margin: 5vh auto;
  14. }
  15. ul {
  16. height: 100%;
  17. width: calc(100% + 75px);
  18. padding: 0;
  19. background: #FFBD37;
  20. list-style-type: none;
  21. box-sizing: border-box;
  22. overflow-x: scroll;
  23. overflow-y: hidden;
  24. white-space:nowrap;
  25. }
  26. li {
  27. padding: 10px;
  28. display: inline-block;
  29. background: #FFD787;
  30. height: 100%;
  31. width: 50vw;
  32. border: 1px solid black;
  33. }

个字符

展开查看全部
3qpi33ja

3qpi33ja4#

请试试这个:
联系我们

  1. <div id="project-slider">
  2. <div class="container">
  3. <ul class="items-holder">
  4. <li class="item" style="background: blue;"></li>
  5. <li class="item" style="background: red;"></li>
  6. <li class="item" style="background: green;"></li>
  7. </div>
  8. </div>
  9. </div>
  10. <div>
  11. <p>
  12. Just to show that currently the window is scrolling instead of the container.
  13. </p>
  14. </div>

字符串
中文(简体)

  1. html, body {
  2. margin: 0;
  3. padding: 0;
  4. background: #12969D;
  5. }
  6. .container {
  7. height: 90vh;
  8. width: 90vw;
  9. padding: 40px 0;
  10. box-sizing: border-box;
  11. border: 1px solid black;
  12. background: #6B00BE;
  13. margin: 5vh auto;
  14. }
  15. ul {
  16. height: 100%;
  17. width: calc(100% + 75px);
  18. padding: 0;
  19. background: #FFBD37;
  20. list-style-type: none;
  21. box-sizing: border-box;
  22. overflow-x: scroll;
  23. overflow-y: hidden;
  24. white-space:nowrap;
  25. }
  26. li {
  27. padding: 10px;
  28. display: inline-block;
  29. background: #FFD787;
  30. height: 100%;
  31. width: 50vw;
  32. border: 1px solid black;
  33. }


DEMO HERE

展开查看全部
gxwragnw

gxwragnw5#

  1. $container-width: 1160px;
  2. $responsive-padding: 16px;
  3. * {
  4. margin: 0;
  5. padding: 0;
  6. box-sizing: border-box;
  7. }
  8. .container {
  9. max-width: $container-width;
  10. margin: 0 auto;
  11. padding: 0 $responsive-padding;
  12. box-sizing: content-box;
  13. }
  14. .slider {
  15. background: rgb(210, 208, 255);
  16. padding: 80px 0;
  17. min-height: 100vh;
  18. &__track {
  19. margin-top: 30px;
  20. display: flex;
  21. gap: 20px;
  22. overflow: scroll;
  23. padding: 10px 0;
  24. /* Hide scrollbar for IE, Edge and Firefox */
  25. -ms-overflow-style: none; /* IE and Edge */
  26. scrollbar-width: none;
  27. }
  28. /* Hide scrollbar for Chrome, Safari and Opera */
  29. &__track::-webkit-scrollbar {
  30. display: none;
  31. }
  32. &__item {
  33. min-width: 600px;
  34. min-height: 180px;
  35. background: rgb(255, 255, 255);
  36. border-radius: 20px;
  37. /* CORE LOGIC */
  38. &:first-child {
  39. margin-left: calc((100vw - $container-width) / 2);
  40. }
  41. &:last-child {
  42. margin-right: calc((100vw - $container-width) / 2);
  43. }
  44. }
  45. }
  46. /* CORE LOGIC (responsive) @optional */
  47. @media (max-width: calc($container-width + $responsive-padding)) {
  48. .slider {
  49. &__item {
  50. &:first-child {
  51. margin-left: $responsive-padding;
  52. }
  53. &:last-child {
  54. margin-right: $responsive-padding;
  55. }
  56. }
  57. }
  58. }

个字符

展开查看全部

相关问题