我们将使用一个无序列表的幻灯片,我们会添加一个跨度为每个图像和一个标题:
<ul class="cb-slideshow">
<li>
<span>Image 01</span>
<div>
<h3>re·lax·a·tion</h3>
</div>
</li>
<li><!--...--></li>
<li><!--...--></li>
</ul>
让我们首先定义无序列表的样式。这将是固定的。我们还将使用:在伪元素以地方模式的图片:
.cb-slideshow,
.cb-slideshow:after {
position: fixed;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
z-index: 0;
}
.cb-slideshow:after {
content: '';
background: transparent url(../images/pattern.png) repeat top left;
}
我们将使用一个重复的点模式,但你也可以使用,例如,一个css梯度与一些透明度。
将包含一个幻灯片图像定位绝对有100%的宽度和高度。因为我们有一些文本内部,我们将使颜色透明,因为我们不想看到它。background-size属性值“cover”将确保元素的背景图像覆盖所有的区域,因此它是屏幕的大小。
.cb-slideshow li span {
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
color: transparent;
background-size: cover;
background-position: 50% 50%;
background-repeat: none;
opacity: 0;
z-index: 0;
animation: imageAnimation 36s linear infinite 0s;
}
每个跨度的动画将持续6秒,运行一个inifinite次数。但让我们来看看细节,首先,我们将与整体风格划分:
.cb-slideshow li div {
z-index: 1000;
position: absolute;
bottom: 30px;
left: 0px;
width: 100%;
text-align: center;
opacity: 0;
color: #fff;
animation: titleAnimation 36s linear infinite 0s;
}
.cb-slideshow li div h3 {
font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
font-size: 240px;
padding: 0;
line-height: 200px;
}
标题的动画还将持续6秒。
现在,我们将为所有的跨越和定义背景图片动画延迟,因此每个幻灯片图片和标题后出现前一个的6秒后:
.cb-slideshow li:nth-child(1) span {
background-image: url(../images/1.jpg)
}
.cb-slideshow li:nth-child(2) span {
background-image: url(../images/2.jpg);
animation-delay: 6s;
}
.cb-slideshow li:nth-child(3) span {
background-image: url(../images/3.jpg);
animation-delay: 12s;
}
.cb-slideshow li:nth-child(4) span {
background-image: url(../images/4.jpg);
animation-delay: 18s;
}
.cb-slideshow li:nth-child(5) span {
background-image: url(../images/5.jpg);
animation-delay: 24s;
}
.cb-slideshow li:nth-child(6) span {
background-image: url(../images/6.jpg);
animation-delay: 30s;
}
.cb-slideshow li:nth-child(2) div {
animation-delay: 6s;
}
.cb-slideshow li:nth-child(3) div {
animation-delay: 12s;
}
.cb-slideshow li:nth-child(4) div {
animation-delay: 18s;
}
.cb-slideshow li:nth-child(5) div {
animation-delay: 24s;
}
.cb-slideshow li:nth-child(6) div {
animation-delay: 30s;
}
现在,让我们看一下幻灯片的动画。每个将动画时间跨度6秒。在6秒时我们将从0到1改变不透明度动画达到8%。然后这种透明度得到保持,直到17%。当达到25%不透明度应该已经0又保持到最后。
现在,为什么这些值?我们希望每个图像可见6秒钟,我们知道,一个周期结束时,我们希望第一个图像显示一次。
@keyframes imageAnimation {
0% { opacity: 0; animation-timing-function: ease-in; }
8% { opacity: 1; animation-timing-function: ease-out; }
17% { opacity: 1 }
25% { opacity: 0 }
100% { opacity: 0 }
}
标题,我们有相同的理由,我们希望它消失更快一点,因此,不透明度19%:
@keyframes titleAnimation {
0% { opacity: 0 }
8% { opacity: 1 }
17% { opacity: 1 }
19% { opacity: 0 }
100% { opacity: 0 }
}
浏览器不支持动画,我们将简单地显示过去的幻灯片图像通过跨度的不透明度设置为1:
.no-cssanimations .cb-slideshow li span{
opacity: 1;
}
Modernizr no-cssanimations类添加。
让我们照顾标题的字体大小视窗时小。我们将使用媒体查询,以设置在特定宽度较小字体大小:
@media screen and (max-width: 1140px) {
.cb-slideshow li div h3 { font-size: 140px }
}
@media screen and (max-width: 600px) {
.cb-slideshow li div h3 { font-size: 80px }
}
这是所有的简单版本幻灯片!现在,让我们看看我们如何转换有点增添情趣。
选择动画的例子
现在,我们可以一起玩有点标题显示图像和动画。
下面的动画会让图像扩大一点,稍微旋转:
@keyframes imageAnimation {
0% {
opacity: 0;
animation-timing-function: ease-in;
}
8% {
opacity: 1;
transform: scale(1.05);
animation-timing-function: ease-out;
}
17% {
opacity: 1;
transform: scale(1.1) rotate(3deg);
}
25% {
opacity: 0;
transform: scale(1.1) rotate(3deg);
}
100% { opacity: 0 }
}
从正确的标题将下滑(我们不得不改变标题的text-align部门“正确的”),向左滑动和消失,消失:
@keyframes titleAnimation {
0% {
opacity: 0;
transform: translateX(200px);
}
8% {
opacity: 1;
transform: translateX(0px);
}
17% {
opacity: 1;
transform: translateX(0px);
}
19% {
opacity: 0;
transform: translateX(-400px);
}
25% { opacity: 0 }
100% { opacity: 0 }
}