bootstrap carousel不能正常工作 - 摇动项目背景

时间:2015-07-06 07:26:48

标签: jquery twitter-bootstrap-3 carousel

我不确定我做错了什么,但我对引导转盘有一个奇怪的问题。在幻灯片放映期间,我的物品在摇晃。您可以在下面查看。

  <section id="main-slider" class="no-margin">
    <div class="carousel slide">


    <div class="reservation-nav">
            <div class="container">
                <div class="row">
                    <div class="col-md-4">

                        <div class="btn-group btn-group-justified">
                                <div class="btn-group">
                                  <button type="button" id="login_click" class="btn btn-default active btnvisit" data-toggle="button">Umów wizytę</button>
                                </div>

                                <div class="btn-group">
                                  <button type="button" id="register_click" class="btn btn-default btncall">Zadzwoń do nas</button>
                                </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>          


        <div class="carousel-inner">
            <div class="item active" style="background-image: url(http://lorempixel.com/output/fashion-q-c-1400-650-4.jpg)" data-ride="carousel">
                <div class="container">
                    <div class="row slide-margin">
                        <div class="col-sm-6">
                            <div class="carousel-content">
                            </div>
                        </div>

                    </div>
                </div>
            </div><!--/.item-->

            <div class="item" style="background-image: urlhttp://lorempixel.com/output/people-q-c-1400-650-5.jpg)" data-ride="carousel">
                <div class="container">
                    <div class="row slide-margin">
                        <div class="col-sm-6">
                            <div class="carousel-content">
                            </div>
                        </div>

                    </div>
                </div>
            </div><!--/.item-->

            <div class="item" style="background-image: url(http://lorempixel.com/output/city-q-c-1400-650-6.jpg)" data-ride="carousel">
                <div class="container">
                    <div class="row slide-margin">
                        <div class="col-sm-6">
                            <div class="carousel-content">
                            </div>
                        </div>

                    </div>
                </div>
            </div><!--/.item-->
        </div><!--/.carousel-inner-->
    </div><!--/.carousel-->
    <a class="prev hidden-xs" href="#main-slider" data-slide="prev">
        <i class="fa fa-chevron-left"></i>
    </a>
    <a class="next hidden-xs" href="#main-slider" data-slide="next">
        <i class="fa fa-chevron-right"></i>
    </a>
</section><!--/#main-slider-->

JS:

$(function(){
    $('#main-slider .carousel').carousel({
        interval: 1000,
        cycle: 'true'
    });
});

CSS

    #main-slider {
  position: relative;
}

.no-margin {
  margin: 0;
  padding: 0;
}

#main-slider .carousel .carousel-content {
  margin-top: 150px;
}

#main-slider .carousel .slide-margin{
  margin-top: 140px;
}

#main-slider .carousel h2 {
  color: #fff;
}

#main-slider .carousel .btn-slide {
  padding: 8px 20px;
  background: #b98bae;
  color: #fff;
  border-radius: 4px;
  margin-top: 25px;
  display: inline-block;
}

#main-slider .carousel .slider-img{
  text-align: right;
  position: absolute;
}


#main-slider .carousel .item {
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: cover;
  left: 0 !important;
  opacity: 0;
  top: 0;
  position: absolute;
  width: 100%;
  display: block !important;
  height: 650px!important;
  -webkit-transition: opacity ease-in-out 500ms;
  -moz-transition: opacity ease-in-out 500ms;
  -o-transition: opacity ease-in-out 500ms;
  transition: opacity ease-in-out 500ms;
}

#main-slider .carousel .item:first-child {
  top: auto;
  position: relative;
}

#main-slider .carousel .item.active {
  opacity: 1;
  -webkit-transition: opacity ease-in-out 500ms;
  -moz-transition: opacity ease-in-out 500ms;
  -o-transition: opacity ease-in-out 500ms;
  transition: opacity ease-in-out 500ms;
  z-index: 1;
}

#main-slider .prev,
#main-slider .next {
  position: absolute;
  top: 50%;
  background-color: #a02270;
  color: #fff;
  display: inline-block;
  margin-top: -25px;
  height: 40px;
  line-height: 40px;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 4px;
  z-index: 5;
}

#main-slider .prev:hover,
#main-slider .next:hover {
  background-color: #b98bae;
}

#main-slider .prev {
  left: 10px;
}

#main-slider .next {
  right: 10px;
}

#main-slider .carousel-indicators li {
  width: 20px;
  height: 20px;
  background-color: #fff;
  margin: 0 15px 0 0;
  position: relative;
}

#main-slider .carousel-indicators li:after {
  position: absolute;
  content: "";
  width: 24px;
  height: 24px;
  border-radius: 100%;
  background: rgba(0, 0, 0, 0);
  border: 1px solid #FFF;
  left: -3px;
  top: -3px;
}

#main-slider .carousel-indicators .active{
  width: 20px;
  height: 20px;
  background-color: #a02270;
   margin: 0 15px 0 0;
   border: 1px solid #a02270;
   position: relative;
}

