Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created August 22, 2011 22:18
Show Gist options
  • Save nathggns/1163786 to your computer and use it in GitHub Desktop.
Save nathggns/1163786 to your computer and use it in GitHub Desktop.
Full Source code for Harley McKittrick
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>jQuery News Ticker</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
(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);
$(document).ready(function() {
$('p').text('Random News Message 1');
$('p').ticker({
repeat: true,
callback: function(tick) {
// tick is the amount of times it has scrolled already. Without repeat: true will always be 1
$(this).text('Random News Message ' + ++tick);
}
})
});
</script>
</head>
<body>
<p>Temp</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment