简体   繁体   English

如何在网页加载时显示加载内容,并在页面完全加载时隐藏该内容?

[英]How can I show load content while my webpage is loading and hide this content when the page is fully loaded?

I made a nice loading animation with CSS and JS/JQuery. 我使用CSS和JS / JQuery制作了不错的加载动画。 It features a white line turning around into a coloured circle. 它具有一条白线,变成一个彩色圆圈。 It works perfectly fine in a seperated html, but now i wanna include it on my webpage. 它在单独的html中工作得很好,但是现在我想将其包含在我的网页中。 There is a general script linked to the HTML called script.js. 有一个链接到HTML的通用脚本叫script.js。 The script for making the animation work is called animation.js. 用于制作动画的脚本称为animation.js。 This is my code so far: 到目前为止,这是我的代码:

<div class="loading">
    <div id="colouredCircle">
        <div id="whiteCircle"></div>
        <div id="line"></div>
    </div>
</div>
<script src="animation/animaton.js"></script>
<div class="contents">
    ...
</div>

I want to show the div.loading, while the document is still loading. 我想显示div.loading,而文档仍在加载。 When the document is fully loaded, i want to hide that div.loading, and show the actual content of my HTML page (div.contents). 当文档完全加载后,我想隐藏该div.loading,并显示我的HTML页面的实际内容(div.contents)。 Anyone any idea how to do this? 有人知道如何执行此操作吗? I'm kinda new to JS/JQuery. 我是JS / JQuery的新手。

Try to use this script. 尝试使用此脚本。 In this javascript your template append to body while loading. 在此javascript中,您的模板在加载时会附加到正文中。 after that it's remove from body 之后,将其从身体上移除

 var tmpl = '<div id="loading" class="loading">' + ' <div id="colouredCircle">' + '<div id="whiteCircle"></div>' + '<div id="line"></div>' + '</div>' + '</div>'; var loading = { start: function() { document.body.insertAdjacentHTML('beforeend', tmpl); }, complete: function() { var loading = document.getElementById("loading"); loading.remove(loading); } }; loading.start(); document.addEventListener("readystatechange", function() { if (document.readyState === "complete") { loading.complete(); } }); 

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

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