Bootstrap 4 Carousel指示器定位

时间:2018-04-03 00:22:35

标签: css twitter-bootstrap bootstrap-4 carousel

我正在设计一个Bootstrap 4旋转木马,我需要旋转木马指示灯进入左上角。但是我还需要它们与它上面的导航垂直对齐,它包含在Bootstrap类.container中。

因为我知道我的解释很可怕,所以这是一张图片,显示了我的意思:

enter image description here

如果我使用绝对位置,并添加top:0,left:0(或其他数字),则定位会根据屏幕大小而移动,并且与导航不匹配。

以下是我的代码大纲:

.carousel-indicators {
  top: 10px;
  left: 0;
  right: auto;
}

.carousel-indicators li {
  width: 20px;
  height: 20px;
  border-radius: 100%;
}

.carousel-indicators li:not(:last-child) {
  margin-right: 20px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>


<div class="container">
  <nav class="navbar navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
  </nav>
</div>

<div id="carousel" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="3"></li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="Third slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="Fourth slide">
    </div>
  </div>
</div>

我怎样才能做到这一点?什么是最好的方法?

谢谢!

1 个答案:

答案 0 :(得分:2)

只需覆盖旋转木马指示符的对齐内容和边距,然后添加填充,如下所示。还要在容器内包装指示符,并通过添加相对于它的预定义类位置使其成为位置:相对。

.my-slider .carousel-indicators {
  top: 10px;
  justify-content:flex-start;
  margin:0;
  padding-left:15px;
  padding-right:15px;
}

.carousel-indicators li {
  width: 20px;
  height: 20px;
  border-radius: 100%;
}

.carousel-indicators li:not(:last-child) {
  margin-right: 20px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>


<div class="container">
  <nav class="navbar navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
  </nav>
</div>

<div id="carousel" class="my-slider carousel slide" data-ride="carousel">
  <div class="container position-relative">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="3"></li>
  </ol>
 </div>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="Third slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="http://via.placeholder.com/800x300" alt="Fourth slide">
    </div>
  </div>
</div>