Skip to content

Instantly share code, notes, and snippets.

@ZhangHang
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhangHang/b7cfa8044a4d8122e688 to your computer and use it in GitHub Desktop.
Save ZhangHang/b7cfa8044a4d8122e688 to your computer and use it in GitHub Desktop.
have fun with javascript
function slideshowFactory(slideNodes, config) {
var _config = config || {};
var nodeAppearAnimator = _config.appear || fadeIn;
var nodeDisappearAnimator = _config.disappear || fadeOut;
var normalDelayInMS = _config.normalDelay || 3000;
var switchTimes = 0;
var isFirstLoop = true;
var isCancel = true;
var switchSlideTimeoutId = null;
var cancelCondition = config.cancelCondition || function() {
return false;
}
function getItemBySwitchTimes(_switchTimes) {
return slideNodes[_switchTimes % slideNodes.length]
}
function getItemDelayBySwitchTime(_switchTimes) {
if (_config.firstLoopDelays && isFirstLoop) {
return _config.firstLoopDelays[switchTimes] * 1000;
}
return normalDelayInMS;
}
function switchSlide() {
if (!isCancel && !cancelCondition()) {
switchSlideTimeoutId = pageAction(function() {
switchSlide();
}, getItemDelayBySwitchTime(switchTimes))
} else {
console.log("canceled");
}
if (switchTimes > 0) {
nodeDisappearAnimator({
el: getItemBySwitchTimes(switchTimes - 1)
})
}
nodeAppearAnimator({
el: getItemBySwitchTimes(switchTimes)
})
switchTimes++;
if (switchTimes === slideNodes.length) {
isFirstLoop = false;
}
}
var control = {};
control.start = function() {
isCancel = false;
switchSlide();
}
control.stop = function() {
isCancel = true;
clearTimeout(switchSlideTimeoutId);
}
return control;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment