Skip to content

Instantly share code, notes, and snippets.

@tomedme
Created April 18, 2012 01:50
Show Gist options
  • Select an option

  • Save tomedme/2410492 to your computer and use it in GitHub Desktop.

Select an option

Save tomedme/2410492 to your computer and use it in GitHub Desktop.
jquery.pod1.fade.js
// $Id: jquery.pod1.fade.js 1206 2011-01-14 09:00:53Z tedme $
/**
* Pod1Fade
* Tom Edme & Hannah Stothard
* Nov 2010.
*/
(function ($) {
$.pod1 = $.pod1 || { version: '1.0' };
$.pod1.fade = {
conf: {
initDelay: 4000,
interval: 5000,
fadeSpeed: 2000,
imageId: '#image_wrap',
numbersClass: '.items2'
}
};
function Pod1Fade(root, conf) {
var self = this,
nodes = [],
current = 0,
timer; // timer is still "undefined"
$.extend(self, {
init: function () {
if ( $(conf.numbersClass).find('img').length < 2 ) { return; }
$(conf.numbersClass).find('img').each(function (i, el) {
var det = $(el).attr('alt').split('|', 2);
nodes[i] = { node: el, img: det[0], url: det[1] };
$(el).click(function () { self.seek(i); });
});
$(conf.imageId).hover(function () { self.pause(); }, function () { self.fade(); });
setTimeout(function () { self.fade(); }, conf.initDelay); // go go go!
},
pause: function () {
timer = clearInterval(timer);
},
fade: function () {
if (timer) { return; }
timer = setInterval(function() {
self.next();
}, conf.interval);
},
next: function () {
if (nodes.length == (current + 1) || nodes.length <= current || current < 0) {
self.seek(0); // back to the beginning
}
else {
self.seek(current + 1);
}
},
seek: function (i) {
if (current == i) { return; }
$(nodes[current].node).toggleClass('active');
var elca = $(conf.imageId).children();
var eln = new Image();
$(eln)
.load(function () {
$(elca).each(function (idx, el) {
if ('A' == el.nodeName) {
$(el).find('img').fadeOut(conf.fadeSpeed, function () {
$(el).remove();
});
}
if ('IMG' == el.nodeName) {
$(el).fadeOut(conf.fadeSpeed, function () {
$(el).remove();
});
}
});
if ('' != nodes[i].url) {
var a = document.createElement('a');
a.href = nodes[i].url;
$(eln).wrap(a);
}
})
.attr('src', nodes[i].img)
.prependTo(conf.imageId);
current = i;
$(nodes[current].node).toggleClass('active');
}
});
};
$.fn.pod1fade = function(conf) {
var f = this.data('pod1fade');
if (f) { return f; }
conf = $.extend({}, $.pod1.fade.conf, conf);
this.each(function() {
f = new Pod1Fade($(this), conf);
$(this).data('pod1fade', f);
f.init();
});
// return for chainability? maybe later
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment