Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active December 2, 2017 07:26
Show Gist options
  • Save black-black-cat/2caa581e5c353f1c075c9fa7b667badb to your computer and use it in GitHub Desktop.
Save black-black-cat/2caa581e5c353f1c075c9fa7b667badb to your computer and use it in GitHub Desktop.
样式名加前缀
function vendor(style) {
if (!styles[style]) {
prefixes.map(function (prefix) {
return prefix
? prefix + style[0].toUpperCase() + style.slice(1)
: style
}).some(function (prefixedStyle) {
if (elementStyle[prefixedStyle] !== void 0) {
styles[style] = prefixedStyle
return true
}
})
}
return styles[style] || null
}
var styles = {}
var prefixedEvents = {}
var prefixes = ['', 'Webkit', 'Moz', 'O', 'MS']
var evPrefixes = ['', 'webkit', 'moz', 'o', 'MS']
var elementStyle = document.createElement('div').style
var transitionEnds = {
transition: 'transitionend',
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
OTransition: 'oTransitionEnd'
}
var animationEnds = {
animation: 'animationend',
WebkitAnimation: 'webkitAnimationEnd',
MozAnimation: 'animationend',
OAnimation: 'oAnimationEnd'
}
function transitionEnd() {
return transitionEnds[vendor('transition')] || transitionEnds.transition
}
function animationEnd() {
return animationEnds[vendor('animation')] || animationEnds.animation
}
/**
* type event type, animationEnd, transitionStart, etc.
*
* @return {String} prefixed event type
*/
function basePrefixEvent(type) {
var result = type.toLowerCase()
evPrefixes.map(function (pfx) {
if (!pfx) {
return type.toLowerCase()
}
return pfx + type[0].toUpperCase() + type.slice(1)
}).some(function (prefixed) {
var style = vendor( type.replace(/[A-Z][a-z]*$/, '') )
var re = new RegExp('^' + style, 'i')
var isMatch = re.test(prefixed)
if (isMatch) {
result = prefixed
return true
}
})
return result
}
function prefixEvent(type) {
var result = prefixedEvents[type]
if (!result) {
prefixedEvents[type] = basePrefixEvent(type)
}
return prefixedEvents[type]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment