3 Star 56 Fork 13

Yaohaixiao/dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
getStyles.js 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
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) || !isArray(attrs)) {
return props
}
props = getComputedStyle(el)
keys = Object.keys(props)
/* istanbul ignore else */
if (isArray(attrs)) {
keys = attrs
if (attrs.length < 1) {
return null
}
}
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/yaohaixiao/dom.js.git
git@gitee.com:yaohaixiao/dom.js.git
yaohaixiao
dom.js
dom.js
main

搜索帮助