Last active
August 29, 2015 14:11
-
-
Save ZhangHang/b7cfa8044a4d8122e688 to your computer and use it in GitHub Desktop.
have fun with javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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