好吧,这是一种方法(而且,尽管花了一些时间尝试,但似乎并不是一种明显更容易的方法):
/* Nivo seems to add images in order to implement its effects,
so we need to grab a reference to the original images, or, at least, their `src` */
var images = $('#slider img').map(
function() {
return $(this).attr('src');
});
$('#slider').nivoSlider({
beforeChange: function() {
var wrap = $('#slider'),
// caching data because I'm accessing it twice:
data = wrap.data('nivo:vars'),
// the current src:
current = data.currentImage.attr('src'),
len = data.totalSlides,
/* compares the current src against the srcs stored in the
images variable, and returns the index at which it was
found */
arrayPos = $.inArray(current, images),
nextSrc;
if (arrayPos == (len - 1)) {
/* if the array position of the src is equal to length-1
(zero-based arrays in JS), then the next image to be
shown will be the first in the array */
nextSrc = images[0];
}
else {
// otherwise get the next src from the next position in the array
nextSrc = images[arrayPos + 1];
}
$('#output code.src').text(nextSrc);
}
});
JS 小提琴演示。
参考: