嗯,我读了所有相关的问题,但都没有解决我的问题.我正在试着做一个 carousel ,但它的滚动按钮没有垂直居中.我try 了top:Calc(200px/2)或100px,但它不能解决我的问题……因为无论 carousel 在哪里,它的位置都是静止的.我希望它位于 carousel 的中心,无论 carousel 在哪里,它都应该粘在 carousel 的中间.

   let currentScrollPosition = 0;
    let scrollAmount = 320;

    const sCont = document.querySelector(".card-container");
    const hScroll = document.querySelector(".horizontal-scroll");
    const btnScrollLeft = document.getElementById("btn-scroll-left");
    const btnScrollRight = document.getElementById("btn-scroll-right");
document.addEventListener('DOMContentLoaded', () => {
    // i prefer adding event listeners in javascript.
    btnScrollLeft.addEventListener('click', function(e) {
      e.preventDefault();
      scrollHorizontally(1);
    });
    btnScrollRight.addEventListener('click', function(e) {
      e.preventDefault();
      scrollHorizontally(-1);
    });
});

function scrollHorizontally(val) {
      let c_width = hScroll.offsetWidth,
          box_width = sCont.offsetWidth,
          amount = val * scrollAmount,
          diff = c_width - box_width;
      // to calculate the difference, we only need to subtract width of the div containing the cards.
      currentScrollPosition = currentScrollPosition + amount;

      if (currentScrollPosition > 0) {
        // cap start
        currentScrollPosition = 0;
        btnScrollLeft.style.opacity = "0";
      } else if (currentScrollPosition < diff) {
        // cap end
        currentScrollPosition = diff;
        btnScrollRight.style.opacity = "0";
      } else {
        btnScrollLeft.style.opacity = "1";
        btnScrollRight.style.opacity = "1";
      }
      sCont.style.left = currentScrollPosition + "px";
    }
<style>
     .horizontal-scroll {
      overflow-x: hidden;
    }

    .card-container {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-direction: row;
      flex-basis: 30%;
      position: absolute;
      gap: 10px;
      left: 0;
      transition: 1s left ease-out;
      overflow-x:hidden
    }

    .card-container .card {
      min-width: 240px;
      flex: 1;
      height: 200px;
      width: 100%;
      border: 1px solid #ccc;
      background: #fff;
      background: #00F260;
      /* fallback for old browsers */
      background: -webkit-linear-gradient(to bottom, #0575E6, #00F260);
      /* Chrome 10-25, Safari 5.1-6 */
      background: linear-gradient(to bottom, #0575E6, #00F260);
      /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
      box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.1);
      -webkit-box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.1);
      -moz-box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.1);
      text-align: center;
      justify-content: space-between;
    }

    .card-container .card img {
      flex-wrap: wrap;
      pointer-events: none;
      -moz-user-select: none;
      -webkit-user-select: none;
      user-select: none;
      width: 100%;
      float: none;
      display: block;
      object-fit: fill;
      height: 180px;
    }

    /* btn */

    .horizontal-scroll .btn-scroll {
      background: 0;
      border: 0;
      border: 1px solid red;
      padding: 0 10px;
      z-index: 10;
      cursor: pointer;
      font-size: 40px;
      color: #007bff;
    }

    .horizontal-scroll .btn-scroll:hover {
      color: #ffffff;
      background-color: #007bff;
      border-radius: 10px;
    }

    .horizontal-scroll .btn-scroll:focus {
      outline: none;
      background-color: #007bff;
      color: rgba(255, 255, 255, 1.0);
      border-radius: 5px;
    }

    .horizontal-scroll #btn-scroll-left {
      position: absolute;
      left: 0;
    }

    .horizontal-scroll #btn-scroll-right {
      position: absolute;
      right: 0;
    }
    @media( max-width:  768px){
      .card-container{
        overflow-x: hidden;
      }
    }
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" integrity="sha512-q3eWabyZPc1XTCmF+8/LuE1ozpg5xxn7iO89yfSOd5/oKvyqLngoNGsx8jq92Y8eXJ/IRxQbEC+FGSYxtk2oiw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sliding test</title>
  </head>
