[英]show and hide list elements in a sequence
I'm trying to show and hide list elements (that I can't advice a individual class to) in a sequence, ie delayed. 我试图在序列中显示和隐藏列表元素(我无法建议单个类),即延迟。
this is my html… 这是我的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. 我希望看到第一个li元素两秒,然后替换但第二个元素两秒钟,然后下一个等等。我不知道如何选择“下一个”li元素并在每个元素上运行该函数依次。
Thanks for help. 感谢帮助。
Creative solution: 创意解决方案
:first-child
) :first-child
) $(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.com/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. @tgifred最后,我设法找到了有效的解决方案。 It is like this.
就是这样。 I hope it helps.
我希望它有所帮助。
<html>
<head>
<script src="https://ajax.googleapis.com/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. 你可以玩缓和。
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"));
});
Try using $.next(). 尝试使用$ .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())
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.