Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created August 22, 2011 21:07
Show Gist options
  • Save nathggns/1163577 to your computer and use it in GitHub Desktop.
Save nathggns/1163577 to your computer and use it in GitHub Desktop.
Ticker
(function($) {
$.fn.ticker = function(options) {
var settings = {
delay: 500,
speed: 600,
repeat: false,
callback: false
}
tick = function(id) {
speed = settings.speed;
ele = $('#' + id).get();
$(ele).css({
height: $(ele).height(),
width: $(ele).width()
});
child = $(ele).children().eq(0).get();
$(child).css({
position: 'absolute',
right: 0
}, speed).animate({
right: ($(ele).outerWidth() / 2) - $(child).width()
}).delay(settings.delay).animate({
right: $(ele).outerWidth()
}, {
speed: speed,
complete: function() {
if (settings.repeat) {
setTimeout(function() {
if (settings.callback) settings.callback();
if ($(child).hasClass('stopRepeat')) {
settings.repeat = false;
} else {
tick(id);
}
}, 500);
}
}
});
}
return $(this).each(function() {
if (options) {
$.extend(settings, options)
}
ele = this;
ticker = document.createElement('div');
ticker.id = 'ticker-' + Math.floor(Math.random() * 10000);
$(ticker).css({
position: 'relative',
overflow: 'hidden'
});
$(this).after(ticker);
$(this).appendTo(ticker);
tick(ticker.id);
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment