简体   繁体   English

隐藏一个div并显示另一个?

[英]Hide one div and show another?

I have two divs on called #loading and one called #main ; 我有两个divs名为#loading和一个叫#main ;

<div id="loading"></div>
<div id="main"></div>

What I'm trying to achieve is with using javascript, show #loading for five seconds then after the five seconds hide the #loading div and show the #main div. 我正在努力实现是使用JavaScript,显示#loading五秒钟,然后后五秒隐藏#loading股利和显示#main div中。 The #main div is default hidden and once the #loading div is hidden the #main div shows. #main的div是默认隐藏,一旦#loading DIV隐藏在#main DIV节目。

I'm assuming that to achieve this would be a mixture of css and javascript; 我假设要实现此目标将是CSS和javascript的混合; hopefully you understand what I'm trying to achieve and can help me accomplish what I'm trying to achieve. 希望您能理解我正在努力实现的目标,并能帮助我完成正在努力实现的目标。

Thankyou. 谢谢。

Your css would be: 您的CSS将是:

#main {
    display:none;
}

The JS would be: JS将是:

setTimeout(function() {
    document.getElementById('main').style.display = 'block';
    document.getElementById('loading').style.display = 'none';
}, 5000);

Maybe this could help you out without the use of CSS. 也许这可以在不使用CSS的情况下帮助您。 Only pure Jquery. 仅纯Jquery。

 $("#loading").show(); $("#main").hide(); setTimeout(function() { $("#loading").hide(); $("#main").show() }, 5000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="loading">loading here..</div> <div id="main" class="hidden">This is main content</div> 

Use setTimeout. 使用setTimeout。

 window.onload = function() { setTimeout(function() { document.getElementById("loading").style.display = "none"; document.getElementById("main").style.display = "block"; }, 5*1000); } 
 #main { display: none; } 
 <div id="loading">loading</div> <div id="main">main</div> 

Hope this helps 希望这可以帮助

function loading(dur) {
  if (window.busy) {return;}
  document.getElementById('loading').style.display = "block";
  document.getElementById('main').style.display = "none";
  window.busy = setTimeout(function () {
    document.getElementById('loading').style.display = "none";
    document.getElementById('main').style.display = "block";
    window.busy = 0;
  }, dur);
}

loading(5000);

Personaly I would avoid using ID's here as they polute the global. 我个人会避免在这里使用ID,因为它们会污染全局。

You can do this nicely with CSS and classes.. 您可以使用CSS和类很好地做到这一点。

 var holder = document.querySelector('.holder'); setTimeout(function () { holder.classList.remove('isloading'); }, 5000); 
 .loading { display: none; } div.isloading .loading { display: block; } .main { display: none; } div:not(.isloading) .main { display: block; } 
 <div class="holder isloading"> <div class="loading">Loading</div> <div class="main">Main</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM