首页 > 解决方案 > 添加元素时时间轴滑块被压缩

问题描述

我正在尝试使用滑块构建一个时间轴,您可以在其中隐藏更多元素,单击按钮时,它将滑动并显示隐藏的元素。问题是当我添加更多元素时,它会缩小其他元素以适应 div,但我需要它来隐藏通过 div 边缘的元素,我可以使用水平滚动条或单击按钮来显示它们。

谁能帮我??

这是我到目前为止的代码,如果您想查看问题,只需将更多项目添加到列表中。

https://codepen.io/Arthur_Oliveira/pen/GRKNGBb

.timeline-background {
  position: relative;
  width: 100%;
  height: 300px;
  background-color: #F5F5F5;
  padding: 0;
  padding-top: 50px;
}

.list-items {
  position: absolute;
  width: auto;
  height: auto;
  margin: 0;
  padding: 0;
  margin-top: 20px;
  max-width: 400px;
  overflow: hidden;
  object-fit: cover;
}

.list-item {
  position: absolute;
  width: 500px;
  top: 0;
  height: auto;
  margin: 0;
  padding: 0;
}

.list-items li {
  position: relative;
  display: flex;
}

.list-item li:first-child {
  position: absolute;
  left: 78px;
  top: 63px;
  cursor: pointer;
  list-style: none;
  width: 30px;
  height: 30px;
  background-color: #fff;
  border: 3px solid #AD80FF;
  color: #D9D9D9;
  border-radius: 50%;
  float: left;
}

.list-item li:last-child {
  position: absolute;
  right: -13px;
  top: 63px;
  cursor: pointer;
  list-style: none;
  width: 30px;
  height: 30px;
  margin-left: 50px;
  background-color: #fff;
  border: 3px solid #AD80FF;
  color: #D9D9D9;
  border-radius: 50%;
  float: left;
}

.list-item li:last-child:hover {
  color: #FF04FF;
}

.list-item li:first-child:hover {
  color: #FF04FF;
}

.list-item .active {
}

.list-items li {
  cursor: pointer;
  margin-left: 50px;
  list-style: none;
  width: 15px;
  height: 15px;
  background-color: #fff;
  border: 3px solid #D9D9D9;
  margin-top: 0;
  border-radius: 50%;
  float: left;
}

.list-items li.active {
  cursor: pointer;
  margin-left: 50px;
  list-style: none;
  border: 3px solid #FF04FF;
  background-color: #FF04FF;
  border-radius: 50%;
  float: left;
}

.list-item li:first-child:after {
  cursor: initial;
  position: absolute;
  content: '';
  width: 50px;
  margin-top: -6px;
  margin-left: 40px;
  height: 50%;
  border-bottom: 2px solid #D9D9D9;
  z-index: 0;
}

.list-items li:after {
  cursor: initial;
  position: absolute;
  content: '';
  width: 50px;
  margin-top: 0px;
  margin-left: 15px;
  height: 50%;
  border-bottom: 2px solid #D9D9D9;
  z-index: 0;
}

