JavaScript Loading Screen while page loads

后端 未结 6 1274
无人共我
无人共我 2020-11-30 18:17

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

6条回答
  •  青春惊慌失措
    2020-11-30 18:27

    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;
    }
    
    
    

    The standard Lorem Ipsum passage

    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.

    Here is a JSFiddle that demonstrates this technique.

提交回复
热议问题