Skip to content

Instantly share code, notes, and snippets.

@chunpu
Last active August 29, 2015 14:04
Show Gist options
  • Save chunpu/4d3a836d53410471753d to your computer and use it in GitHub Desktop.
Save chunpu/4d3a836d53410471753d to your computer and use it in GitHub Desktop.
jQuery css3 animate, if browser support css3 transition, use it. (ps: however, the performance is not obvious improved, and it is not in promise queue, so just for simplest situation)
!function($, doc) {
if (!$ || !$.fn) return
var css3 = function() {
var style = doc.body.style
var test = ['transition', 'webkitTransition', 'MozTransition', 'msTransition']
for (var i = 0, x; x = test[i++];) {
if (x in style) {
return x
}
}
}()
if (!css3) return // do nothing
$.fn.animate = function(prop, speed, easing, fn) {
var self = this
var duration = 1000, ease = 'ease',
callback = arguments[arguments.length - 1]
if (typeof speed == 'number')
duration = speed
if (typeof easing == 'string')
ease = easing
if (typeof callback == 'function') {
self.one('transitionend', function(ev) {
self.css(css3, '')
var target = ev.target
callback.call(target)
})
} else {
setTimeout(function() {
self.css(css3, '')
}, duration)
}
return self.css(css3, ['all', duration + 'ms', ease].join(' ')).css(prop)
}
}(window.$, document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment