background-image: url(../img/nature-4.jpg);
}
这部分可以用代码动态实现,但我们更关心切换的过渡效果,这里这样写就比较简单。
定义一个名叫hide的class,当需要隐藏某张幻灯片的时候,将这个class添加上去。这个class定义了用于遮罩的sprite image。
每一帧是100%全屏展示,我们的动画包含23张图像,需要将宽度设置为23×100%=2300%。使用CSS3 animation的steps方式过渡,添加CSS动画。我们想让sprite停在最后一帧的开头。要做到这一点,需要的步数比总数少一步,也就是22步:
.demo-1 .page-view .project:nth-child(even).hide {
-webkit-mask: url(../img/nature-sprite.png);
mask: url(../img/nature-sprite.png);
-webkit-mask-size: 2300% 100%;
mask-size: 2300% 100%;
-webkit-animation: mask-play 1.4s steps(22) forwards;
animation: mask-play 1.4s steps(22) forwards;
}.demo-1 .page-view .project:nth-child(odd).hide {
-webkit-mask: url(../img/nature-sprite-2.png);
mask: url(../img/nature-sprite-2.png);
-webkit-mask-size: 7100% 100%;
mask-size: 7100% 100%;
-webkit-animation: mask-play 1.4s steps(70) forwards;
animation: mask-play 1.4s steps(70) forwards;
}
最后定义动画的关键帧:
@-webkit-keyframes mask-play {
from {
-webkit-mask-position: 0% 0;
mask-position: 0% 0;
}
to {
-webkit-mask-position: 100% 0;
mask-position: 100% 0;
}
}@keyframes mask-play {
from {
-webkit-mask-position: 0% 0;
mask-position: 0% 0;
}
to {
-webkit-mask-position: 100% 0;
mask-position: 100% 0;
}
}
到这里,我们现在用了具有结构和样式的幻灯片了,接下来是让它更加具有实用性!

The JavaScript
在这个例子中用到了 zepto.js ,它是一个非常轻量级的JavaScript 框架类似于jQuery。
最开始是声明所有的变量,设置持续时间和其他需要的元素。接下来初始化事件,获取当前幻灯片和下一张幻灯片,设置正确的z-index。
function Slider() {
// Durations
this.durations = {
auto: 5000,
slide: 1400
};
// DOM
this.dom = {
wrapper: null,
container: null,
project: null,
current: null,
next: null,
arrow: null
};
// Misc stuff
this.length = 0;
this.current = 0;
this.next = 0;
this.isAuto = true;
this.working = false;
this.dom.wrapper = $('.page-view');
this.dom.project = this.dom.wrapper.find('.project');
this.dom.arrow = this.dom.wrapper.find('.arrow');
this.length = this.dom.project.length;
this.init();
this.events();
this.auto = setInterval(this.updateNext.bind(this), this.durations.auto);










