Owl Carousel 2 - caption div (img title & alt tags)

一世执手 提交于 2020-01-01 16:23:37

问题


I'm looking for a way to display the img title & alt tags in a div (.image-caption).

This is my code so far:

    owl.on('changed.owl.carousel', function(event) {

        var comment = $(this).find('img').attr('alt');
        var title = $(this).find('img').attr('title');
        if(comment) $('#desktop .image-caption').html('<h4>'+title+'</h4><p>'+comment+'</p>');

    })      

Any ideas? Thanks!


回答1:


You can use translated.owl.carousel event for that

Here is a working Fiddle

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

UPDATE:

Slightly improved code and added functionality to update image caption on carousel load.

FIDDLE




回答2:


this works:

var comment = $(this).find('.active').find('img').attr('alt');
var title = $(this).find('.active').find('img').attr('title');
if(comment) $('.image-caption').html('<h4>'+title+'</h4><p>'+comment+'</p>');

try it: https://jsfiddle.net/wx0ovpzh/49/



来源:https://stackoverflow.com/questions/29751878/owl-carousel-2-caption-div-img-title-alt-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!