<body>
  <div class="horizontal-scroll">
      <button class="btn-scroll" id="btn-scroll-left" onclick="scrollHorizontally(1)"><i class="fas fa-chevron-left"></i>
    </button>
    
    <button class="btn-scroll" id="btn-scroll-right" onclick="scrollHorizontally(-1)"><i class="fas fa-chevron-right"></i>
    </button>
      <div class="card-container">
        <div class="card">Card 1</div>
        <div class="card">Card 2</div>
        <div class="card">Card 3</div>
        <div class="card">Card 4</div>
        <div class="card">Card 5</div>
        <div class="card">Card 1</div>
        <div class="card">Card 2</div>
        <div class="card">Card 3</div>
        <div class="card">Card 4</div>
        <div class="card">Card 5</div>
      </div>
      <!--card-container-->
    </div>
    <!--horizontal-scroll-->
    </body>
</html>

推荐答案

此css将为您提供所需的输出:

/* Add position: relative to .horizontal-scroll */
.horizontal-scroll {
  overflow-x: hidden;
  position: relative;
  height: 200px;
}

/* Remove position: absolute from .card-container */
.card-container {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  position: absolute;
  flex-direction: row;
  flex-basis: 30%;
  gap: 10px;
  left: 0;
  transition: 1s left ease-out;
  overflow-x: hidden;
}

.card-container .card {
  min-width: 240px;
  flex: 1;
  height: 200px;
  width: 100%;
  border: 1px solid #ccc;
  background: #fff;
  background: #00f260;
  /* fallback for old browsers */
  background: -webkit-linear-gradient(to bottom, #0575e6, #00f260);
  /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to bottom, #0575e6, #00f260);
  /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.1);
  -webkit-box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.1);
  -moz-box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.1);
  text-align: center;
  justify-content: space-between;
}

.card-container .card img {
  flex-wrap: wrap;
  pointer-events: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  width: 100%;
  float: none;
  display: block;
  object-fit: fill;
  height: 180px;
}

/* btn */
/* Add the top & transform properties to the buttons */
.horizontal-scroll .btn-scroll {
  background: 0;
  border: 0;
  border: 1px solid red;
  padding: 0 10px;
  z-index: 10;
  cursor: pointer;
  font-size: 40px;
  color: #007bff;
  top: 50%;
  transform: translateY(-50%);
}

.horizontal-scroll .btn-scroll:hover {
  color: #ffffff;
  background-color: #007bff;
  border-radius: 10px;
}

.horizontal-scroll .btn-scroll:focus {
  outline: none;
  background-color: #007bff;
  color: rgba(255, 255, 255, 1);
  border-radius: 5px;
}


.horizontal-scroll #btn-scroll-left {
  position: absolute;
  left: 0;
}

.horizontal-scroll #btn-scroll-right {
  position: absolute;
  right: 0; 
}
@media (max-width: 768px) {
  .card-container {
    overflow-x: hidden;
  }
}

This will give you the following output: enter image description here

Html相关问答推荐

如何才能完成这个背景按钮

仅包含名为X的子元素的元素的XPath?

使用CSS Flexbox时,我的图像未对齐

如何使背景图像在面包屑中相互重叠

有没有一种方法可以提高代码中tailwind 类名的可读性?

带有排序元素的 CSS 网格

顶部有文字的图像的悬停效果

并排放置两个 div,同时 div2 环绕 div1

对齐页面左侧的单选按钮

Angular:从服务器下载 HTML 并打印

如何将两个平行 div 的页面内的表格居中?

HTML 如何根据屏幕尺寸调整/裁剪视频背景?

输入框不是全宽

如何将 2 种不同的 colored颜色 应用于 css 中的复选框?

从右到左支持百分号

将 div 放在图片旁边而不是下方

涉及短代码时如何在页面上定位元素?

正确使用视频作为背景

沿文本对齐双引号

是否可以在换行时向锚标记添加填充?