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 的特效
- 苟利
- 國家
- 生死以
- 豈能
- 禍福
- 趨避之
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{z-index:99;list-style:none}li{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}}