【问题标题】:How to link a pre loader html to index如何将预加载器 html 链接到索引
【发布时间】:2020-07-28 05:08:04
【问题描述】:

我为我的网站设计了一个运行良好的预加载器。但我想将它链接到另一个 html 页面而不将源代码复制到它。 就像我将预加载器页面命名为 pre=loader.html 一样,我想通过提供 preloader.html 的链接直接将其链接到 index.html 页面。我希望我只是在索引页面中提供预加载器的链接,并且预加载器可以正常工作。

代码如下:

function myFunction() {
  myVar = setTimeout(showPage, 3000);
}
function showPage() {
  document.getElementById("loader").style.display = "none";
  document.getElementById("myDiv").style.display = "block";
}
/* Center the loader */
#loader {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 1;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid #3498db;
  width: 120px;
  height: 120px;
-webkit-animation: spin 2s linear infinite;
  animation: spin 2s linear infinite;

@-webkit-keyframes spin {
  100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Add animation to "page content" */
.animate-bottom {
  position: relative;
  -webkit-animation-name: animatebottom;
  -webkit-animation-duration: 1s;
  animation-name: animatebottom;
  animation-duration: 1s
}

@-webkit-keyframes animatebottom {
  from { bottom:-100px; opacity:0 } 
  to { bottom:0px; opacity:1 }
}

@keyframes animatebottom { 
  from{ bottom:-100px; opacity:0 } 
  to{ bottom:0; opacity:1 }
}

#myDiv {
  display: none;
  text-align: center;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body onload="myFunction()" style="margin:0;">
    <div id="loader"></div>
    <div style="display:none;" id="myDiv" class="animate-bottom">
      <h2>Tada!</h2>
      <p>Some text in my newly loaded page..</p>
    </div>
  </body>          
</html>

提前谢谢你

【问题讨论】:

    标签: javascript html css preloader


    【解决方案1】:

    创建一个JS文件说prelaoder.js,它的内容应该是:

    function appendLoader(){
    document.body.prepend('<div id="loader"></div>');
    }
    function hideLoader(){
     document.getElementById("loader").style.display = "none";
    }
    

    在你的 html 页面中包含 prelaoder.js 作为脚本标签的源,然后在需要的地方调用 appendLoader 和 hideLoader。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-25
      • 2011-04-08
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多