Skip to content

Instantly share code, notes, and snippets.

@lucemia
Created July 27, 2015 02:15
Show Gist options
  • Save lucemia/7ff1078659aa598fc8db to your computer and use it in GitHub Desktop.
Save lucemia/7ff1078659aa598fc8db to your computer and use it in GitHub Desktop.
var SlideShow = function(objs, option) {
/*
objs, the element to do the slide
*/
var index = 0;
var page_size = option.page_size;
var interval = option.interval || 1000;
var fade_time = option.fade_time || 300;
var timeout = option.timeout || 30000;
var worker = undefined;
var onChange = option.onChange || function() {};
var item_num = objs.length;
var do_slide_page = (item_num >= page_size);
function animate(offset) {
offset %= (page_size * item_num);
if (index < 0) {
index = page_size * item_num - index;
}
index += offset;
var start_index = parseInt(index / page_size) * page_size;
var end_index = start_index + page_size;
var item_page = parseInt(index / item_num);
// console.log('current index:' + index + ':' + start_index + ':' + end_index);
// may need to handle fade-in twice issues
objs.each(function(i, elem) {
start_index %= item_num;
end_index %= item_num;
if (start_index < end_index && (start_index <= i && i < end_index))
{
$(elem).fadeIn(fade_time);
} else if (end_index < start_index && (start_index <= i || i < end_index)) {
$(elem).fadeIn(fade_time);
} else {
$(elem).hide();
}
});
onChange(index);
}
// public
return {
prevPage: function() {
animate(-page_size);
},
nextPage: function() {
animate(page_size);
},
nextStep: function() {
animate(1);
},
start: function() {
if (typeof worker === "undefined") {
this.nextStep();
worker = setInterval(this.nextStep, interval);
}
setTimeout(this.stop, timeout);
},
stop: function() {
if (typeof worker !== "undefined") {
clearInterval(worker);
worker = undefined;
}
},
resume: function() {
if (typeof worker === "undefined") {
this.nextStep();
worker = setInterval(this.nextStep, interval);
}
},
onoff: function() {
if (typeof worker === "undefined") {
this.resume();
} else {
this.stop();
}
},
curIndex: function() {
return index % item_num;
}
}
};
module.exports = SlideShow;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment