简体   繁体   English

Javascript 加载屏幕 - window.onload 不起作用

[英]Javascript Loading screen - window.onload not working

I'm trying to make a loading screen for my project.我正在尝试为我的项目制作一个加载屏幕。 I need the javascript to remove the CSS property "Display: none" from (page) I can't figure out why my code doesn't work.我需要javascript从(页面)中删除CSS属性“Display:none”我不知道为什么我的代码不起作用。

The Problem was:问题是:

window.onload is not a function and i should use something like "window.onload = loader;" window.onload 不是一个函数,我应该使用类似“window.onload = loader;”的东西instead.反而。 Afterwards I should make it "display:inline"之后我应该让它“显示:内联”

Worked perfectly!完美地工作!

 window.onload(loader) function loader(){ document.getElementById("page").style.removeProperty("display"); }
 #page{ display: none; } /* Loading */ #loading{ width: 100%; height:100vh; background-color: #428BCA; } .loading_container{ margin: 0; position: absolute; top: 50%; left: 50%; margin-right: -50%; transform: translate(-50%, -50%); } #spin { border: 16px solid #f3f3f3; /* Light grey */ border-top: 16px solid #3498db; /* Blue */ border-radius: 50%; width: 120px; height: 120px; animation: spin 2s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } #loading h1{ color: #FFF; margin-left:10px; }
 <div id="page"> Content! </div> <div id="loading"> <div class="loading_container"> <div id="spin"></div> <h1>Loading...</h1> </div> </div>

window.onload is not a function. window.onload 不是一个函数。

You need to assign it to the function as follows.您需要将其分配给函数,如下所示。

window.onload = loader;

I'm assuming you want the loading animation to disappear upon successfully loading.我假设您希望加载动画在成功加载后消失。 The snippet in the loader() function emulates loading by waiting 3 seconds. loader()函数中的代码段通过等待 3 秒来模拟加载。

I set the css attributes differently.我以不同的方式设置了 css 属性。 You initialise #page to display: none , so I simply set it to the default value inline.您将 #page 初始化为display: none ,所以我只是将其设置为内联的默认值。

document.getElementById("page").style.display = "inline";

I would favor this method instead of removing the attribute entirely and letting it default.我更喜欢这种方法,而不是完全删除该属性并让它默认。 This way you can specify a particular display value of your desire.通过这种方式,您可以指定您想要的特定显示值。 Ie inline-block, block etc.即内联块,块等。

 window.onload = loader; function loader(){ setTimeout(function() { document.getElementById("loading").style.visibility = "hidden"; document.getElementById("page").style.display = "inline"; }, 3000); }
 #page{ display: none; } /* Loading */ #loading{ width: 100%; height:100vh; background-color: #428BCA; } .loading_container{ margin: 0; position: absolute; top: 50%; left: 50%; margin-right: -50%; transform: translate(-50%, -50%); } #spin { border: 16px solid #f3f3f3; /* Light grey */ border-top: 16px solid #3498db; /* Blue */ border-radius: 50%; width: 120px; height: 120px; animation: spin 2s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } #loading h1{ color: #FFF; margin-left:10px; }
 <div id="page"> Content! </div> <div id="loading"> <div class="loading_container"> <div id="spin"></div> <h1>Loading...</h1> </div> </div>

If I understand you correctly, you want to show the content of the page with the function 'loader', correct?如果我理解正确,您想显示带有“加载程序”功能的页面内容,对吗? If so, you can't try to get rid of the display property, because it needs to be set.如果是这样,您不能尝试摆脱display属性,因为它需要设置。 You want to change it from none to initial or inline .您想将其从none更改为initialinline

First of all window.onload is not a function.首先window.onload不是一个函数。 It's a property.这是一个财产。 You can simply assign your callback to it.您可以简单地将您的回调分配给它。 removeProperty("display"); is also wrong.也是错的。 It should be style.display = "none"应该是style.display = "none"

 window.onload = loader; function loader() { document.getElementById("page").style.display = "none"; } loader();
 #page { display: none; } /* Loading */ #loading { width: 100%; height: 100vh; background-color: #428BCA; } .loading_container { margin: 0; position: absolute; top: 50%; left: 50%; margin-right: -50%; transform: translate(-50%, -50%); } #spin { border: 16px solid #f3f3f3; /* Light grey */ border-top: 16px solid #3498db; /* Blue */ border-radius: 50%; width: 120px; height: 120px; animation: spin 2s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } #loading h1 { color: #FFF; margin-left: 10px; }
 <div id="page"> Content! </div> <div id="loading"> <div class="loading_container"> <div id="spin"></div> <h1>Loading...</h1> </div> </div>

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

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