Bootstrap news ticker
A Bootstrap news ticker! Here's a simple example to get you started:
HTML
<div id="news-ticker" class="ticker">
<ul>
<li>News item 1</li>
<li>News item 2</li>
<li>News item 3</li>
<li>News item 4</li>
<li>News item 5</li>
</ul>
</div>
CSS (using Bootstrap)
.ticker {
position: relative;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #f9f9f9;
}
.ticker ul {
list-style: none;
margin: 0;
padding: 0;
}
.ticker li {
display: inline-block;
margin-right: 10px;
}
.ticker li:last-child {
margin-right: 0;
}
JavaScript (using Bootstrap's JavaScript library)
$(document).ready(function() {
var ticker = $('#news-ticker');
var items = ticker.find('li');
var interval = setInterval(function() {
items.eq(0).animate({
'margin-left': '-100%'
}, 1000, function() {
$(this).css('margin-left', '0');
});
}, 5000); // animate every 5 seconds
});
Here's how it works:
- We create a
div
element with the classticker
and anul
element inside it. - We style the
ticker
element using Bootstrap's CSS classes to give it a basic layout and design. - We use JavaScript to animate the
li
elements inside theul
element. We use theanimate
method to move the firstli
element to the left by 100% of its width, and then reset its margin to 0. This creates a scrolling effect. - We use the
setInterval
function to call the animation function every 5 seconds.
You can customize the animation speed, interval, and design to fit your needs. You can also add more features, such as pausing the ticker on hover or adding a pause button.