admin管理员组文章数量:1291177
I'm trying to show and hide list elements (that I can't advice a individual class to) in a sequence, i.e. delayed.
this is my html…
<ul id="aclass">
<?php for ($i = 0; $i < count($enties); ++$i) :
<li class="animation">
<div id="frame">
</div>
</li>
<?php endfor; ?>
</ul>
so far I have
$(document).ready(function() {
function showpanel() {
$("ul#aclass > li").each(function() {
$(this).css("display", "none");
});
setTimeout(showpanel, 200)
});
I want to see the first li element for two seconds, then replaced but the second one for two seconds, then the next one etc. I don't know how to select the "next" li element and to run the function on each element successively.
Thanks for help.
I'm trying to show and hide list elements (that I can't advice a individual class to) in a sequence, i.e. delayed.
this is my html…
<ul id="aclass">
<?php for ($i = 0; $i < count($enties); ++$i) :
<li class="animation">
<div id="frame">
</div>
</li>
<?php endfor; ?>
</ul>
so far I have
$(document).ready(function() {
function showpanel() {
$("ul#aclass > li").each(function() {
$(this).css("display", "none");
});
setTimeout(showpanel, 200)
});
I want to see the first li element for two seconds, then replaced but the second one for two seconds, then the next one etc. I don't know how to select the "next" li element and to run the function on each element successively.
Thanks for help.
Share Improve this question asked Apr 26, 2015 at 15:25 tgifredtgifred 1091 gold badge2 silver badges8 bronze badges5 Answers
Reset to default 5Creative solution:
- CSS: Hide all panels except the first (with the help of
:first-child
) - jQuery: keep moving the first child to the end periodically.
$(document).ready(function() {
var panelInterval;
panelInterval = setInterval(function () {
$("ul#aclass > li:first-child").appendTo("ul#aclass");
}, 200)
});
ul#aclass > li {
display: none;
}
ul#aclass > li:first-child {
display: block;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="aclass">
<li>Panel 1</li>
<li>Panel 2</li>
<li>Panel 3</li>
<li>Panel 4</li>
<li>Panel 5</li>
</ul>
@tgifred In the end, I managed to find the working solution. It is like this. I hope it helps.
<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<ul id="aclass">
<li class="animate">Panel 1</li>
<li class="animate">Panel 2</li>
<li class="animate">Panel 3</li>
<li class="animate">Panel 4</li>
<li class="animate">Panel 5</li>
</ul>
<script>
$(function() {
var elements = $("ul#aclass li");
elements.hide();
elements.each(function (i) {
$(this).delay(2000* i++).fadeIn(2000);
});
});
</script>
</body>
</html>
In case you want to have visible only one element at a time, you could do something like this:
<script>
$(function() {
var elements = $("ul#aclass li");
elements.hide();
elements.each(function (i) {
$(this).delay(2000 * i++).fadeIn(20).fadeOut(1800);
});
});
</script>
And you can play with the easing.
Try using $.next().
$(document).ready(function() {
function showpanel($toShow, $toHide) {
if ($toHide) {
$toHide.hide();
}
$toShow.show();
setTimeout(showpanel, 200, $toShow.next(), $toShow)
});
showpanel($("ul#aclass > li").hide().first())
});
You could use a recursive function with a variable that increments for each run, something like
$(document).ready(function() {
(function showpanel(i, elems) {
i = i === elems.length-1 ? 0 : i+1;
elems.eq(i).show(0).delay(1000).hide(0, function() {
showpanel(i, elems);
});
})(-1, $("ul#aclass > li"));
});
FIDDLE
check it out:
function showpanel(current, elems) {
return function() {
current = current < elems.length - 1 ? current + 1 : 0;
elems.hide().eq(current).show();
}
};
setInterval(showpanel(0, $("#aclass li")), 200)
DEMO
本文标签: javascriptshow and hide list elements in a sequenceStack Overflow
版权声明:本文标题:javascript - show and hide list elements in a sequence - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741514388a2382795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论