There are a lot of different jQuery News Ticker plugins with lot of options that you can use. Do you want to learn how to create one on your own in only 4 lines of jQuery code ?
Idea is pretty simple, take first element from list, apply some disappearing effect on it and on callback attach it to the end of the list.
First we have to create HTML with list of our news, something like this:
<ul id="ticker"> <li> jqBarGraph is jQuery plugin that gives you freedom to easily display your data as graphs. There are three types of graphs: simple, multi and stacked. </li> <li> Learn how to create image gallery in 4 lines of Jquery </li> <li> jqFancyTransitions is easy-to-use jQuery plugin for displaying your photos as slideshow with fancy transition effects. </li> <li> mooBarGraph is AJAX graph plugin for MooTools which support two types of graphs, simple bar and stacked bar graph. </li> </ul>
Now, let’s put some style on this. We will set height of our visible ticker area and set overflow to hidden. If we want only one news to be visible at the time than ticker height should be the same as height of each element of the list. For more visible news at same time we just have to multiply height of each element with number of news that we want to be visible and set that as height of ticker.
#ticker { height: 40px; overflow: hidden; } #ticker li { height: 40px; }
Of course, you can and should add more style here, but this will be enough to make your ticker to work.
Ok, we have layout set and now it’s time for make this news to start ticking. First we will create function which will apply disappearing effect on first element, append that element to the end of the list after it disappear and revert effects changes. After that, all we have to do is to call that function in desired intervals.
And here is plain code:
function tick(){ $('#ticker li:first').slideUp( function () { $(this).appendTo($('#ticker')).slideDown(); }); } setInterval(function(){ tick () }, 5000);
Of course, you can change slideUp with any other animation that you like, just don’t forget to revert effects on element after transition. Next example will set opacity to 0 for first element.
function tick(){ $('#ticker li:first').animate({'opacity':0}, 200, function () { $(this).appendTo($('#ticker')).css('opacity', 1); }); } setInterval(function(){ tick () }, 4000);
And please don’t forget to include jQuery at the head of your document
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
That’s it, news ticker is done. Take a look at the demo, download source code or leave a comment.
I see the global.css but mt pages template is overriding the global.css file
Hi, this might be a very simple fix but is it possible to add font tags in the code somewhere?
thanks for share helpful nice tutorial
why it is not working in IE10
please help me
Great JOb,
Very, very good.
Nice..very short and effective code and representation its effective..sure going to use in my site..Thanks
Great Article
who use onmuseover = stop tick slide ?
how to get the author name also included (example 4)
Absolutely greate
Great plugin and explanations
great . very helpful . thanks
thank you…
can you suggest me on how to pause on mouse over..
I like simple scripts. People do the same thing with 200 lines of code.
I made pause hover effect like this:
function tick(){
jQuery(‘#ticker li:first’).slideUp(
function () { jQuery(this).appendTo(jQuery(‘#ticker’)).slideDown(); });
}
var t = setInterval(tick, 2000);
function mOver(obj)
{
clearInterval(t)
}
function mOut(obj)
{
t = setInterval(tick, 2000);
}
Instead of tag add
great work! does anyone know kow to animate the ticker (not only slide up the new layer) like the promo ticker at the bottom on this site? thanks again for the highly respected work.
@Manolof, can you show me an example of a working pause solution? I tried your post with no luck š
A pause solution that actually works!
jQuery(document).ready(function($) {
function tick(){
$(‘#ticker li:first’).slideUp( function () { $(this).appendTo($(‘#ticker’)).slideDown(); });
}
$(‘#ticker’).mouseover(function() {
window.clearInterval(intervalID);
}).mouseout(function(){
intervalID = window.setInterval(function(){tick()}, 1000);
})
var intervalID = window.setInterval(function(){tick()}, 1000);
});
Exactly what I was looking for! You just saved me some work, thanks! š
Great tutorial. Thank you very much
it’s work, very thanks u.
Sorry I got it in the end. Just a case of understanding and rereading the article.. which is quite clear!!! I was able to achieve my goal by setting the opacity of the LI in the CSS and then altering the opacity value before fading out and appending.
This is great but can you tell me how the ticker is able to animate through the list and then restart at the first item again. I must be missing something.
$(‘#ticker_03 li:first’).animate({‘opacity’:0}, 2000, function () { $(this).appendTo($(‘#ticker_03’)).css(‘opacity’, 1); });
Ideally I need to work out a way to animate the opactity back in slowly with another animation transition.
Thank you very much, i really love this script, its very simple easy and easy to modify it.
Thank you so much to this script.
Regards
Ghullam Murtaza Mallah
hi,
could this ticker also be hooked up with a mysql database to display the latest entries?
This is a great little piece of code. Thank you, thank you, thank you!
I had been messing around with three other jQuery tickers with tons of code, and they all basically worked, but not just quite right. Then I gave up and went searching for alternatives, and found this amazing gem. It never ceases to amaze me what jQuery is capable of!
Any, here’s a quick code snippet to enable pause on mouseover/mouseout, and in addition, it changes the cursor during the pause. Remove the original “setInterval…” statement in the source:
$(‘#ticker’).hover(function() {
$(this).css(‘cursor’,’progress’);
clearInterval(tickInterval);
},function() {
$(this).css(‘cursor’,’pointer’);
startTimer();
});
function startTimer(){
tickInterval = setInterval(function(){tick()}, 5000);
}
startTimer();
Also, I’m doing a top-down/multiple item ticker, with a fade-in:
$(‘#ticker-list li:last’).hide();
$(‘#ticker-list li:last’).prependTo($(‘#ticker-list’));
$(‘#ticker-list li:first’).slideDown(500).css(‘opacity’, .1);
$(‘#ticker-list li:first’).animate({‘opacity’:1}, 1000);
And also loading the ticker dynamically with AJAX:
$.get(‘feed.php’, function(data) {
$(“#ticker-list”).html(data);
});
Where “feed.php” returns the records. I can also dynamically reload the list and the ticker just keeps on going.