Created
August 22, 2011 22:34
-
-
Save nathggns/1163826 to your computer and use it in GitHub Desktop.
Meh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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() { | |
messages = new Array("Message 1", "Message 2", "Message 3"); // Put your messages in here, each on in a new entry | |
count = messages.length; // Length of the messages array | |
curr = 1; // Index of the current message. 1 because the first message is added manually | |
$('p').text(messages[0]).ticker({ | |
repeat: true, | |
callback: function(tick) { | |
// tick is the amount of times it has scrolled already. Without repeat: true will always be 1 | |
if (curr == count) { | |
curr = 0; // Reset if it gets to the end | |
} | |
$(this).text(messages[curr++]); | |
} | |
}) | |
}); | |
</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