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

2019-04-11 15:01发布

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!

2条回答
▲ chillily
2楼-- · 2019-04-11 15:32

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-11 15:35

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/

查看更多
登录 后发表回答