gpt4 book ai didi

javascript - 正文中的淡入淡出背景图像

转载 作者:行者123 更新时间:2023-11-30 09:31:49 26 4
gpt4 key购买 nike

我有下一个问题:我想交叉淡化 <body> 的不同背景图像标签,带计时器。可以看到here我想要实现的一个例子。问题是我对这些东西很陌生,不知道该怎么做...我看过很多帖子,但它们只会让我更加困惑!

我的 HTML 代码:

<body id="one" onload="startTimer()">
<!-- Some other tags -->
</body>
<script type = "text/javascript">
function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
var url = "url(" + images[x] + ")";
$("#one").css('background-image', url);
}

function startTimer() {
setInterval(displayNextImage, 6000);
}

var images = [], x = -1;
images[0] = "someimage1.jpg";
images[1] = "someimage2.jpg";
images[2] = "someimage3.jpg";
</script>

和我的 CSS 代码:

body {
background-image: url("images/forest1.jpeg");
background-size: cover;
}

我乐于接受建议,我的意思是,如果有更好的方法来做这些,那太好了!我只是想得到与上面显示的网络相同的观点!

提前致谢!

最佳答案

你不需要任何 JavaScript,你可以用 CSS 动画来实现它:

body {
background-size: cover;
}

@-webkit-keyframes changeBckg {
0% { background-image: url("https://unsplash.it/1000?image=1080"); }
25% { background-image: url("https://unsplash.it/1000?image=1081"); }
50% { background-image: url("https://unsplash.it/1000?image=1064"); }
75% {background-image: url("https://unsplash.it/1000?image=1078"); }
100% {background-image: url("https://unsplash.it/1000?image=1080"); }
}

@keyframes changeBckg {
0% { background-image: url("https://unsplash.it/1000?image=1080"); }
25% { background-image: url("https://unsplash.it/1000?image=1081"); }
50% { background-image: url("https://unsplash.it/1000?image=1064"); }
75% {background-image: url("https://unsplash.it/1000?image=1078"); }
100% {background-image: url("https://unsplash.it/1000?image=1080"); }
}

.letterAnimation {
-webkit-animation: changeBckg 16s ease infinite;
animation: changeBckg 16s ease infinite;
}
<body class="letterAnimation">

</body>

关于javascript - 正文中的淡入淡出背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45775213/

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