javascript - 使 Javascript 成为 HTML 页面的预加载器

标签 javascript jquery html

我想制作一个 JavaScript(JavaScript 动画)作为我的项目网站的预加载器。

大致如下: http://soulwire.github.io/Plasmatic-Isosurface/

我希望它首先运行,直到下载页面的元素,之后它应该关闭并显示完全加载的页面

这可能吗?

谢谢

这是我想要测试的示例 HTML 代码

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Javascript preloader</title>
  <link rel="stylesheet" href="css/style.css">  
</head>

<body>
  <div class="preloader"></div>
  <canvas id='canvas'></canvas>
    <script  src="js/index.js"></script>
    <img src="https://wallpaperscraft.com/image/stars_sky_shore_84534_1920x1080.jpg" alt="safe"></img>
</body>
</html>

**编辑道歉代码

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Preloader Testing</title>
       <style>
      .preloader{
          position: fixed;
          top: 0;
          left: 0;
          width: 100vw;
          height: 100vh;
          z-index: 1000;
      }
      </style>
      <link rel="stylesheet" href="css/style.css">
</head>

<body>
 <canvas class="preloader" id='canvas'></canvas>
    <script  src="js/index.js"></script>
 <img src="https://wallpaperscraft.com/image/stars_sky_shore_84534_1920x1080.jpg" alt="safe"></img>
</body>
<script>
    //after window is loaded completely 
    window.onload = function(){
        //hide the preloader
        document.querySelector(".preloader").style.display = "none";
    }
</script>
</html>

最佳答案

您可以默认显示预加载器。一旦网页完全加载,您只需将其隐藏即可。这是一个代码示例

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Javascript preloader</title>



      <link rel="stylesheet" href="css/style.css">
      <style>
      /*Styling preloader*/
      .preloader{
          /*
          Making the preloader floating over other elements.
          The preloader is visible by default. 
          */
          position: fixed;
          top: 0;
          left: 0;
          width: 100vw;
          height: 100vh;
          z-index: 1000;
      }
      </style>


</head>

<body>
    <div class="preloader"><span class="preloader-js"></span></div>
  <canvas id='canvas'></canvas>

    <script  src="js/index.js"></script>

    <img src="https://wallpaperscraft.com/image/stars_sky_shore_84534_1920x1080.jpg" alt="safe"></img>
</body>
<script>
    //after window is loaded completely 
    window.onload = function(){
        //hide the preloader
        document.querySelector(".preloader").style.display = "none";
    }
</script>
</html>

谢谢

关于javascript - 使 Javascript 成为 HTML 页面的预加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46635368/

相关文章:

javascript - 从链接中删除所有目标 ="_blank"

html - 我的 anchor 不工作

javascript - 如何发送 URL 中间带有参数的 JSON 请求

javascript - 使用基于 Angular js 的 UI 在 slider 上进行 Selenium 测试(python)

javascript - 使用变量 id 更改元素的文本

javascript - Angular JS 加载问题

javascript - 我如何在 jquery 中为 html5 视频制作 playTo() 函数

javascript - 如何获取 ExtJS 中按钮的 "pressed"属性的状态?

javascript - jQuery UI 的可排序更新事件未触发

javascript - JSON - 无法读取未定义的属性