javascript - 数据动画 Bootstrap 轮播效果不起作用

标签 javascript jquery html css twitter-bootstrap

您好,我已经在我的 slider 中实现了数据动画效果,但是在我的元素中实现后动画效果不起作用。它将所有图像显示为一张幻灯片, slider 也第一次没有移动。如果我们点击在下一个和上一个按钮上,如果不接受,则 slider 会自动移动。这是我的代码。在我的元素中实现时出现错误

未捕获的类型错误:$myCarousel.carousel 不是一个函数

  (function( $ ) {
//Function to animate slider captions 
function doAnimations( elems ) {
    //Cache the animationend event in a variable
    var animEndEv = 'webkitAnimationEnd animationend';

    elems.each(function () {
        var $this = $(this),
            $animationType = $this.data('animation');
        $this.addClass($animationType).one(animEndEv, function () {
            $this.removeClass($animationType);
        });
    });
}

//Variables on page load 
var $myCarousel = $('#carousel-example-generic'),
    $firstAnimatingElems = $myCarousel.find('.item:first').find("[data-animation ^= 'animated']");

//Initialize carousel 
$myCarousel.carousel();

//Animate captions in first slide on page load 
doAnimations($firstAnimatingElems);

//Pause carousel  
$myCarousel.carousel('pause');


//Other slides to be animated on carousel slide event 
$myCarousel.on('slide.bs.carousel', function (e) {
    var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");
    doAnimations($animatingElems);
});  
$('#carousel-example-generic').carousel({
    interval:3000,
    pause: "false"
});

 })(jQuery);    
#first-slider .main-container {
padding: 0;
}
#first-slider .slide1 h3, #first-slider .slide2 h3, #first-slider .slide3 h3, #first-slider .slide4 h3{
color: #fff;
font-size: 30px;
  text-transform: uppercase;
  font-weight:700;
}
#first-slider .slide1 h4,#first-slider .slide2 h4,#first-slider .slide3 h4,#first-slider .slide4 h4{
color: #fff;
font-size: 30px;
  text-transform: uppercase;
  font-weight:700;
 }
 #first-slider .slide1 .text-left ,#first-slider .slide3 .text-left{
padding-left: 40px;
}
#first-slider .carousel-indicators {
bottom: 0;
}
#first-slider .carousel-control.right,
#first-slider .carousel-control.left {
background-image: none;
}
#first-slider .carousel .item {
 min-height: 425px; 
height: 100%;
width:100%;
}
 .carousel-inner .item .container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
bottom: 0;
top: 0;
left: 0;
right: 0;
 }
  #first-slider h3{
  animation-delay: 1s;
  }
  #first-slider h4 {
  animation-delay: 2s;
  }
  #first-slider h2 {
  animation-delay: 3s;
  }
  #first-slider .carousel-control {
  width: 6%;
  text-shadow: none;
  }
  #first-slider h1 {
  text-align: center;  
  margin-bottom: 30px;
  font-size: 30px;
  font-weight: bold;
  }
  #first-slider .p {
  padding-top: 125px;
  text-align: center;
  }
  #first-slider .p a {
  text-decoration: underline;
  }
  #first-slider .carousel-indicators li {
  width: 14px;
  height: 14px;
  background-color: rgba(255,255,255,.4);
  border:none;
  }
  #first-slider .carousel-indicators .active{
  width: 16px;
  height: 16px;
  background-color: #fff;
  border:none;
  }
 .carousel-fade .carousel-inner .item {
 -webkit-transition-property: opacity;
  transition-property: opacity;
  }
 .carousel-fade .carousel-inner .item,
 .carousel-fade .carousel-inner .active.left,
 .carousel-fade .carousel-inner .active.right {
 opacity: 0;
}
  .carousel-fade .carousel-inner .active,
  .carousel-fade .carousel-inner .next.left,
  .carousel-fade .carousel-inner .prev.right {
  opacity: 1;
  }
 .carousel-fade .carousel-inner .next,
 .carousel-fade .carousel-inner .prev,
 .carousel-fade .carousel-inner .active.left,
 .carousel-fade .carousel-inner .active.right {
   left: 0;
  -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
   .carousel-fade .carousel-control {
  z-index: 2;
  }
 .carousel-control .fa-angle-right, .carousel-control .fa-angle-left {
  position: absolute;
  top: 50%;
  z-index: 5;
  display: inline-block;
  }
 .carousel-control .fa-angle-left{
 left: 50%;
width: 38px;
height: 38px;
margin-top: -15px;
font-size: 30px;
color: #fff;
border: 3px solid #ffffff;
-webkit-border-radius: 23px;
-moz-border-radius: 23px;
border-radius: 53px;
 }
.carousel-control .fa-angle-right{
right: 50%;
width: 38px;
height: 38px;
margin-top: -15px;
font-size: 30px;
color: #fff;
border: 3px solid #ffffff;
-webkit-border-radius: 23px;
-moz-border-radius: 23px;
border-radius: 53px;
}
.carousel-control {
opacity: 1;
filter: alpha(opacity=100);
}
#first-slider .slide1 {
background-image: url(http://s20.postimg.org/h50tgcuz1/image.jpg);
  background-size: cover;
background-repeat: no-repeat;
}
 #first-slider .slide2 {
  background-image: url(http://s20.postimg.org/uxf8bzlql/image.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  }
  #first-slider .slide3 {
  background-image: url(http://s20.postimg.org/el56m97f1/image.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  }
  #first-slider .slide4 {
  background-image: url(http://s20.postimg.org/66pjy66dp/image.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://cdn.bootcss.com/animate.css/3.5.1/animate.min.css">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />


<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.1/js/bootstrap.min.js"></script>
<div id="first-slider">
<div id="carousel-example-generic" class="carousel slide carousel-fade">
    <!-- Indicators -->
    <ol class="carousel-indicators">
        <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
        <li data-target="#carousel-example-generic" data-slide-to="1"></li>
        <li data-target="#carousel-example-generic" data-slide-to="2"></li>
        <li data-target="#carousel-example-generic" data-slide-to="3"></li>
    </ol>
    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <!-- Item 1 -->
        <div class="item active slide1">
            <div class="row"><div class="container">
                <div class="col-md-3 text-right">
                    <img style="max-width: 200px;"  data-animation="animated zoomInLeft" src="http://s20.postimg.org/pfmmo6qj1/window_domain.png">
                </div>
                <div class="col-md-9 text-left">
                    <h3 data-animation="animated bounceInDown">Add images, or even your logo!</h3>
                    <h4 data-animation="animated bounceInUp">Easily use stunning effects</h4>             
                 </div>
            </div></div>
         </div> 
        <!-- Item 2 -->
        <div class="item slide2">
            <div class="row"><div class="container">
                <div class="col-md-7 text-left">
                    <h3 data-animation="animated bounceInDown"> 50 animation options A beautiful</h3>
                    <h4 data-animation="animated bounceInUp">Create beautiful slideshows </h4>
                 </div>
                <div class="col-md-5 text-right">
                    <img style="max-width: 200px;"  data-animation="animated zoomInLeft" src="http://s20.postimg.org/sp11uneml/rack_server_unlock.png">
                </div>
            </div></div>
        </div>
        <!-- Item 3 -->
        <div class="item slide3">
            <div class="row"><div class="container">
                <div class="col-md-7 text-left">
                    <h3 data-animation="animated bounceInDown">Simple Bootstrap Carousel</h3>
                    <h4 data-animation="animated bounceInUp">Bootstrap Image Carousel Slider with Animate.css</h4>
                 </div>
                <div class="col-md-5 text-right">
                    <img style="max-width: 200px;"  data-animation="animated zoomInLeft" src="http://s20.postimg.org/eq8xvxeq5/globe_network.png">
                </div>     
            </div></div>
        </div>
        <!-- Item 4 -->
        <div class="item slide4">
            <div class="row"><div class="container">
                <div class="col-md-7 text-left">
                    <h3 data-animation="animated bounceInDown">We are creative</h3>
                    <h4 data-animation="animated bounceInUp">Get start your next awesome project</h4>
                 </div>
                <div class="col-md-5 text-right">
                    <img style="max-width: 200px;"  data-animation="animated zoomInLeft" src="http://s20.postimg.org/9vf8xngel/internet_speed.png">
                </div>  
            </div></div>
        </div>
        <!-- End Item 4 -->

    </div>
    <!-- End Wrapper for slides-->
    <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
        <i class="fa fa-angle-left"></i><span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
        <i class="fa fa-angle-right"></i><span class="sr-only">Next</span>
    </a>
</div>

最佳答案

尝试在 html 之后包含脚本,

<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
        <i class="fa fa-angle-left"></i><span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
        <i class="fa fa-angle-right"></i><span class="sr-only">Next</span>
    </a>
</div>
<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.1/js/bootstrap.min.js"></script>
<script src="path/to/your/custom.js"></script>

关于javascript - 数据动画 Bootstrap 轮播效果不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43206798/

相关文章:

javascript - 离开网站时弹出窗口

javascript - 如何获取调用我的函数的元素的 ID、类或 HTML 标记?

php - 如何将 php 数组从 mysql 返回到 google geolocation

javascript - 从 json 获取值到 angularjs 对象

javascript - 丢失的 ;声明前

javascript - 字符串 str 始终为空,尽管它充满了来自 .geojson 文件的数据,你知道吗?

javascript - 如何使用 jquery 选择器找到某个类的表

javascript - 如何使用 javascript/jquery 获取数组中选中和未选中的复选框值

javascript - 移除类中定义的 EventListener

javascript - AngularJS和Firebase异步加载数据问题