问题
Based on this great image slider with prev/next buttons by adaneo, in this answer here: image gallery next and previous button
His JSFiddle is here, and it does what I need it to do, except that I need it to work on two separate galleries in the one page: http://jsfiddle.net/adeneo/5VEeH/3/
Here is my forked JSFiddle, which I haven't done much to except duplicate the mark-up: http://jsfiddle.net/cbp9T/1/
How could I re-write that so that I could use it twice in one page? Would I make the whole thing it's own function, killing the variable values at the end, and then duplicate it into a new function with new selectors for the 2nd gallery? Or?
Suggestions/links to JSFiddles that answer a problem like this extremely gratefully received, thanks.
回答1:
You'll need to create unique IDs for display, next, prev, and img-list as well as styling each display in CSS.
DEMO
(function($) {
$.fn.extend({
gallerify: function(o) {
return this.each(function() {
var $this = "#" + this.id;
var E = $("a", o.imgList), N = 0, T = E.length-1;
$($this).html('<img src="'+$(E[N]).attr('href')+'" />');
var btns = o.next.concat(",",o.prev);
$(btns).on('click', function() {
var A = this.id == (o.next).substr(1,o.next.length),X=A?T:0,Y=A?0:T,Z=A?N+1:N-1;N=N==X?Y:Z;
$($this).html('<img src="'+$(E[N]).attr('href')+'" />');
});
E.on('click', function(e) {
var S = $(this).attr('href');
$($this).html('<img src="'+S+'" />');
e.preventDefault();
});
});
}
});
})(jQuery);
$("#display").gallerify({imgList:'#img-list',next:'#next',prev:'#prev'});
$("#display2").gallerify({imgList:'#img-list2',next:'#next2',prev:'#prev2'});
来源:https://stackoverflow.com/questions/11800096/how-could-this-jquery-slider-with-prev-next-buttons-be-used-on-two-galleries-in