为什么我的Bootstrap 4轮播指示器无法点击?

时间:2018-04-08 16:01:03

标签: twitter-bootstrap bootstrap-4 carousel

我一直在制作旋转木马,我从Bootstrap 4文档中复制了代码的大纲。但是当我编辑它看起来像我需要它时,指标不再在点击时改变幻灯片了吗?

我需要编辑什么来修复此问题?我认为它不是添加的CSS,因为我删除了它,它仍然无法正常工作。可能是HTML中的错误。但是,我无法看到我做错了什么。有人可以看看吗?

这是我的代码:



#hero-slider {
  margin-bottom: 30px;
}

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

.carousel-inner {
  max-height: 500px;
}

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

.carousel-indicators li:not(:last-child) {
  margin-right: 20px;
}

.carousel-indicators .active {
  background-color: #fff;
}

.carousel-item:before {
  -webkit-box-shadow: inset 0 20rem 40rem rgba(0, 0, 0, 0.5);
  box-shadow: inset 0 20rem 40rem rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  content: "";
}

.carousel-caption {
  top: 50px;
  bottom: auto;
}

.carousel-caption h5 {
  color: #ffa70c;
  font-size: 34px;
  font-weight: 600;
  line-height: 1.2;
  margin-bottom: 25px;
}

.carousel-caption p {
  color: #fff;
  font-size: 18px;
  letter-spacing: 1px;
  font-weight: 600;
  line-height: 1.2;
  margin-bottom: 50px;
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div id="hero-slider" class="carousel slide" data-ride="carousel">
  <div class="container">
    <ol class="carousel-indicators position-relative">
      <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="https://placeimg.com/1000/480/any" alt="First slide">
      <div class="carousel-caption">
        <h5>Title goes here</h5>
        <p>Subtitle goes here</p>
        <a href="#" class="btn btn-primary border-0">Button Text</a>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://placeimg.com/1000/480/any" alt="Second slide">
      <div class="carousel-caption">
        <h5>Title goes here</h5>
        <p>Subtitle goes here</p>
        <a href="#" class="btn btn-primary border-0">Button Text</a>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://placeimg.com/1000/480/any" alt="Third slide">
      <div class="carousel-caption">
        <h5>Title goes here</h5>
        <p>Subtitle goes here</p>
        <a href="#" class="btn btn-primary border-0">Button Text</a>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://placeimg.com/1000/480/any" alt="Fourth slide">
      <div class="carousel-caption">
        <h5>Title goes here</h5>
        <p>Subtitle goes here</p>
        <a href="#" class="btn btn-primary border-0">Button Text</a>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

谢谢!

1 个答案:

答案 0 :(得分:1)

因为您的轮播ID是英雄 - 滑块,您需要将指标数据目标更改为此值而不是当前值:

从:

data-target="#carouselExampleIndicators"

为:

data-target="#hero-slider"

&#13;
&#13;
#hero-slider {
    margin-bottom: 30px;
}

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

.carousel-inner {
    max-height: 500px;
}

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

.carousel-indicators li:not(:last-child) {
    margin-right: 20px;
}

.carousel-indicators .active {
    background-color: #fff;
}

.carousel-item:before {
    -webkit-box-shadow: inset 0 20rem 40rem rgba(0, 0, 0, 0.5);
    box-shadow: inset 0 20rem 40rem rgba(0, 0, 0, 0.5);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    content: "";
}

.carousel-caption {
    top: 50px;
    bottom: auto;
}

.carousel-caption h5 {
    color: #ffa70c;
    font-size: 34px;
    font-weight: 600;
    line-height: 1.2;
    margin-bottom: 25px;
}

.carousel-caption p {
    color: #fff;
    font-size: 18px;
    letter-spacing: 1px;
    font-weight: 600;
    line-height: 1.2;
    margin-bottom: 50px;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>


<div id="hero-slider" class="carousel slide" data-ride="carousel">
    <div class="container">
        <ol class="carousel-indicators position-relative">
            <li data-target="#hero-slider" data-slide-to="0" class="active"></li>
            <li data-target="#hero-slider" data-slide-to="1"></li>
            <li data-target="#hero-slider" data-slide-to="2"></li>
            <li data-target="#hero-slider" data-slide-to="3"></li>
        </ol>
    </div>
    <div class="carousel-inner" role="listbox">
        <div class="carousel-item active">
            <img class="d-block w-100" src="https://placeimg.com/1000/480/any" alt="First slide">

            <div class="carousel-caption">
                <h5>Title goes here</h5>

                <p>Subtitle goes here</p>
                <a href="#" class="btn btn-primary border-0">Button Text</a>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="https://placeimg.com/1000/480/any" alt="Second slide">

            <div class="carousel-caption">
                <h5>Title goes here</h5>

                <p>Subtitle goes here</p>
                <a href="#" class="btn btn-primary border-0">Button Text</a>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="https://placeimg.com/1000/480/any" alt="Third slide">

            <div class="carousel-caption">
                <h5>Title goes here</h5>

                <p>Subtitle goes here</p>
                <a href="#" class="btn btn-primary border-0">Button Text</a>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="https://placeimg.com/1000/480/any" alt="Fourth slide">

            <div class="carousel-caption">
                <h5>Title goes here</h5>

                <p>Subtitle goes here</p>
                <a href="#" class="btn btn-primary border-0">Button Text</a>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;