[英]Show/Hide Series of Divs After X Seconds Without Loop
On page load, I'm trying to show a div1 for 10 seconds and then hide it, show div2
for 3 seconds and then hide it, then show the last div3 without hiding it. 在页面加载时,我试图显示div1 10秒钟,然后将其隐藏,显示
div2
3秒钟,然后将其隐藏,然后显示最后一个div3,而不隐藏它。
So I have in my css... 所以我在CSS中...
#show1, #show2, #show3 {display: none; }
In the html... 在HTML中...
<div id="showhide">
<div id="show1">
<h2>Step 1: Checking if your area is eligible...</h2>
</div>
<div id="show2">
<h2>Success!</h2>
</div>
<div id="show3">
<h2>Just need to gather a little more information...</h2>
</div>
</div>
And a functioning show/hide script, but I'm not sure how to 1) make it not loop and 2) set different intervals for show/hide of div1 and div2. 和功能正常的显示/隐藏脚本,但是我不确定如何1)使其不循环和2)为div1和div2的显示/隐藏设置不同的间隔。
$(function () {
var counter = 0,
divs = $('#show1, #show2, #show3');
function showDiv () {
divs.hide() // hide all divs
.filter(function (index) { return index == counter % 3; }) // figure out correct div to show
.show('fast'); // and show it
counter++;
}; // function to loop through divs and show correct div
showDiv(); // show first div
setInterval(function () {
showDiv(); // show next div
}, 1 * 1000); // do this every 10 seconds
});
Any help would be appreciated! 任何帮助,将不胜感激!
Added data-nextid="show2" data-showtimeout="10" class="hide-div" style="display:none;"
添加了
data-nextid="show2" data-showtimeout="10" class="hide-div" style="display:none;"
to <div>
tags. 到
<div>
标签。
data-nextid
will have id of next div to show. data-nextid
将显示下一个div的ID。
data-showtimeout
will have value of display time for current div. data-showtimeout
将具有当前div的显示时间值。
hide-div
class is use for set all such div to hide at function call. hide-div
类用于将所有此类div设置为在函数调用时隐藏。
style="display:none;"
to each div will hide all div initially. 每个div最初都会隐藏所有div。
Removed CSS #show1, #show2, #show3 {display: none; }
删除了CSS
#show1, #show2, #show3 {display: none; }
#show1, #show2, #show3 {display: none; }
Added initial call for display first div displayDivWithTimer.apply($('#show1'));
添加了对显示第一个div的初始调用
displayDivWithTimer.apply($('#show1'));
inside $(document).ready()
. 在
$(document).ready()
。
function displayDivWithTimer() { $(this).css('display', 'block'); var timeout = $(this).attr('data-showtimeout'); var nextid = $(this).attr('data-nextid'); //console.log("timeout:" + timeout + ", nextid:" + nextid); if (timeout && nextid) { setTimeout(() => { $('.hide-div').css('display', 'none'); displayDivWithTimer.apply($('#' + nextid)); }, parseInt(timeout) * 1000); } } $(document).ready(function() { displayDivWithTimer.apply($('#show1')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div id="showhide"> <div data-nextid="show2" data-showtimeout="10" id="show1" class="hide-div" style="display:none;"> <h2>Step 1: Checking if your area is eligible...</h2> </div> <div data-nextid="show3" data-showtimeout="3" id="show2" class="hide-div" style="display:none;"> <h2>Success!</h2> </div> <div id="show3" class="hide-div" style="display:none;"> <h2>Just need to gather a little more information...</h2> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.