栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

怎么停止CSS3的动画

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

怎么停止CSS3的动画

我们在移动端一般使用zepto框架,与其说zepto是jquery的轻量级替代版,不如说是html5替代版
我们在js中会用到animate方法执行动画,这个家伙可是真资格的动画,完全是css一点点变化的!
而zepto则不然,使用的是HTML5/CSS3的方案,而CSS相关是不保存元素状态值的,也没办法保存,所以停止动画就成了一大问题
我们今天就一起来讨论下相关停止动画的方案,反正没有什么好的......

CSS3动画原理

在现有浏览器中,一般有两种模式(我只知道两种):
一种是js动画,他是动态改写元素的style实现动画,所以任意时间想停止动画都是没问题的,因为我们可以获得各个阶段的状态值
另一种就是CSS3动画了,至于CSS3动画的原理,我不知道但是可以说一点的就是——见代码:

 1 
 2 
 3 
 4     
 5  
 6 
 7 
 8     
10     
11 
12 
13 
24 


zepto的animate事实上马上就改变了style的值,所以我们在里面看到了left为100px,虽然他正在运动
而他动画的实现事实上使用的是CSS3的transition动画属性,我们这里来看看zepto的源码:

 1 var prefix = '', eventPrefix, endEventName, endAnimationName,
 2 vendors = { Webkit: 'webkit', Moz: '', O: 'o', ms: 'MS' },
 3 document = window.document, testEl = document.createElement('div'),
 4 supportedTransforms = /^((translate|rotate|scale)(X|Y|Z|3d)?|matrix(3d)?|perspective|skew(X|Y)?)$/i,
 5 transform,
 6 transitionProperty, transitionDuration, transitionTiming,
 7 animationName, animationDuration, animationTiming,
 8 cssReset = {}
 9 
10 function dasherize(str) { return downcase(str.replace(/([a-z])([A-Z])/, '$1-$2')) }
11 function downcase(str) { return str.toLowerCase() }
12 function normalizeEvent(name) { return eventPrefix ? eventPrefix + name : downcase(name) }
13 
14 $.each(vendors, function (vendor, event) {
15     if (testEl.style[vendor + 'TransitionProperty'] !== undefined) {
16         prefix = '-' + downcase(vendor) + '-'
17         eventPrefix = event
18         return false
19     }
20 })
21 
22 transform = prefix + 'transform'
23 cssReset[transitionProperty = prefix + 'transition-property'] =
24 cssReset[transitionDuration = prefix + 'transition-duration'] =
25 cssReset[transitionTiming = prefix + 'transition-timing-function'] =
26 cssReset[animationName = prefix + 'animation-name'] =
27 cssReset[animationDuration = prefix + 'animation-duration'] =
28 cssReset[animationTiming = prefix + 'animation-timing-function'] = ''
29 
30 $.fx = {
31     off: (eventPrefix === undefined && testEl.style.transitionProperty === undefined),
32     speeds: { _default: 400, fast: 200, slow: 600 },
33     cssPrefix: prefix,
34     transitionEnd: normalizeEvent('TransitionEnd'),
35     animationEnd: normalizeEvent('AnimationEnd')
36 }
37 
38 $.fn.animate = function (properties, duration, ease, callback) {
39     if ($.isPlainObject(duration))
40         ease = duration.easing, callback = duration.complete, duration = duration.duration
41     if (duration) duration = (typeof duration == 'number' ? duration :
42                 ($.fx.speeds[duration] || $.fx.speeds._default)) / 1000
43     return this.anim(properties, duration, ease, callback)
44 }
45 
46 $.fn.anim = function (properties, duration, ease, callback) {
47     var key, cssValues = {}, cssProperties, transforms = '',
48     that = this, wrappedCallback, endEvent = $.fx.transitionEnd
49 
50     if (duration === undefined) duration = 0.4
51     if ($.fx.off) duration = 0
52 
53     if (typeof properties == 'string') {
54         // keyframe animation
55         cssValues[animationName] = properties
56         cssValues[animationDuration] = duration + 's'
57         cssValues[animationTiming] = (ease || 'linear')
58         endEvent = $.fx.animationEnd
59     } else {
60         cssProperties = []
61         // CSS transitions
62         for (key in properties)
63             if (supportedTransforms.test(key)) transforms += key + '(' + properties[key] + ') '
64             else cssValues[key] = properties[key], cssProperties.push(dasherize(key))
65 
66         if (transforms) cssValues[transform] = transforms, cssProperties.push(transform)
67         if (duration > 0 && typeof properties === 'object') {
68             cssValues[transitionProperty] = cssProperties.join(', ')
69             cssValues[transitionDuration] = duration + 's'
70             cssValues[transitionTiming] = (ease || 'linear')
71         }
72     }
73 
74     wrappedCallback = function (event) {
75         if (typeof event !== 'undefined') {
76             if (event.target !== event.currentTarget) return // makes sure the event didn't bubble from "below"
77             $(event.target).unbind(endEvent, wrappedCallback)
78         }
79         $(this).css(cssReset)
80         callback && callback.call(this)
81     }
82     if (duration > 0) this.bind(endEvent, wrappedCallback)
83 
84     // trigger page reflow so new elements can animate
85     this.size() && this.get(0).clientLeft
86 
87     this.css(cssValues)
88 
89     if (duration <= 0) setTimeout(function () {
90         that.each(function () { wrappedCallback.call(this) })
91     }, 0)
92 
93     return this
94 }

最后实际上是执行anim实现我们的动画,大家注意看这里
$(this).css(cssReset)
this.css(cssValues)
他事实上搞了个先设置动画属性,再给style属性给元素,所以会产生动画
到此,zepto实现动画原理我们大概知道了,现在问题就是如何停止他了,所以我们继续往下看

如何停止动画

我们先看看这个东西:

1 
 2 
 3 
 4     
 5     
 6 
 7 
 8     
10     
11 
12 
13 
24 

其中虽然left一开始就变了,我们惊奇的发现,offset这个家伙居然保存了我们的状态!!!
我和我的小伙伴都惊呆了,因为我之前一直以为什么状态都不能获得,于是我们为他加上mousedown事件,各位运动时候点击试试
我们这里这样干了下:

 1 
 2 
 3 
 4     
 5     
 6 
 7 
 8     
10     
11 
12 
13 
30 

于是我们发现,动画停止了,亲!他真的停止了!!!
PS:因为项目过程中,我这里要模仿类似iscroll的滚动效果,所以使用的最多的就是top或者translate3d(0, 0, 0)这种东西。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/242737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号