Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Loading Screen while page loads

This is a little hard to explain, So I'll try my best

So while a HTML page loads, I'd like there to be a cool loading screen going on. When it finishes loading, I want the loading screen to clear and the HTML document to be shown.

Basically, I want This:

CSS:

/* Absolute Center CSS Spinner */ .loading {   position: fixed;   z-index: 999;   height: 2em;   width: 2em;   overflow: show;   margin: auto;   top: 0;   left: 0;   bottom: 0;   right: 0; }  /* Transparent Overlay */ .loading:before {   content: '';   display: block;   position: fixed;   top: 0;   left: 0;   width: 100%;   height: 100%;   background-color: rgba(0,0,0,0.3); }  /* :not(:required) hides these rules from IE9 and below */ .loading:not(:required) {   /* hide "loading..." text */   font: 0/0 a;   color: transparent;   text-shadow: none;   background-color: transparent;   border: 0; }  .loading:not(:required):after {   content: '';   display: block;   font-size: 10px;   width: 1em;   height: 1em;   margin-top: -0.5em;   -webkit-animation: spinner 1500ms infinite linear;   -moz-animation: spinner 1500ms infinite linear;   -ms-animation: spinner 1500ms infinite linear;   -o-animation: spinner 1500ms infinite linear;   animation: spinner 1500ms infinite linear;   border-radius: 0.5em;   -webkit-box-shadow: rgba(0, 0, 0, 0.75) 1.5em 0 0 0, rgba(0, 0, 0, 0.75) 1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) 0 1.5em 0 0, rgba(0, 0, 0, 0.75) -1.1em 1.1em 0 0, rgba(0, 0, 0, 0.5) -1.5em 0 0 0, rgba(0, 0, 0, 0.5) -1.1em -1.1em 0 0, rgba(0, 0, 0, 0.75) 0 -1.5em 0 0, rgba(0, 0, 0, 0.75) 1.1em -1.1em 0 0;   box-shadow: rgba(0, 0, 0, 0.75) 1.5em 0 0 0, rgba(0, 0, 0, 0.75) 1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) 0 1.5em 0 0, rgba(0, 0, 0, 0.75) -1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) -1.5em 0 0 0, rgba(0, 0, 0, 0.75) -1.1em -1.1em 0 0, rgba(0, 0, 0, 0.75) 0 -1.5em 0 0, rgba(0, 0, 0, 0.75) 1.1em -1.1em 0 0; }  /* Animation */  @-webkit-keyframes spinner {   0% {     -webkit-transform: rotate(0deg);     -moz-transform: rotate(0deg);     -ms-transform: rotate(0deg);     -o-transform: rotate(0deg);     transform: rotate(0deg);   }   100% {     -webkit-transform: rotate(360deg);     -moz-transform: rotate(360deg);     -ms-transform: rotate(360deg);     -o-transform: rotate(360deg);     transform: rotate(360deg);   } } @-moz-keyframes spinner {   0% {     -webkit-transform: rotate(0deg);     -moz-transform: rotate(0deg);     -ms-transform: rotate(0deg);     -o-transform: rotate(0deg);     transform: rotate(0deg);   }   100% {     -webkit-transform: rotate(360deg);     -moz-transform: rotate(360deg);     -ms-transform: rotate(360deg);     -o-transform: rotate(360deg);     transform: rotate(360deg);   } } @-o-keyframes spinner {   0% {     -webkit-transform: rotate(0deg);     -moz-transform: rotate(0deg);     -ms-transform: rotate(0deg);     -o-transform: rotate(0deg);     transform: rotate(0deg);   }   100% {     -webkit-transform: rotate(360deg);     -moz-transform: rotate(360deg);     -ms-transform: rotate(360deg);     -o-transform: rotate(360deg);     transform: rotate(360deg);   } } @keyframes spinner {   0% {     -webkit-transform: rotate(0deg);     -moz-transform: rotate(0deg);     -ms-transform: rotate(0deg);     -o-transform: rotate(0deg);     transform: rotate(0deg);   }   100% {     -webkit-transform: rotate(360deg);     -moz-transform: rotate(360deg);     -ms-transform: rotate(360deg);     -o-transform: rotate(360deg);     transform: rotate(360deg);   } } 