#main-slider .carousel-indicators .active:after {
  position: absolute;
  content: "";
  width: 24px;
  height: 24px;
  border-radius: 100%;
  background: #ca02270;
  border: 1px solid #a02270;
  left: -3px;
  top: -3px;
}

#main-slider .active .animation.animated-item-1 {
  -webkit-animation: fadeInUp 300ms linear 300ms both;
  -moz-animation: fadeInUp 300ms linear 300ms both;
  -o-animation: fadeInUp 300ms linear 300ms both;
  -ms-animation: fadeInUp 300ms linear 300ms both;
  animation: fadeInUp 300ms linear 300ms both;
}

#main-slider .active .animation.animated-item-2 {
  -webkit-animation: fadeInUp 300ms linear 600ms both;
  -moz-animation: fadeInUp 300ms linear 600ms both;
  -o-animation: fadeInUp 300ms linear 600ms both;
  -ms-animation: fadeInUp 300ms linear 600ms both;
  animation: fadeInUp 300ms linear 600ms both;
}

#main-slider .active .animation.animated-item-3 {
  -webkit-animation: fadeInUp 300ms linear 900ms both;
  -moz-animation: fadeInUp 300ms linear 900ms both;
  -o-animation: fadeInUp 300ms linear 900ms both;
  -ms-animation: fadeInUp 300ms linear 900ms both;
  animation: fadeInUp 300ms linear 900ms both;
}

#main-slider .active .animation.animated-item-4 {
  -webkit-animation: fadeInUp 300ms linear 1200ms both;
  -moz-animation: fadeInUp 300ms linear 1200ms both;
  -o-animation: fadeInUp 300ms linear 1200ms both;
  -ms-animation: fadeInUp 300ms linear 1200ms both;
  animation: fadeInUp 300ms linear 1200ms both;
}

#reservation {
    background:#fff;
    padding:30px 0;
    text-align:justify;
}

.reservation-nav {
    position:absolute;
    z-index:15;
    bottom:0px;
    width:100%;
}

#res-content-2 {display:none;}

#res-content-2, #res-content-1 {
    text-align:center;
}

我试过通过css height属性来解决它,到目前为止还没有成功。你知道主要问题是什么吗? jsfiddle

1 个答案:

答案 0 :(得分:1)

