CSS3随机背景图片切换特效

css3的随机背景图片淡入淡出切换特效  演示效果如本篇文章背景所示
看大家扒我的幻想领域二次元限定版扒的比较累,扒了大半个小时的,抽空整理一下发出来
设计之初本来是打算使用jQuery进行实现的,但是注意到了css3的@keyframes 规则,css3已经强大到曾经只有js才能实现的效果

定义和用法

通过 @keyframes 规则,您能够创建动画。

创建动画的原理是,将一套 CSS 样式逐渐变化为另一套样式。

在动画过程中,您能够多次改变这套 CSS 样式。

以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%。

0% 是动画的开始时间,100% 动画的结束时间。

为了获得最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。

注释:请使用动画属性来控制动画的外观,同时将动画与选择器绑定。


核心css部分(记得切换图片地址)
body {
	background: #000;
	background-attachment: fixed;
	word-wrap: break-word;
	-webkit-background-size: cover;
	-moz-background-size: cover;
	background-size: cover;
	background-repeat: no-repeat
}

ul {
	list-style: none
}

.cb-slideshow li:nth-child(1) span {
	background-image: url(https://random.52ecy.cn/randbg.php?v=1520341099)
}

.cb-slideshow li:nth-child(2) span {
	background-image: url(https://random.52ecy.cn/randbg.php?v=1520341159)
}

.cb-slideshow li:nth-child(3) span {
	background-image: url(https://random.52ecy.cn/randbg.php?v=1520341149)
}

.cb-slideshow li:nth-child(4) span {
	background-image: url(https://random.52ecy.cn/randbg.php?v=1520341139)
}

.cb-slideshow li:nth-child(5) span {
	background-image: url(https://random.52ecy.cn/randbg.php?v=1520341129)
}

.cb-slideshow li:nth-child(6) span {
	background-image: url(https://random.52ecy.cn/randbg.php?v=1520341119)
}

.cb-slideshow,.cb-slideshow:after {
	position: fixed;
	width: 100%;
	height: 100%;
	top: 0;
	left: 0;
	z-index: -2
}

.cb-slideshow:after {
	content: ''
}

.cb-slideshow li span {
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
	color: transparent;
	background-size: cover;
	background-position: 50% 50%;
	background-repeat: none;
	opacity: 0;
	z-index: -2;
	-webkit-backface-visibility: hidden;
	-webkit-animation: imageAnimation 36s linear infinite 0s;
	-moz-animation: imageAnimation 36s linear infinite 0s;
	-o-animation: imageAnimation 36s linear infinite 0s;
	-ms-animation: imageAnimation 36s linear infinite 0s;
	animation: imageAnimation 36s linear infinite 0s
}

.cb-slideshow li:nth-child(2) span {
	-webkit-animation-delay: 6s;
	-moz-animation-delay: 6s;
	-o-animation-delay: 6s;
	-ms-animation-delay: 6s;
	animation-delay: 6s
}

.cb-slideshow li:nth-child(3) span {
	-webkit-animation-delay: 12s;
	-moz-animation-delay: 12s;
	-o-animation-delay: 12s;
	-ms-animation-delay: 12s;
	animation-delay: 12s
}

.cb-slideshow li:nth-child(4) span {
	-webkit-animation-delay: 18s;
	-moz-animation-delay: 18s;
	-o-animation-delay: 18s;
	-ms-animation-delay: 18s;
	animation-delay: 18s
}

.cb-slideshow li:nth-child(5) span {
	-webkit-animation-delay: 24s;
	-moz-animation-delay: 24s;
	-o-animation-delay: 24s;
	-ms-animation-delay: 24s;
	animation-delay: 24s
}

.cb-slideshow li:nth-child(6) span {
	-webkit-animation-delay: 30s;
	-moz-animation-delay: 30s;
	-o-animation-delay: 30s;
	-ms-animation-delay: 30s;
	animation-delay: 30s
}

@-webkit-keyframes imageAnimation {
	0% {
		opacity: 0;
		-webkit-animation-timing-function: ease-in
	}

	8% {
		opacity: 1;
		-webkit-transform: scale(1.05);
		-webkit-animation-timing-function: ease-out
	}

	17% {
		opacity: 1;
		-webkit-transform: scale(1.1) rotate(0)
	}

	25% {
		opacity: 0;
		-webkit-transform: scale(1.1) rotate(0)
	}

	100% {
		opacity: 0
	}
}
当然还是需要配合HTML代码的
HTML部分(其中的文字部分和<li>的数量是可以随意更改的)
<ul class="cb-slideshow">
	<li>
		<span>幻想</span></li>
	<li>
		<span>领域</span></li>
	<li>
		<span>一个</span></li>
	<li>
		<span>专业的</span></li>
	<li>
		<span>二次元</span></li>
	<li>
		<span>图床</span></li>
</ul>
注意:<li>的数量要和css对应上,并且至少要两个以上。另外ie浏览器是不支持CSS3的特效
  • 苟利
  • 国家
  • 生死以
  • 岂能
  • 祸福
  • 趋避之
本博客所有文章如无特别注明均为原创。作者:阿珏复制或转载请以超链接形式注明转自 阿珏博客
原文地址《CSS3随机背景图片切换特效

相关推荐

发表评论

路人甲 OωO表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(16)

这个怎么搞得了嘞?我研究了半天,还是全黑屏
凡人多烦事 5年前 (2020-02-16) 回复
@凡人多烦事:可以在群里at下我发个网址瞅瞅
阿珏 5年前 (2020-02-16) 回复
@阿珏:群里面那个是你呀?叫我主人还是本群群主
凡人多烦事 5年前 (2020-02-17) 回复
翻牌子
365cent 6年前 (2018-10-12) 回复
果然不能纯抄代码,第一行的background: #000;用了之后背景全黑没有特效,排查了后发现改成background-color:rgba(0,0,0,0);成功了,还有代码里缺分号作为一个强迫症的我有点不能接受。不过还是得感谢博主分享的特效代码!
我觉得不行 6年前 (2018-08-30) 回复
@我觉得不行:有些样式是要根据你实际情况更改的。css在线格式化后,默认最后一行样式都是不加分号的
阿珏 6年前 (2018-08-31) 回复
这图片无敌了
墨渊 6年前 (2018-06-20) 回复
审核元素,怎么屏蔽的,这个有点6,直接死机
墨渊 6年前 (2018-06-20) 回复
@墨渊:低调
阿珏 6年前 (2018-06-21) 回复
不能复制?那编写这篇文章干嘛?
腾讯视频 6年前 (2018-06-14) 回复
@腾讯视频:给你复制扒我文章?
阿珏 6年前 (2018-06-14) 回复
@阿珏:真要扒文章是个人都能做到吧,只是对于博客禁右感到莫名所以
腾讯视频 6年前 (2018-06-14) 回复
已解决,强制刷新CSS   ver=<?php echo time(); ?>
梦如 6年前 (2018-05-17) 回复
换的是随机图API的链接  在360极速浏览器里不会刷新随机图
梦奴 6年前 (2018-05-16) 回复
@梦奴:被浏览器缓存了,加个时间戳就可以了
阿珏 6年前 (2018-05-17) 回复
可以,赞一个
梦如 6年前 (2018-05-16) 回复