Skip to content

Instantly share code, notes, and snippets.

@markupboy
Created April 11, 2011 13:08
Show Gist options
  • Save markupboy/913487 to your computer and use it in GitHub Desktop.
Save markupboy/913487 to your computer and use it in GitHub Desktop.
carousel
/* JS carousel instantiated as -
function newsTickerAnimation(activeID, stagedObj, callback) {
var activeObj = $(activeID);
activeObj.animate({
left: '-=100px',
opacity: 0
}, 250, function() {
$(this).css({
display: 'none',
left: 0,
opacity: 1
});
stagedObj.animate({
left: '+=100px',
opacity: 0
}, 0, function() {
$(this).css({
display: 'block'
}).animate({
left: 0,
opacity: 1
}, 250, function() {
if(typeof callback == "function") {
callback();
}
});
});
});
};
var newsTicker = new REED.carousel('.news-ticker', 'li', '', '.news-ticker .prev', '.news-ticker .next', newsTickerAnimation);
*/
REED.carousel = function(scope, slides, pagination, prev, next, animation, delay) {
var s = this;
s.scope = $(scope);
s.delay = delay || 5000;
s.animateSlides = typeof animation == "function" ? animation : function(activeID, stagedObj, callback) {
stagedObj.addClass('staged').css({
opacity: 0
}).show().animate({
opacity: 1
}, 250, 'swing', function() {
if(typeof(callback) == "function") {
callback();
}
});
};
s.showSlide = function(id, dir) {
var staged = getSlide(id);
if(!dir) {
dir = 1;
}
if(!staged.hasClass('active')) {
setActivePage(id);
s.animateSlides(s.active, staged, function() {
setActiveSlide(id);
});
}
};
function getSlide(o) {
if(typeof(o) === 'string' && o.indexOf('#') != 0)
var o = '#'+o;
return $(o, s.scope);
}
function getID(o) {
return "#" + getSlide(o).attr('id');
}
function getPage(o) {
// might be faster
// return $(pagination).find('a[href='+getID(o)+']').parent()
return $(pagination).find('a').filter(function() {
return $(this).attr('href') === getID(o);
}).parent();
}
function setActivePage(o) {
getPage(o)
.addClass('active')
.removeClass('staged')
.siblings('.active')
.removeClass('active');
}
function setActiveSlide(o) {
getSlide(o)
.addClass('active')
.removeClass('staged')
.siblings('.active')
.removeClass('active');
s.active = getID(o);
}
function isImg(src) {
return (/([^\s]+(?=\.(jpg|jpeg|gif|png|bmp))\.\2)/i).test(src);
};
function paginate(dir) {
var index = getSlide(s.active).prevAll().length + dir,
highIndex = $(slides, scope).length - 1;
if(index > highIndex) {
index = 0;
} else if(index < 0) {
index = highIndex;
}
s.showSlide($(slides, scope).eq(index), dir);
}
s.start = function() {
if(s.paused) {
s.paused = false;
s.interval = setInterval(function() {
paginate(1);
}, s.delay);
}
};
s.pause = function() {
if(!s.paused) {
s.paused = true;
clearInterval(s.interval);
}
};
function init() {
setActiveSlide(s.scope.find(slides).first());
setActivePage(s.scope.find(slides).first());
$(pagination).find('li').bind({
click: function(e) {
s.pause();
e.preventDefault();
s.showSlide($(this).find('a').attr('href'));
}
});
$(scope).hover(function() {
s.pause();
}, function() {
s.start();
});
$(prev).bind({
click: function() {
s.pause();
paginate(-1);
}
});
$(next).bind({
click: function() {
s.pause();
paginate(1);
}
});
s.paused = true;
s.start();
}
init();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment