gpt4 book ai didi

html - 使用 css3 自动更改背景图像

转载 作者:行者123 更新时间:2023-11-28 04:54:25 24 4
gpt4 key购买 nike

我正在尝试使用 css3 自动更改背景图像。我有 9 张图片要更改背景。我面临的问题是,它在滑动时只显示第一张和最后一张图像,而不显示 2、3、4、5、6、7、8。我有以下代码:

<img id="img1" src="images/1.gif">
<img id="img2" src="images/2.gif">
<img id="img3" src="images/3.gif">
<img id="img4" src="images/4.gif">

<img id="img5" src="images/5.gif">
<img id="img6" src="images/6.gif">
<img id="img7" src="images/7.gif">
<img id="img8" src="images/8.gif">

<img id="img9" src="images/9.gif">

这是 CSS:

#img1, #img2, #img3, #img4, #img5, #img6, #img7, #img8, #img9 {
width:610px;
height:610px;
position:scroll;
z-index:-1;
animation-name: test;
animation-duration: 90s;
opacity:0;
}
#img2 {
animation-delay:10s;
-webkit-animation-delay:10s
}
#img3 {
animation-delay:20s;
-webkit-animation-delay:20s
}
#img4 {
animation-delay:30s;
-webkit-animation-delay:30s
}

#img5 {
animation-delay:40s;
-webkit-animation-delay:40s
}
#img6 {
animation-delay:50s;
-webkit-animation-delay:50s
}
#img7 {
animation-delay:60s;
-webkit-animation-delay:60s
}

#img8 {
animation-delay:70s;
-webkit-animation-delay:70s
}
#img9 {
animation-delay:80s;
-webkit-animation-delay:80s
}

@-webkit-keyframes test {
0% {
opacity: 1;
}
50% {
opacity: 1;
}
100% {
opacity: 1;
}
}
@keyframes test {
0% {
opacity: 1;
}
50% {
opacity: 1;
}
100% {
opacity: 1;
}
}

从1到8每张幻灯片的延迟时间为10s,除了image9,延迟时间为8.60s。

请帮助我哪里出错了。 :(

最佳答案

好的,我制作了一个 JS Fiddle,我想我已经想出了你想要的东西。

http://jsfiddle.net/d48vdxmv/2/

基本上,您需要先将图像包装在容器中:

<div id="container">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_01.jpg">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_02.jpg">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_03.jpg">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_04.jpg">
</div>

然后在 CSS 中,将容器设置为相对位置,并将图像设置为绝对位置,以便它们相互堆叠。

#container {
position:relative;
height:610px;
width:610px;
}
#container img {
position:absolute;
left:0;
}

然后使用不同的浏览器变体添加关键帧

@-webkit-keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}

@-moz-keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}

@-o-keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}

@keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}

然后是所有浏览器的动画细节

#container img {
-webkit-animation-name: imgFade;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 8s;

-moz-animation-name: imgFade;
-moz-animation-timing-function: ease-in-out;
-moz-animation-iteration-count: infinite;
-moz-animation-duration: 8s;

-o-animation-name: imgFade;
-o-animation-timing-function: ease-in-out;
-o-animation-iteration-count: infinite;
-o-animation-duration: 8s;

animation-name: imgFade;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 8s;
}

最后是每张图片的持续时间

#container img:nth-of-type(1) {
-webkit-animation-delay: 6s;
-moz-animation-delay: 6s;
-o-animation-delay: 6s;
animation-delay: 6s;
}
#container img:nth-of-type(2) {
-webkit-animation-delay: 4s;
-moz-animation-delay: 4s;
-o-animation-delay: 4s;
animation-delay: 4s;
}
#container img:nth-of-type(3) {
-webkit-animation-delay: 2s;
-moz-animation-delay: 2s;
-o-animation-delay: 2s;
animation-delay: 2s;
}
#container img:nth-of-type(4) {
-webkit-animation-delay: 0;
-moz-animation-delay: 0;
-o-animation-delay: 0;
animation-delay: 0;
}

关于html - 使用 css3 自动更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25789673/

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