.list-item ul .actived:after {
  border-bottom: 2px solid #FF04FF;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex justify-content-center timeline-background">
    <div class="d-flex justify-content-center">
        <div class="list-items">
            <ul class="d-flex d-xl-flex justify-content-center align-items-center justify-content-xl-center">
                <li class="active"><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
              
            </ul>
        </div>
        <div class="list-item">
            <ul>
                <li class="d-flex d-sm-flex d-md-flex d-lg-flex d-xl-flex justify-content-center align-items-center justify-content-sm-center align-items-sm-center justify-content-md-center align-items-md-center actived" href="#list1"><i class="icon ion-ios-arrow-back"></i></li>
                <li class="d-flex justify-content-center align-items-center"><a class="item" href="#"></a><i class="icon ion-ios-arrow-forward"></i></li>
            </ul>
        </div>
    </div>
</div>

更新:: 我将类列表项更改为:

.timeline-background {
  position: relative;
  width: 100%;
  height: 300px;
  background-color: #F5F5F5;
  padding: 0;
  padding-top: 50px;
}

.list-items {
  position: relative;
  width: auto;
  height: auto;
  margin: 0;
  padding: 0;
  margin-top: 20px;
  margin-left: 86px;
  max-width: 440px;
}



.list-item {
  position: absolute;
  width: 500px;
  top: 0;
  height: auto;
  margin: 0;
  padding: 0;
}

.list-items li {
  position: relative;
  display: flex;
}

.list-item li:first-child {
  position: absolute;
  left: 78px;
  top: 63px;
  cursor: pointer;
  list-style: none;
  width: 30px;
  height: 30px;
  background-color: #fff;
  border: 3px solid #AD80FF;
  color: #D9D9D9;
  border-radius: 50%;
  float: left;
}

.list-item li:last-child {
  position: absolute;
  right: -13px;
  top: 63px;
  cursor: pointer;
  list-style: none;
  width: 30px;
  height: 30px;
  margin-left: 50px;
  background-color: #fff;
  border: 3px solid #AD80FF;
  color: #D9D9D9;
  border-radius: 50%;
  float: left;
}

.list-item li:last-child:hover {
  color: #FF04FF;
}

.list-item li:first-child:hover {
  color: #FF04FF;
}

.list-item .active {
}

.list-items li {
  cursor: pointer;
  margin-left: 50px;
  list-style: none;
  width: 15px;
  height: 15px;
  background-color: #fff;
  border: 3px solid #D9D9D9;
  margin-top: 0;
  border-radius: 50%;
  float: left;
}

.list-items li.active {
  cursor: pointer;
  margin-left: 50px;
  list-style: none;
  border: 3px solid #FF04FF;
  background-color: #FF04FF;
  border-radius: 50%;
  float: left;
}

.list-item li:first-child:after {
  cursor: initial;
  position: absolute;
  content: '';
  width: 50px;
  margin-top: -6px;
  margin-left: 40px;
  height: 50%;
  border-bottom: 2px solid #D9D9D9;
  z-index: 0;
}

.list-items li:after {
  cursor: initial;
  position: absolute;
  content: '';
  width: 50px;
  margin-top: 0px;
  margin-left: 15px;
  height: 50%;
  border-bottom: 2px solid #D9D9D9;
  z-index: 0;
}

.list-item ul .actived:after {
  border-bottom: 2px solid #FF04FF;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex justify-content-center timeline-background">
    <div class="d-flex justify-content-center">
        <section class="list-items">
            <ul class="dots">
                <li class="active"><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
            </ul>
        </section>
        <div class="list-item">
            <ul>
                <li class="d-flex d-sm-flex d-md-flex d-lg-flex d-xl-flex justify-content-center align-items-center justify-content-sm-center align-items-sm-center justify-content-md-center align-items-md-center actived" href="#list1"><i class="icon ion-ios-arrow-back"></i></li>
                <li class="d-flex justify-content-center align-items-center"><a class="item" href="#"></a><i class="icon ion-ios-arrow-forward"></i></li>
            </ul>
        </div>
    </div>
</div>

现在它在到达 div 边缘时打破了界限!

标签: htmlcsssass

解决方案


非常感谢那些试图提供帮助的人,我真的很感激!我在这里发布我是如何解决这个问题的!从问题文本中,您可以将我遇到的代码与此处的最终代码进行比较。我所做的是添加backpage类,并使用隐藏溢出和绝对位置为其设置宽度,然后将list-items类位置更改为绝对位置并为其设置固定宽度,而不是仅调整屏幕上的元素位置!

这里是最终代码:

.timeline-background {
  position: relative;
  width: 100%;
  height: 300px;
  background-color: #F5F5F5;
  padding: 0;
  padding-top: 50px;
}

.backpage {
  position: relative;
  overflow: auto;
  width: 450px;
}

.list-items {
  position: absolute;
  width: 2000px;
  height: auto;
  margin: 0;
  padding: 0;
  margin-top: 20px;
  margin-left: -10px;
}

.list-item {
  position: absolute;
  width: 500px;
  top: 0;
  height: auto;
  margin: 0;
  padding: 0;
}

.list-items li {
  position: relative;
}

.list-item li:first-child {
  position: absolute;
  left: 20px;
  top: 63px;
  cursor: pointer;
  list-style: none;
  width: 30px;
  height: 30px;
  background-color: #fff;
  border: 3px solid #AD80FF;
  color: #D9D9D9;
  border-radius: 50%;
  float: left;
}

.list-item li:last-child {
  position: absolute;
  right: -13px;
  top: 63px;
  cursor: pointer;
  list-style: none;
  width: 30px;
  height: 30px;
  margin-left: 50px;
  background-color: #fff;
  border: 3px solid #AD80FF;
  color: #D9D9D9;
  border-radius: 50%;
  float: left;
}

.list-item li:last-child:hover {
  color: #FF04FF;
}

.list-item li:first-child:hover {
  color: #FF04FF;
}

.list-item .active {
}

.list-items li {
  cursor: pointer;
  margin-left: 50px;
  list-style: none;
  width: 15px;
  height: 15px;
  background-color: #fff;
  border: 3px solid #D9D9D9;
  margin-top: 0;
  border-radius: 50%;
  float: left;
}

.list-items li.active {
  cursor: pointer;
  margin-left: 50px;
  list-style: none;
  border: 3px solid #FF04FF;
  background-color: #FF04FF;
  border-radius: 50%;
  float: left;
}

.list-items li:not(:last-child):after {
  cursor: initial;
  position: absolute;
  content: '';
  width: 50px;
  margin-top: 0px;
  margin-left: 12px;
  height: 50%;
  border-bottom: 2px solid #D9D9D9;
  z-index: 0;
}

.list-items li:first-child:before {
  cursor: initial;
  position: absolute;
  content: '';
  width: 50px;
  margin-top: 0px;
  margin-left: -53px;
  height: 50%;
  border-bottom: 2px solid #D9D9D9;
  z-index: 0;
}

.list-items li:last-child:after {
  cursor: initial;
  position: absolute;
  content: '';
  width: 50px;
  margin-top: 0px;
  margin-left: 12px;
  height: 50%;
  border-bottom: 2px dashed #D9D9D9;
  z-index: 0;
}

.list-item ul .actived:after {
  border-bottom: 2px solid #FF04FF;
}

.list-item ul .actived {
  color: #FF04FF;
}

.dots {
  width: 100%;
  position: relative;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex d-xl-flex justify-content-center justify-content-xl-center timeline-background">
    <div class="backpage">
        <section class="list-items">
            <ul class="dots">
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
                <li><a class="item" href="#"></a></li>
            </ul>
        </section>
    </div>
    <div class="list-item">
        <ul>
            <li class="d-flex d-sm-flex d-md-flex d-lg-flex d-xl-flex justify-content-center align-items-center justify-content-sm-center align-items-sm-center justify-content-md-center align-items-md-center" href="#list1"><i class="icon ion-ios-arrow-back"></i></li>
            <li class="d-flex justify-content-center align-items-center"><a class="item" href="#"></a><i class="icon ion-ios-arrow-forward"></i></li>
        </ul>
    </div>
</div>

再次,非常感谢!!


推荐阅读