你的JS应该是这样的(只调用#main-slider):1000也可能太快了。

$(function(){
    $('#main-slider').carousel({
        interval: 5000,
        cycle: 'true'
    });
});

您在第二张背景图片代码中也错过了网址和http之间的(

您还必须将onclick="$('#main-slider').carousel('prev')onclick="$('#main-slider').carousel('next')添加到PREV和NEXT按钮以激活它们。

工作修正:

&#13;
&#13;
$(function() {
  $('#main-slider').carousel({
    interval: 5000,
    cycle: 'true'
  });
});
&#13;
#main-slider {
  position: relative;
}
.no-margin {
  margin: 0;
  padding: 0;
}
#main-slider .carousel .carousel-content {
  margin-top: 150px;
}
#main-slider .carousel .slide-margin {
  margin-top: 140px;
}
#main-slider .carousel h2 {
  color: #fff;
}
#main-slider .carousel .btn-slide {
  padding: 8px 20px;
  background: #b98bae;
  color: #fff;
  border-radius: 4px;
  margin-top: 25px;
  display: inline-block;
}
#main-slider .carousel .slider-img {
  text-align: right;
  position: absolute;
}
#main-slider .carousel .item {
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: cover;
  left: 0 !important;
  opacity: 0;
  top: 0;
  position: absolute;
  width: 100%;
  display: block !important;
  height: 650px!important;
  -webkit-transition: opacity ease-in-out 500ms;
  -moz-transition: opacity ease-in-out 500ms;
  -o-transition: opacity ease-in-out 500ms;
  transition: opacity ease-in-out 500ms;
}
#main-slider .carousel .item:first-child {
  top: auto;
  position: relative;
}
#main-slider .carousel .item.active {
  opacity: 1;
  -webkit-transition: opacity ease-in-out 500ms;
  -moz-transition: opacity ease-in-out 500ms;
  -o-transition: opacity ease-in-out 500ms;
  transition: opacity ease-in-out 500ms;
  z-index: 1;
}
#main-slider .prev,
#main-slider .next {
  position: absolute;
  top: 50%;
  background-color: #a02270;
  color: #fff;
  display: inline-block;
  margin-top: -25px;
  height: 40px;
  line-height: 40px;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 4px;
  z-index: 5;
}
#main-slider .prev:hover,
#main-slider .next:hover {
  background-color: #b98bae;
}
#main-slider .prev {
  left: 10px;
}
#main-slider .next {
  right: 10px;
}
#main-slider .carousel-indicators li {
  width: 20px;
  height: 20px;
  background-color: #fff;
  margin: 0 15px 0 0;
  position: relative;
}
#main-slider .carousel-indicators li:after {
  position: absolute;
  content: "";
  width: 24px;
  height: 24px;
  border-radius: 100%;
  background: rgba(0, 0, 0, 0);
  border: 1px solid #FFF;
  left: -3px;
  top: -3px;
}
#main-slider .carousel-indicators .active {
  width: 20px;
  height: 20px;
  background-color: #a02270;
  margin: 0 15px 0 0;
  border: 1px solid #a02270;
  position: relative;
}
#main-slider .carousel-indicators .active:after {
  position: absolute;
  content: "";
  width: 24px;
  height: 24px;
  border-radius: 100%;
  background: #ca02270;
  border: 1px solid #a02270;
  left: -3px;
  top: -3px;
}
#main-slider .active .animation.animated-item-1 {
  -webkit-animation: fadeInUp 300ms linear 300ms both;
  -moz-animation: fadeInUp 300ms linear 300ms both;
  -o-animation: fadeInUp 300ms linear 300ms both;
  -ms-animation: fadeInUp 300ms linear 300ms both;
  animation: fadeInUp 300ms linear 300ms both;
}
#main-slider .active .animation.animated-item-2 {
  -webkit-animation: fadeInUp 300ms linear 600ms both;
  -moz-animation: fadeInUp 300ms linear 600ms both;
  -o-animation: fadeInUp 300ms linear 600ms both;
  -ms-animation: fadeInUp 300ms linear 600ms both;
  animation: fadeInUp 300ms linear 600ms both;
}
#main-slider .active .animation.animated-item-3 {
  -webkit-animation: fadeInUp 300ms linear 900ms both;
  -moz-animation: fadeInUp 300ms linear 900ms both;
  -o-animation: fadeInUp 300ms linear 900ms both;
  -ms-animation: fadeInUp 300ms linear 900ms both;
  animation: fadeInUp 300ms linear 900ms both;
}
#main-slider .active .animation.animated-item-4 {
  -webkit-animation: fadeInUp 300ms linear 1200ms both;
  -moz-animation: fadeInUp 300ms linear 1200ms both;
  -o-animation: fadeInUp 300ms linear 1200ms both;
  -ms-animation: fadeInUp 300ms linear 1200ms both;
  animation: fadeInUp 300ms linear 1200ms both;
}
#reservation {
  background: #fff;
  padding: 30px 0;
  text-align: justify;
}
.reservation-nav {
  position: absolute;
  z-index: 15;
  bottom: 0px;
  width: 100%;
}
#res-content-2 {
  display: none;
}
#res-content-2,
#res-content-1 {
  text-align: center;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<section id="main-slider" class="no-margin">
  <div class="carousel slide">
    <div class="reservation-nav">
      <div class="container">
        <div class="row">
          <div class="col-md-4">
            <div class="btn-group btn-group-justified">
              <div class="btn-group">
                <button type="button" id="login_click" class="btn btn-default active btnvisit" data-toggle="button">Umów wizytę</button>
              </div>
              <div class="btn-group">
                <button type="button" id="register_click" class="btn btn-default btncall">Zadzwoń do nas</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="carousel-inner">
      <div class="item active" style="background-image: url(https://ununsplash.imgix.net/photo-1416339134316-0e91dc9ded92?q=75&fm=jpg&s=883a422e10fc4149893984019f63c818)" data-ride="carousel">
        <div class="container">
          <div class="row slide-margin">
            <div class="col-sm-6">
              <div class="carousel-content"></div>
            </div>
          </div>
        </div>
      </div>
      <!--/.item-->
      <div class="item" style="background-image: url(https://ununsplash.imgix.net/photo-1416339684178-3a239570f315?q=75&fm=jpg&s=c39d9a3bf66d6566b9608a9f1f3765af)" data-ride="carousel">
        <div class="container">
          <div class="row slide-margin">
            <div class="col-sm-6">
              <div class="carousel-content"></div>
            </div>
          </div>
        </div>
      </div>
      <!--/.item-->
      <div class="item" style="background-image: url(https://ununsplash.imgix.net/photo-1416339276121-ba1dfa199912?q=75&fm=jpg&s=9bf9f2ef5be5cb5eee5255e7765cb327)" data-ride="carousel">
        <div class="container">
          <div class="row slide-margin">
            <div class="col-sm-6">
              <div class="carousel-content"></div>
            </div>
          </div>
        </div>
      </div>
      <!--/.item-->
    </div>
    <!--/.carousel-inner-->
  </div>
  <!--/.carousel--> <a class="prev hidden-xs" href="#main-slider" data-slide="prev" onclick="$('#main-slider').carousel('prev')"><i class="fa fa-chevron-left"></i></a>
  <a class="next hidden-xs" href="#main-slider" data-slide="next" onclick="$('#main-slider').carousel('next')"><i class="fa fa-chevron-right"></i></a>

</section>
<!--/#main-slider-->
&#13;
&#13;
&#13;