2016-07-16 75 views
1

我正在使用自己的傳送帶,因此要添加標題我遵循堆棧溢出this question但它不適用於我。然後我檢查了使用檢查元素,發現他們在我的輪播當前圖像上沒有「主動」類。所以我添加了腳本來這樣做。最後我的腳本看起來像這樣如何爲貓頭鷹傳送帶中的圖像添加標題

$(document).ready(function() { 
    $('.owl-carousel').owlCarousel({ 
     loop: true, 
     items: 1, 
     afterAction: function(el) { 
      //remove class active 
      this 
       .$owlItems 
       .removeClass('active') 

      //add class active 
      this 
       .$owlItems 
       .eq(this.currentItem) 
       .addClass('active') 
     }, 

     onInitialized: function() { 
      var activeImg = $('.owl-carousel').find('.active').find('img'); 
      var comment = activeImg.attr('alt'); 
      var title = activeImg.attr('title'); 
      if (comment) $('.image-caption').html('<h4>' + title + '</h4><p>' + comment + '</p>'); 
     } 
    }); 

    owl = $('.owl-carousel').owlCarousel(); 
    $('.prev').click(function() { 
     owl.trigger('prev.owl.carousel'); 
    }); 
    $('.next').click(function() { 
     owl.trigger('next.owl.carousel'); 
    }); 

    owl.on('translated.owl.carousel', function(event) { 
     var activeImg = $(this).find('.active').find('img'); 
     var comment = activeImg.attr('alt'); 
     var title = activeImg.attr('title'); 
     if (comment) $('.image-caption').html('<h4>' + title + '</h4><p>' + comment + '</p>'); 
    }); 
}); 

該腳本無法正常工作。

回答

0

在此解決方案中,我使用HTML figcaption元素表示與HTML figure關聯的標題或圖例。

我也有招回調後添加所有需要的邏輯在OWL CarouselafterMove

$('.owl-carousel').owlCarousel({ 
 
    loop: true, 
 
    items: 1, 
 
    navigation: true, 
 
    pagination: true, 
 
    lazyLoad: true, 
 
    singleItem: true, 
 
    afterMove: function(elem) { 
 
     var current = this.currentItem; 
 
     var currentImg = elem.find('.owl-item').eq(current).find('img'); 
 

 
     $('figure') 
 
      .find('img') 
 
      .attr({ 
 
       'src': currentImg.attr('src'), 
 
       'alt': currentImg.attr('alt'), 
 
       'title': currentImg.attr('title') 
 
      }); 
 
     $('#figcaption').text(currentImg.attr('title')); 
 
    } 
 
});
.owl-carousel .owl-item img { 
 
    display: block; 
 
    width:80%; 
 
    height:100px; 
 
}
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css" /> 
 
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css" /> 
 
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.min.css" /> 
 

 
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script> 
 

 
<figure id="currentImageWithCaption"> 
 
    <img src="https://malsup.github.io/images/p1.jpg" title="Title 1" alt="Title 1" width="50" height="30"> 
 
    <figcaption id="figcaption">Title 1</figcaption> 
 
</figure> 
 

 
<div class="owl-carousel"> 
 
    <div class="item"> 
 
     <img src="https://malsup.github.io/images/p1.jpg" title="Title 1" alt="Alt 1" /> 
 
    </div> 
 
    <div class="item"> 
 
     <img src="https://malsup.github.io/images/p2.jpg" title="Title 2" alt="Alt 2" /> 
 
    </div> 
 
    <div class="item"> 
 
     <img src="https://malsup.github.io/images/p3.jpg" title="Title 3" alt="Alt 3" /> 
 
    </div> 
 
    <div class="item"> 
 
     <img src="https://malsup.github.io/images/p4.jpg" title="Title 4" alt="Alt 4" /> 
 
    </div> 
 
</div>

+0

我們不需要

...
。不用顯示另一個圖像,只需以這種方式將圖像標題設置爲
我們只能顯示當前圖像的標題。 –

+0

您應該閱讀「HTML

」:https://developer.mozilla.org/en/docs/Web/HTML/Element/figure但現在您可以根據自己的喜好來配置您的腳本解決方案 –

0

@YosvelQuintero張貼的腳本,但不知道爲什麼,他刪除了它。所以我再次發佈它,以防有人需要它。

<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css" /> 
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css" /> 
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.min.css" /> 

<figure id="currentImageWithCaption"> 
    <img src="https://malsup.github.io/images/p1.jpg" title="Title 1" alt="Title 1" width="200" height="150"> 
    <figcaption id="figcaption">Title 1</figcaption> 
</figure> 

<div class="owl-carousel"> 
    <div class="item"> 
     <img src="https://malsup.github.io/images/p1.jpg" title="Title 1" alt="Alt 1" /> 
    </div> 
    <div class="item"> 
     <img src="https://malsup.github.io/images/p2.jpg" title="Title 2" alt="Alt 2" /> 
    </div> 
    <div class="item"> 
     <img src="https://malsup.github.io/images/p3.jpg" title="Title 3" alt="Alt 3" /> 
    </div> 
    <div class="item"> 
     <img src="https://malsup.github.io/images/p4.jpg" title="Title 4" alt="Alt 4" /> 
    </div> 
</div> 

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script> 

<style> 
figure img { 
display: block; 
width:100%; 
height: auto; 
} 
.owl-wrapper-outer{ 
    display : none; 
} 
</style> 

<script> 
    $('.owl-carousel').owlCarousel({ 
     loop: true, 
     items: 1, 
     navigation: true, 
     pagination: true, 
     lazyLoad: true, 
     afterMove: function(elem) { 
      var current = this.currentItem; 
      var currentImg = elem.find('.owl-item').eq(current).find('img'); 

      $('figure') 
       .find('img') 
       .attr('src', currentImg.attr('src')) 
       .attr('alt', currentImg.attr('alt')) 
       .attr('title', currentImg.attr('title')); 
      $('#figcaption').text(currentImg.attr('title')); 
     } 
    }); 
</script> 
+0

更新了我的回覆 –

相關問題