HTML:

<div class="loading">Loading&#8230;</div> 

to be happening while the page loads. when the page loads, That is cleared, and the rest of the HTML document is displayed. (I'm making a credits system and while the credits load i just really need it to say it's loading, people complain they can't click anything and i have to tell them all it's just loading)

I'd like to stay away from Ajax as much as possible since I'd like to learn Javascript.

like image 225
unknownA Avatar asked Aug 11 '14 22:08

unknownA


People also ask

How do I display a loading screen while site content loads?

visible through css [ display:block; ] and make the rest of the page invisible through css[ display:none; ]. Once the loading is done, just make the loading invisible and the page visible again with the same technique. You can use the document. getElementById() to select the divs you want to change the display.

What is jQuery and how do you use it?

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.


1 Answers

You can wait until the body is ready:

function onReady(callback) {   var intervalId = window.setInterval(function() {     if (document.getElementsByTagName('body')[0] !== undefined) {       window.clearInterval(intervalId);       callback.call(this);     }   }, 1000); }  function setVisible(selector, visible) {   document.querySelector(selector).style.display = visible ? 'block' : 'none'; }  onReady(function() {   setVisible('.page', true);   setVisible('#loading', false); });
body {   background: #FFF url("https://i.imgur.com/KheAuef.png") top left repeat-x;   font-family: 'Alex Brush', cursive !important; }  .page    { display: none; padding: 0 0.5em; } .page h1 { font-size: 2em; line-height: 1em; margin-top: 1.1em; font-weight: bold; } .page p  { font-size: 1.5em; line-height: 1.275em; margin-top: 0.15em; }  #loading {   display: block;   position: absolute;   top: 0;   left: 0;   z-index: 100;   width: 100vw;   height: 100vh;   background-color: rgba(192, 192, 192, 0.5);   background-image: url("https://i.stack.imgur.com/MnyxU.gif");   background-repeat: no-repeat;   background-position: center; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" rel="stylesheet"/> <link href="https://fonts.googleapis.com/css?family=Alex+Brush" rel="stylesheet"> <div class="page">   <h1>The standard Lorem Ipsum passage</h1>   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure     dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <div id="loading"></div>

Here is a JSFiddle that demonstrates this technique.


Update

Here is a modern version using promises. The promise is completely optional now, as it is only used for a delay. The DOMContentLoaded event will fire once the page is loaded.

const wait = (delay = 0) =>   new Promise(resolve => setTimeout(resolve, delay));  const setVisible = (elementOrSelector, visible) =>    (typeof elementOrSelector === 'string'     ? document.querySelector(elementOrSelector)     : elementOrSelector   ).style.display = visible ? 'block' : 'none';  setVisible('.page', false); setVisible('#loading', true);  document.addEventListener('DOMContentLoaded', () =>   wait(1000).then(() => {     setVisible('.page', true);     setVisible('#loading', false);   }));
body {   background: #FFF url("https://i.imgur.com/KheAuef.png") top left repeat-x;   font-family: 'Alex Brush', cursive !important; }  .page    { display: none; padding: 0 0.5em; } .page h1 { font-size: 2em; line-height: 1em; margin-top: 1.1em; font-weight: bold; } .page p  { font-size: 1.5em; line-height: 1.275em; margin-top: 0.15em; }  #loading {   display: flex;   position: fixed;   z-index: 100;   width: 100%;   height: 100%;   background-color: rgba(192, 192, 192, 0.5);   background-image: url("https://i.stack.imgur.com/MnyxU.gif");   background-repeat: no-repeat;   background-position: center; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" rel="stylesheet"/> <link href="https://fonts.googleapis.com/css?family=Alex+Brush" rel="stylesheet"> <div class="page">   <h1>The standard Lorem Ipsum passage</h1>   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure     dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <div id="loading"></div>
like image 92
Mr. Polywhirl Avatar answered Sep 19 '22 20:09

Mr. Polywhirl