Skip to content

Instantly share code, notes, and snippets.

@srph
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save srph/537833121203dba32ba9 to your computer and use it in GitHub Desktop.

Select an option

Save srph/537833121203dba32ba9 to your computer and use it in GitHub Desktop.
random thumb algo
(function(_, undefined) {
/** Defaults */
var defaults = {
duration: 10000,
registry: [],
alternate: 'Oops'
};
/**
* Modifies the background of the respective element
* @param element
* @return {[type]} [description]
*/
function modifyElementBG (element) {
element = document.querySelectorAll(element);
element.style.backgroundImage = this.current.thumbnail;
}
/**
* @desc Assigns a random value for the current transition
* @return {[type]} [description]
*/
function random() {
var index = _.random(, this.registry.length);
this.current = registry[index];
}
/**
* @usage
var swag = new ImageSwag(opts);
swag.init();
*/
function ImageSwag (element, options) {
this.element = element;
// Init status
this.status = false;
_.extend(this, defaults);
_.extend(this, options);
}
/**
* @desc Initializes the swag
* @return this Method chaining
*/
ImageSwag.prototype.init = function () {
if ( this.status ) {
var msg = "ImageSwag has been initialized for more than a single time.";
throw new Error(msg);
}
this.process();
// Set initialized status to true
if ( ! this.status ) {
this.status = true;
}
}
/**
* @desc Responsible for the whole process as it
* randomizes the value, modifies the element, and
* sets the process in infinite loop
* @see random
* @see pogi
* @return {[type]} [description]
*/
ImageSwag.prototype.process = function () {
random(this.random);
this.modifyElement();
this.timeout = setTimeout(this.process, this.duration);
}
/**
* @desc Calls appropriate modification of the element
* @see modifyElementBG
* @return {[type]} [description]
*/
ImageSwag.prototype.modifyElement = function() {
if ( ! this.element.length ) {
return;
}
if ( isArray(this.element) ) {
[].forEach.call(this.element, modifyElementBG);
} else {
modifyElementBG(this.element);
}
}
/**
* @desc Destroys the swag.
* @desc Removes the transition of shit
* @return {[type]} [description]
*/
ImageSwag.prototype.destroy = function() {
if ( this.timeout ) {
clearTimeout(this.timeout);
}
}
})(_);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment