【问题标题】:Change Background on ID load在 ID 加载时更改背景
【发布时间】:2017-04-14 06:53:43
【问题描述】:

我想知道如何在 Id 加载时更改我的背景。现在背景设置为预加载器的颜色。我已经想出了如何将装载机隐藏在身体负载上,但有人帮助我如何将背景更改为图片。也是在加载程序出现之前,身体弹出的元素,所以有什么解决方案可以隐藏它吗?后台链接在后台 1 id。

https://www.w3schools.com/code/tryit.asp?filename=FEMJ1DP31QMZ

function hidespinner() {
  document.getElementById('spinner').style.display = 'none';
  document.getElementById('heading').style.display = 'none';
  document.getElementById('background1').style.display.backgroundImage = 'url(https://s28.postimg.org/rbexyjiil/IFBAKGROUND1.jpg)';
}
html {
  background-color: #ace5f4;
  background-size: cover;
}

#spinner {
  height: 50px;
  width: 50px;
  left: 0;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  position: fixed;
}

#spinner .inner {
  height: 50px;
  width: 50px;
  position: absolute;
  border: 5px solid transparent;
  opacity: 0.7;
  border-radius: 100%;
  animation-name: rotate;
  animation-iteration-count: infinite;
  animation-timing-function: ease;
}

#spinner .inner:nth-child(1) {
  border-top-color: white;
  border-bottom-color: white;
  animation-duration: 2s;
}

#spinner .inner:nth-child(2) {
  border-left-color: #3bb3ee;
  border-right-color: #3bb3ee;
  animation-duration: 3s;
}

#spinner .inner:nth-child(3) {
  border-top-color: #34abdb;
  border-bottom-color: #34abdb;
  animation-duration: 4s;
}

@keyframes rotate {
  from {
    transform: rotate(0deg)
  }
  to {
    transform: rotate(360deg)
  }
}

#heading {
  color: white;
  font-family: Helvetica;
  text-align: center;
  font-size: 72px;
}

#background1 {
  background: url(https://s28.postimg.org/rbexyjiil/IFBAKGROUND1.jpg) no-repeat center fixed;
  background-size: cover;
}
<link rel="shortcut icon" type="image/png" href="https://image.flaticon.com/icons/png/128/222/222506.png">

<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

<!-- Fonts Awesome CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

<body onload="hidespinner()">
  <h1 id="heading"><i class="fa fa-plane"></i> v<strong>Crew</strong></h1>
      <div id="spinner">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
      </div>
      Hello Is the spinner on?
</body>

请注意,即使是当前代码也受版权保护。我还想将我制作的这个加载器添加到页面中,这样任何人都可以提出有关如何添加它或将其添加到网页并给我代码的建议。

https://www.w3schools.com/code/tryit.asp?filename=FEAZZM840UQS

function hideloader() {
  document.getElementById("loading").style.display = "none";
}
@import url('https://fonts.googleapis.com/css?family=Spirax');
@import url('https://fonts.googleapis.com/css?family=Indie+Flower|Spirax');

body {
  background-color: #58e8f8;
}

.silly {
  color: white;
  text-align: center;
  font-family: "Indie Flower"
}

.spinner {
  width: 80px;
  height: 80px;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}

.double-bounce1,
.double-bounce2 {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: white;
  opacity: 0.6;
  position: absolute;
  top: 0;
  left: 0;
  -webkit-animation: sk-bounce 2.0s infinite ease-in-out;
  animation: sk-bounce 2.0s infinite ease-in-out;
}

.double-bounce2 {
  -webkit-animation-delay: -2.0s;
  animation-delay: -1.0s;
}

@-webkit-keyframes sk-bounce {
  0%,
  100% {
    -webkit-transform: scale(0.0)
  }
  50% {
    -webkit-transform: scale(1.0)
  }
}

@keyframes sk-bounce {
  0%,
  100% {
    transform: scale(0.0);
    -webkit-transform: scale(0.0);
  }
  50% {
    transform: scale(1.0);
    -webkit-transform: scale(1.0);
  }
<title>Page Title</title>
<script type="text/javascript" src="https://gc.kis.v2.scr.kaspersky-labs.com/88CE1C4C-84C0-9E49-A763-9D3DCEC43907/main.js" charset="UTF-8"></script>

<body onload="hideloader">
  <h1 class="silly"> vCrew </h1>

  <div id="loading" class="spinner">
    <div class="double-bounce1"></div>
    <div class="double-bounce2"></div>
  </div>
</body>

这对 w3schools.com/code/tryit.asp?filename=FEMJIZCDHJBW 有用吗?

【问题讨论】:

  • 欢迎来到 SO。请先转至HELP center,然后阅读How to 并提供MCVE : Minimal, Complete, and Verifiable Example。如果周围的人可以轻松阅读并理解您的意思或问题所在,他们将更愿意提供帮助 :) 但请记住,SO 是一个有帮助的社区,所以请向我们展示一些您搞砸的代码我们很乐意帮助改进它...
  • 您可能需要使用一些 Javascript。
  • 我创建这个是为了回答另一个与加载相关的问题:stackoverflow.com/a/25253988/1762224
  • 首先:document.getElementById('background1').style.display.backgroundImage = 'url(https://s28.postimg.org/rbexyjiil/IFBAKGROUND1.jpg)'; 我认为.display 不应该在那里。它本身就是一个属性,例如 backgroundImage

标签: javascript php html css


【解决方案1】:

您可以尝试在内容顶部添加加载 div 并隐藏/显示加载顺序,直到您的数据出现。

onReady(function() {
  toggleClass(document.getElementById('page'), 'hidden');
  toggleClass(document.getElementById('loading'), 'hidden');
});

function onReady(callback) {
  var intervalID = window.setInterval(function checkReady() {
    if (document.getElementsByTagName('body')[0] !== undefined) {
      window.clearInterval(intervalID);
      callback.call(this);
    }
  }, 1000);
}

// http://youmightnotneedjquery.com/
function toggleClass(el, className) {
  if (el.classList) el.classList.toggle(className);
  else {
    var classes = el.className.split(' ');
    var existingIndex = classes.indexOf(className);
    if (existingIndex >= 0) classes.splice(existingIndex, 1);
    else classes.push(className);
    el.className = classes.join(' ');
  }
}
body {
  background: #FFF url("http://i.imgur.com/KheAuef.png") top left repeat-x;
  font-family: "Brush Script MT", cursive;
}

h1 {
  font-size: 2em;
  margin-bottom: 0.2em;
  padding-bottom: 0;
}

p {
  font-size: 1.5em;
  margin-top: 0;
  padding-top: 0;
}

#loading {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 100;
  width: 100vw;
  height: 100vh;
  background-color: rgba(192, 192, 192, 0.9);
  background-image: url("http://i.stack.imgur.com/MnyxU.gif");
  background-repeat: no-repeat;
  background-position: center;
}

.hidden {
  display: none !important;
}
<script src="https://rawgit.com/bgrins/loremjs/master/lorem.js"></script>
<div id="page" class="hidden">
  <h1>The standard Lorem Ipsum passage</h1>

  <div class="content" data-lorem="7s"></div>
</div>
<div id="loading"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2023-03-22
    相关资源
    最近更新 更多