当单击控件时,轮播效果不起作用

时间:2019-04-01 13:26:28

标签: html css twitter-bootstrap bootstrap-4

我在轮播中滚动效果时遇到问题。只是动画,简单的滚动工作,而动画则没有。 也许有人可以检查我的代码并修复^ _ ^谢谢。 image使用类carousel-image-1(2,3)添加CSS。我比较了官方引导站点上的示例,并且都比较了我的代码。也许我看不到错误

.carousel-item {
  height: 450px;
}

.carousel-image-1 {
  background: url(https://source.unsplash.com/random/560x560);
  background-size: cover;
}

.carousel-image-2 {
  background: url(https://source.unsplash.com/random/560x560);
  background-size: cover;
}

.carousel-image-3 {
  background: url(https://source.unsplash.com/random/560x560);
  background-size: cover;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
    integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
  <link rel="stylesheet" href="/css/style.css">
  <title>Title</title>
</head>

<body>

  <!-- Showcase Slider-->

  <section class="showcase">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
      <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
      </ol>
      <div class="carousel-inner">
        <div class="carousel-item carousel-image-1 active">
          <div class="container">
            <div class="carousel-caption d-none d-sm-block text-right mb-5">
              <h1 class="display-3">
                Heading One
              </h1>
              <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
              <a href="#" class="btn btn-danger btn-lg">Sign Up Now</a>
            </div>
          </div>
        </div>

        <div class="carousel-item carousel-image-2 ">
          <div class="container">
            <div class="carousel-caption d-none d-sm-block  mb-5">
              <h1 class="display-3">
                Heading Two
              </h1>
              <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
              <a href="#" class="btn btn-primary btn-lg">Learn More</a>
            </div>
          </div>
        </div>

        <div class="carousel-item carousel-image-3 ">
          <div class="container">
            <div class="carousel-caption d-none d-sm-block text-right mb-5">
              <h1 class="display-3">
                Heading Three
              </h1>
              <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
              <a href="#" class="btn btn-success btn-lg">Learn More</a>
            </div>
          </div>
        </div>
      </div>

      <a href="#myCarousel" class="carousel-control-prev" data-slide="prev">
        <span class="carousel-control-prev-icon"></span>
      </a>
      <a href="#myCarousel" class="carousel-control-next" data-slide="next">
        <span class="carousel-control-next-icon"></span>
      </a>
    </div>
  </section>

  <script src="http://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
    integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
  </script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
    integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
  </script>
 
</body>

</html>

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
          integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
    <link rel="stylesheet" href="/css/style.css">
    <title>Title</title>
</head>

<body>

<!-- Showcase Slider-->

<section class="showcase">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
            <li data-target="#myCarousel" data-slide-to="1"></li>
            <li data-target="#myCarousel" data-slide-to="2"></li>
        </ol>
        <div class="carousel-inner">
            <div class="carousel-item carousel-image-1 active">
                <div class="container">
                    <div class="carousel-caption d-none d-sm-block text-right mb-5">
                        <h1 class="display-3">
                            Heading One
                        </h1>
                        <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                            repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
                        <a href="#" class="btn btn-danger btn-lg">Sign Up Now</a>
                    </div>
                </div>
            </div>

            <div class="carousel-item carousel-image-2 ">
                <div class="container">
                    <div class="carousel-caption d-none d-sm-block text-right mb-5">
                        <h1 class="display-3">
                            Heading Two
                        </h1>
                        <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                            repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
                        <a href="#" class="btn btn-primary btn-lg">Learn More</a>
                    </div>
                </div>
            </div>

            <div class="carousel-item carousel-image-3 ">
                <div class="container">
                    <div class="carousel-caption d-none d-sm-block text-right mb-5">
                        <h1 class="display-3">
                            Heading Three
                        </h1>
                        <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                            repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
                        <a href="#" class="btn btn-success btn-lg">Learn More</a>
                    </div>
                </div>
            </div>
        </div>

        <a href="#myCarousel" class="carousel-control-prev" data-slide="prev">
            <span class="carousel-control-prev-icon"></span>
        </a>
        <a href="#myCarousel" class="carousel-control-next" data-slide="next">
            <span class="carousel-control-next-icon"></span>
        </a>
    </div>
</section>

<script src="http://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
</script>
<style>
    section.showcase {
        float: left;
        width: 100%;
        height: 100%;
    }
    .carousel {
        float: left;
        width: 100%;
        position: unset;
    }
    .carousel-inner {
        float: left;
        width: 100%;
        height: 100%;
        position: unset !important;
    }
    .carousel-item {
         float: left;
         position: initial !important;
         height: 100%;
         width: 100%;
     }
    p.lead {
        color: #000;
        font-size: 14px;
    }
    h1.display-3 {
        color: #000;
    }
    ol.carousel-indicators {
        background: #000;
    }
a.carousel-control-next {
    background: #000;
}
a.carousel-control-prev {
    background: #000;
}
</style>
</body>

</html>

我添加了样式...请检查一下

答案 1 :(得分:0)

您只需要为滑块指定一个高度-默认高度为0像素。 :-)

如下所示,我刚刚添加了400px的min-height(您可以将其设置为任意值),并且还设置了随机背景,以便您可以看到白色文本(因为它没有出现在您的示例中,但是滑块实际上是在幕后工作。

.carousel-item {
  min-height: 400px;
  background-image: url('https://picsum.photos/1200/400');
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
    integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
  <link rel="stylesheet" href="/css/style.css">
  <title>Title</title>
</head>

<body>

  <!-- Showcase Slider-->

  <section class="showcase">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
      <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
      </ol>
      <div class="carousel-inner">
        <div class="carousel-item carousel-image-1 active">
          <div class="container">
            <div class="carousel-caption d-none d-sm-block text-right mb-5">
              <h1 class="display-3">
                Heading One
              </h1>
              <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
              <a href="#" class="btn btn-danger btn-lg">Sign Up Now</a>
            </div>
          </div>
        </div>

        <div class="carousel-item carousel-image-2 ">
          <div class="container">
            <div class="carousel-caption d-none d-sm-block  mb-5">
              <h1 class="display-3">
                Heading Two
              </h1>
              <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
              <a href="#" class="btn btn-primary btn-lg">Learn More</a>
            </div>
          </div>
        </div>

        <div class="carousel-item carousel-image-3 ">
          <div class="container">
            <div class="carousel-caption d-none d-sm-block text-right mb-5">
              <h1 class="display-3">
                Heading Three
              </h1>
              <p class="lead">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus nostrum excepturi
                repellendus quos provident sit! Odio odit quo voluptatum aperiam.</p>
              <a href="#" class="btn btn-success btn-lg">Learn More</a>
            </div>
          </div>
        </div>
      </div>

      <a href="#myCarousel" class="carousel-control-prev" data-slide="prev">
        <span class="carousel-control-prev-icon"></span>
      </a>
      <a href="#myCarousel" class="carousel-control-next" data-slide="next">
        <span class="carousel-control-next-icon"></span>
      </a>
    </div>
  </section>

  <script src="http://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
    integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
  </script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
    integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
  </script>
 
</body>

</html>