问题
I am using the bxslider. It works very well for me, but I have 1 problem. I want the captions outside of the bxwrapper.
The html of the slider looks like this only the div captions is created by me
<div class="captions"></div>
<ul class="bxslider">
<li><img src="images/1.jpg" title="headline, text" /></li>
<li><img src="images/1.jpg" /></li>
<li><img src="images/1.jpg" title="text" /></li>
<li><img src="images/1.jpg" title="text" /></li>
</ul>
The captions are generated out of the title from the images with this code:
* Appends image captions to the DOM
*/
var appendCaptions = function(){
// cycle through each child
slider.children.each(function(index){
// get the image title attribute
var title = $(this).find('img:first').attr('title');
// append the caption
if (title != undefined && ('' + title).length) {
$(this).append('<div class="bxcaptions"><span>' + title + '</span></div>');
}
});
}
i tried to style the captions that they are outside of the slider, but the slider uses overflow hidden and so its impossible.
So i want that the captions are written into the <div class="captions"></div>
which i can position the way i want it.
Further i maybe want that the headline before the , is highlighted with h1 and the rest is normal but thats just additional.
Thanks for your help!
回答1:
Here the jsbin: http://jsbin.com/ikokex/3
jquery:
var captionText = '';
$('ul.bxslider > li > img').each(function(i,e) {
var titleAttr = $(this).attr('title');
if(typeof titleAttr != 'undefined') {
var captionSplit = titleAttr.split(',');
$('.captions').append('<div class="caption-'+i+'"></div>');
if (captionSplit.length > 1) {
captionText += '<h1>'+captionSplit[0]+'</h1>';
captionText += '<span>'+captionSplit[1]+'</span>';
} else {
captionText += '<span>'+captionSplit[0]+'</span>';
}
$('.caption-'+i).html(captionText);
captionText = '';
}
});
回答2:
i think i have a solution but its very dirty! i insert this code $('.captions').html(slider.children.eq(slider.active.index).find('img:first').attr('title'));
at the end of the slide function in the jquery.bxslider.js line 1188.
if somebody has a better solution please let me know!
回答3:
I think you want like this. below snippet is better solution for bx slider caption outside
jQuery('.bxslider').bxSlider({
nextSelector: '#slider-next',
prevSelector: '#slider-prev',
nextText: 'next',
prevText: 'prev',
pager: true,
prependtxt: 'Examples ',
pagerType: 'short',
mode: 'fade',
auto: true,
onSliderLoad: function(currentIndex) {
$(".slider-txt").html($('.bxslider li').eq(currentIndex).find("img").attr("title"));
},
onSlideBefore: function($slideElement, oldIndex, newIndex) {
$(".slider-txt").html($slideElement.find("img").attr("title"));
}
});
.slider-txt{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://bxslider.com/lib/jquery.bxslider.css" rel="stylesheet" />
<script src="http://bxslider.com/lib/jquery.bxslider.js"></script>
<div class="image-slider">
<ul class="bxslider">
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Letterhead" title="Letterhead" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Business Card" title="Business Card" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Compliment Slip" title="Compliment Slip" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Envelope" title="Envelope" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Folder" title="Folder" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Referral Card" title="Referral Card" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Single Sided Referral Pad" title="Single Sided Referral Pad" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="Post, note or thank you card" title="Post, note or thank you card" />
</li>
<li>
<img src="http://www.menucool.com/slider/prod/image-slider-2.jpg" alt="WEBSITE (OPTIONAL)" title="WEBSITE (OPTIONAL)" />
</li>
</ul>
<div class="outside"><span id="slider-prev"></span><span id="slider-next"></span>
</div>
<div class="slider-txt"></div>
</div>
来源:https://stackoverflow.com/questions/17489553/captions-in-own-div-outside-of-slider