Fügen Sie der Eigenschaft „Hintergrundbild' im Body-Tag eine Animation hinzu: Schritt-für-Schritt-Anleitung
P粉347804896
P粉347804896 2024-03-29 21:59:19
0
2
272

Ich verwende ein Hintergrundbild im Style-Tag.

<style>
body {
 background-image: url("img.jpg");
 background-size: cover;
 transition: background-image 4s linear;
 }
</style>

Ich möchte, dass das Bild eingeblendet wird, ohne der Seite ein separates Div hinzuzufügen. Kann dieses Ziel erreicht werden?

P粉347804896
P粉347804896

Antworte allen(2)
P粉940538947

是的,这是可能的。您可以这样做。

@keyframes mymove {
     from {
      /* background: white; */     
     }
     to {
      background: url("https://www.shutterstock.com/image-photo/welded-metal-chain-various-rigging-260nw-2238426561.jpg");
       }
}
body {        
        animation: mymove 2s infinite;
      }



P粉541551230

这是一个仅使用 CSS 的方法。但是,有一个警告,CSS 无法等待事件,因此它无法知道整个网站何时加载。

相反,在此代码段中,它会等待 1 秒,然后开始引入位于主体的 before 伪元素上的背景图像。

body {
  width: 100vw;
  height: 100vh;
  /*just for demo */
  position: relative;
}

body::before {
  background-image: url(https://picsum.photos/id/1015/1024/768);
  background-size: cover;
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  opacity: 0;
  animation: fadein 5s 1 1s linear forwards;
}

@keyframes fadein {
  to {
    opacity: 1;
  }
}
Hello, this is some text in the body. It will be seen even before the background starts to fade in.
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!