我想弄清楚如何在達到列表的最後一項後重新啓動循環。基本上它每3秒就通過active
到下一個兄弟姐妹。一旦到達最後一個項目,我如何才能將它傳遞給列表中的第一個項目。使用setTimeout在無限循環中創建
我想也許如果我能有使用if/else
代替for
環在我的計劃
$(document).ready(function() {
\t function durationSlider() {
\t var listItems = $('.loop ul li').length;
\t \t for(count=0; count <= listItems - 1; count++) {
(function(count) {
setTimeout(function() {
\t \t \t \t \t $('.loop ul li.active').removeClass('active');
\t \t \t \t \t $('.loop ul li:eq(' + count + ')').addClass('active'); \t
\t \t \t \t \t console.log(count);
}, 3000 * count);
}(count));
\t \t } \t \t
\t }
\t durationSlider();
})
\t
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
\t margin: 0;
\t padding: 0;
\t border: 0;
\t font-size: 100%;
\t font: inherit;
\t vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
\t display: block;
}
body {
\t line-height: 1;
}
ol, ul {
\t list-style: none;
}
blockquote, q {
\t quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
\t content: '';
\t content: none;
}
table {
\t border-collapse: collapse;
\t border-spacing: 0;
}
.loop ul li {
\t color : green;
\t -webkit-transition: color 1s linear;
}
.loop ul li.active {
\t -webkit-transition: color 1s linear; \t
color : red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="loop">
<ul>
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
如果你試圖與'setTimeout'你可能想OT使用一個無限循環'setInterval'代替 –