3 Star 54 Fork 14

Yaohaixiao / dom.js

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
getStyles.js 1.03 KB
Copy Edit Raw Blame History
import toCamel from './utils/string/toCamel'
import isUndefined from './utils/types/isUndefined'
import isArray from './utils/types/isArray'
import isElement from './isElement'
/**
* 获取 DOM 元素 attrs 数组中指定的多个 CSS 样式值
* ========================================================================
* @param {HTMLElement} el
* @param {Array} [attrs]
* @returns {Object|null}
*/
const getStyles = (el, attrs = []) => {
const styles = {}
let props = null
let keys
if (!isElement(el)) {
return props
}
props = getComputedStyle(el)
keys = Object.keys(props)
/* istanbul ignore else */
if (isArray(attrs) && attrs.length > 0) {
keys = attrs
}
keys.forEach((attr) => {
const prop = toCamel(attr)
const isNumber = /\d+/g
const value = props[prop]
/* istanbul ignore else */
if (!isUndefined(prop) && !isNumber.test(prop) && !isUndefined(value)) {
styles[prop] = props[prop]
}
})
return styles
}
export default getStyles
JavaScript
1
https://gitee.com/yaohaixiao/dom.js.git
git@gitee.com:yaohaixiao/dom.js.git
yaohaixiao
dom.js
dom.js
main

Search

53164aa7 5694891 3bd8fe86 5694891