Jquery news ticker
A jQuery news ticker! Here's a simple example to get you started:
HTML
<div id="news-ticker">
<ul>
<li>News item 1</li>
<li>News item 2</li>
<li>News item 3</li>
<!-- Add more news items here -->
</ul>
</div>
CSS
#news-ticker {
position: relative;
width: 300px; /* adjust the width to your liking */
height: 30px; /* adjust the height to your liking */
overflow: hidden;
background-color: #f0f0f0;
border: 1px solid #ccc;
padding: 5px;
font-size: 14px;
font-family: Arial, sans-serif;
}
#news-ticker ul {
list-style: none;
margin: 0;
padding: 0;
}
#news-ticker li {
display: inline-block;
margin-right: 10px;
}
JavaScript (using jQuery)
$(document).ready(function() {
var newsTicker = $('#news-ticker');
var newsItems = newsTicker.find('li');
var newsItemWidth = newsItems.eq(0).width();
var newsItemCount = newsItems.length;
var newsTickerWidth = newsItems.width();
newsTicker.animate({
scrollLeft: newsItemWidth * newsItemCount
}, 30000, function() {
newsTicker.animate({
scrollLeft: 0
}, 0, function() {
// Reset the scroll position
newsTicker.scrollLeft(0);
});
});
});
This code creates a news ticker that scrolls through a list of news items. The news items are displayed horizontally, and the ticker scrolls at a rate of 30 seconds per cycle.
Here's how it works:
- We select the news ticker element (
#news-ticker
) and find all the news items (li
) inside it. - We calculate the width of each news item and the total width of all news items.
- We animate the news ticker to scroll to the end of the news items (i.e.,
newsItemWidth * newsItemCount
) over a period of 30 seconds. - When the animation completes, we reset the scroll position to the beginning of the news items.
You can adjust the animation speed, news item width, and other styles to customize the appearance and behavior of your news ticker.