2

html

<ul class="slider">
   <li>Lipsum</li>
    <li>lipsum</li>
    <li>lipsum</li>
     <li>lipsum</li>
</ul>

css

.active{
    color:#9c0;
}

javascript (jquery)

$(document).ready(function(){
        $(".slider li:first-child").addClass("active");
        $("li").click(function () {
         $(".active").removeClass("active");
         $(this).addClass("active");

    });

});

在页面上加载第一个 li 激活类,单击每个 li 它的激活类并删除其他活动类。我希望班级自动更改间隔,例如幻灯片,我该如何实现。

http://jsfiddle.net/awkTU/

4

4 回答 4

3

这有帮助吗:

$(document).ready(function(){
        $(".slider li:first-child").addClass("active");
        setTimeout(autoAddClass, 1000);
});

function autoAddClass(){
    var next = $(".active").removeClass("active").next();
    if(next.length)
        $(next).addClass('active');
    else
        $('.slider li:first-child').addClass('active');
    setTimeout(autoAddClass, 1000);
}
于 2012-12-09T07:19:46.280 回答
2

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;
}

现场演示

于 2012-12-09T07:16:41.677 回答
0

$(document).ready(function(){
    setInterval(function(){
if($("#last").hasClass('active'))
{
$("#last").removeClass('active');
$("#first").addClass('active');
}
$("li.active").next().addClass("active").prev().removeClass("active");
},2000);  
});
.active {
    color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<body>
<ul>
<li id="first" style="display:none"></li>
<li  class="active">1111</li>
<li>2222</li>
<li>3333</li>
<li id="last" >4444</li>
</ul>
<br>

<lable style="border:1px solid black;border-radius:5px;background-color:gray">class cycling through li elements</lable>

</body>

于 2016-11-02T10:22:57.793 回答
0

只需添加一个计数器并使用:eq()选择器

var $item= $(".item");
$item.first().addClass("active");
var count = 0;
setInterval(function() {
    $(".item:eq("+count+")").removeClass("active");
    if (count < 2) {
        count++
    }else {
        count = 0
    }
    $(".item:eq("+count+")").addClass("active");
}, 1000);
于 2018-04-26T05:44:51.637 回答