gpt4 book ai didi

jquery - 在响应式背景图像上更改一次背景图像

转载 作者:行者123 更新时间:2023-11-28 12:58:20 27 4
gpt4 key购买 nike

我用的就是这个

background: url(bilder/cover.jpg) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

我想要完成的是,背景图像在大约 2 秒后更改为背景图像,然后保持不变。

我知道有 Jqueries 可以像这样更改背景,但我不确定如何使用我正在使用的代码来实现类似的功能!如果有人对此有解决方案,那就太棒了!

我改变背景的起点是我在 w3schools 上找到的代码:

{
width:100px;
height:100px;
background:red;
animation:myfirst 5s;
-webkit-animation:myfirst 5s; /* Safari and Chrome */
}

@keyframes myfirst
{
from {background:red;}
to {background:yellow;}
}

@-webkit-keyframes myfirst /* Safari and Chrome */
{
from {background:red;}
to {background:yellow;}
}

最佳答案

EXAMPLE

HTML

<div id="myBackground"></div>

CSS

#myBackground {
bottom: 0;
left: 0;
position: fixed;
right: 0;
top: 0;

background: url(http://jsfiddle.net/img/initializing.png) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

JavaScript

var imgUrl = "url(http://cdn.css-tricks.com/wp-content/themes/CSS-Tricks-10/images/bg.png)";

$(function() { // starts when page is loaded and ready
setTimeout(function() {
$("#myBackground").css("background-image", imgUrl);
}, 2000); // 2 second timer
})

Alternate Style (with FadeIn/Out effect)

HTML

<div id="myBackground">
<img src="http://jsfiddle.net/img/initializing.png" />
<img src="http://cdn.css-tricks.com/wp-content/themes/CSS-Tricks-10/images/bg.png" />
</div>

CSS

#myBackground {
bottom: 0;
left: 0;
position: fixed;
right: 0;
top: 0;
}
#myBackground img {
height: 100%;
width: 100%;
}
#myBackground img:nth-child(2) {
display: none;
}

JavaScript

$(function() {    //    starts when page is loaded and ready
setTimeout(function() {
$("#myBackground img:nth-child(1)").fadeOut("slow");
$("#myBackground img:nth-child(2)").fadeIn(1500);
}, 2000); // 2 second timer
})

关于jquery - 在响应式背景图像上更改一次背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16359492/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com