Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created August 22, 2011 21:46
Show Gist options
  • Save nathggns/1163700 to your computer and use it in GitHub Desktop.
Save nathggns/1163700 to your computer and use it in GitHub Desktop.
A great ticker written by Nathaniel Higgins
(function($) {
$.fn.ticker = function(options) {
var settings = {
delay: 500,
speed: 600,
repeat: false,
callback: false
}
var t = 0;
tick = function(id) {
speed = settings.speed;
ele = $('#' + id).get();
child = $(ele).children().eq(0).get();
temp = document.createElement('span');
temp.innerHTML = $(child).html();
document.body.appendChild(temp);
innerWidth = $(temp).width();
$(temp).remove();
$(ele).css({
height: $(ele).height()
});
$(child).css({
width: innerWidth,
left: $(ele).outerWidth(),
position: 'absolute',
});
$(child)
.animate({
left: ($(ele).outerWidth() / 2) - ($(child).outerWidth() / 2)
})
.delay(settings.delay)
.animate({
left: '-' + ($(child).outerWidth())
}, {
complete: function() {
if (settings.callback) {
settings.callback.call(child, ++t);
}
if (settings.repeat) {
if ($(child).hasClass('stopRepeat')) {
settings.repeat = false;
} else {
setTimeout(function() {
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