



时间:2017-02-17 关注公众号 来源:https://segmentfault.com/
页面布局
1 | <div class = "panorama" ></div> |
基础样式
首先定义一些基本的样式和动画
01 | .panorama { |
02 | width: 300px; |
03 | height: 300px; |
04 | background-image: url(http: //7vilbi.com1.z0.glb.clouddn.com/blog/6608185829213862083.jpg); |
05 | background-size: auto 100%; |
06 | cursor: pointer; |
07 | animation: panorama 10s linear infinite
alternate; |
08 | } |
09 |
10 | @keyframes panorama { |
11 | to { |
12 | background-position: 100% 0; |
13 | } |
14 | } |
background-size: auto 100%; 这段代码的意思是让图片的高等于容器的高,并且水平方向自动,即图片最左边贴着容器左侧。
执行动画的流程是:周而复始、往复交替、线性并且时间周期是10s。
手动控制动画执行
到这里为止,当我们打开该网页后,立马会出现一张图片来回水平滑动的效果。但是这样的话,访客可能会被动画吸引而忽略了真正的内容。
我们的要求是当鼠标悬浮于图片时才让它动起来,我们当然可以很简单的实现这个效果。
删除之前的animation,添加以下样式。
1 | .panorama:hover, |
2 | .panorama:focus { |
3 | animation: panorama 10s linear infinite
alternate; |
4 | } |
现在的效果是:鼠标移入图片,图片开始水平来回滑动。
动画的优化
虽然效果达到了,但是你会发现,当鼠标移出图片,图片立刻回到初始位置。
对于我们来说,这有点突然,如何记录图片当前的位置并且当鼠标移入时继续执行动画呢?
我们可以依靠这个属性animation-play-state: paused | running,它表示动画的两个状态:暂停和运行。
完整css代码
01 | .panorama { |
02 | width: 300px; |
03 | height: 300px; |
04 | background-image: url(http: //7vilbi.com1.z0.glb.clouddn.com/blog/6608185829213862083.jpg); |
05 | background-size: auto 100%; |
06 | cursor: pointer; |
07 | animation: panorama 10s linear infinite
alternate; |
08 | animation-play-state: paused; |
09 | } |
10 |
11 | .panorama:hover, |
12 | .panorama:focus { |
13 | animation-play-state: running; |
14 | } |
15 |
16 | @keyframes panorama { |
17 | to { |
18 | background-position: 100% 0; |
19 | } |
20 | } |
下一篇:愉此一生电视剧第7集地址
文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)
电话:13918309914
QQ:1967830372
邮箱:rjfawu@163.com