2013-01-08 32 views
2

使用以下代码,我会获得“#featured-carousel”的内容淡入和淡出幻灯片2,3等。
但是,不适用于第一张幻灯片
即使第一次幻灯片第一次淡入淡出,我该如何获得?淡入淡出Twitter中每张幻灯片的内容Bootstrap Carousel

<script type="text/javascript"> 
     jQuery(document).ready(function($) { 

      var color = "<?=$color?>"; 
      var header_color = "<?=$header_color?>";   

      $('#featured-carousel').bind('slide', function() { 
       $(".carousel-caption").animate({'opacity': 0}, 500).delay(2000); 
       $(".item img").animate({'opacity': 1}, 500).delay(2000);   
       $(".carousel-caption").animate({'opacity': 1}, 500).delay(2000); 
       $(".item img").animate({'opacity': 0.2}, 500).delay(2000);     
      }); 

      $('#featured-carousel').carousel({ 
        interval: 6000, 
         cycle: true,   
      }); 

     }); 
    </script> 

回答

5

对不起,我无法应付您的延误,所以我用超时代替。这样,当传送带手动导航时,动画将被取消。

Demo (jsfiddle)

首先,一些CSS做的第一个项目看起来像开头动画,不过这可以通过一些JS来完成,如果你喜欢:

#myCarousel .carousel-caption { 
    opacity: 0; 
    filter: alpha(opacity=0); 
} 

然后就是动画部分,分为两个绑定:当我们开始动画和当我们需要重置外观时:

var $carousel = $('#myCarousel'); 
var $carouselCaptions = $carousel.find('.item .carousel-caption'); 
var $carouselImages = $carousel.find('.item img'); 
var carouselTimeout; 

$carousel.on('slid', function() { 
    var $item = $carousel.find('.item.active'); 
    carouselTimeout = setTimeout(function() { // start the delay 
     carouselTimeout = false; 
     $item.find('.carousel-caption').animate({'opacity': 1}, 500); 
     $item.find('img').animate({'opacity': 0.2}, 500); 
    }, 2000); 
}).on('slide', function() { 
    if(carouselTimeout) { // Carousel is sliding, stop pending animation if any 
     clearTimeout(carouselTimeout); 
     carouselTimeout = false; 
    } 
    // Reset styles 
    $carouselCaptions.animate({'opacity': 0}, 500); 
    $carouselImages.animate({'opacity': 1}, 500); 
});; 

$carousel.carousel({ 
    interval: 6000, 
    cycle: true, 
}).trigger('slid'); // Make the carousel believe that it has just been slid so that the first item gets the animation 
+0

谢谢你,谢尔布罗。你的代码完全按照我的意愿去做! – Kristoffer

+0

谢谢..真的。您可以在http://demo.theme.firmasite.com/上查看修改后的版本 –