HTML:
<ul class="slider">
<li>Hong Kong - Lorem ipsum dolor sit amet consectetur adipiscing...</li>
<li>Hong Kong - Lorem ipsum dolor sit amet consectetur adipiscing...</li>
<li>Hong Kong - Lorem ipsum dolor sit amet consectetur adipiscing...</li>
<li>Hong Kong - Lorem ipsum dolor sit amet consectetur adipiscing...</li>
<li>Hong Kong - Lorem ipsum dolor sit amet consectetur adipiscing...</li>
<li>Hong Kong - Lorem ipsum dolor sit amet consectetur adipiscing...</li>
</ul>
JavaScript:
$(document).ready(function() {
// Cache all list items
var $liCollection = $(".slider li");
// Cache the first list item for later use
var $firstListItem = $liCollection.first();
// Give the first list item the active state
$liCollection.first().addClass("active");
// Each 500 ms
setInterval(function() {
// Get the active list item
var $activeListItem = $(".active")
// Remove its active state
$activeListItem.removeClass("active");
// Try to find the next list item
var $nextListItem = $activeListItem.closest('li').next();
// If the next list item (jQuery object) length property is 0
// (this means that this list item was the last in the list)
if ($nextListItem.length == 0) {
// The next list item is actually the first list item
$nextListItem = $firstListItem;
}
$nextListItem.addClass("active");
}, 500);
});
CSS:
.active{
color:#9c0;
}
现场演示