function getStyleValue(styleVal) {
  let value = styleVal
  if (typeof styleVal !== 'number') {
    const isPxReg = /^(\d+(px))$/g
    const isPercentageReg = /(\d{1,2}|100)%$/g
    if (isPxReg.test(styleVal)) {
      value = styleVal.split('px')[0]
    } else if (isPercentageReg.test(styleVal)) {
      value = (styleVal.split('%')[0] / 100) * getClientHeight()
    }
  }
  return value + 'px'
}


function getClientHeight() {
  return document.documentElement.clientHeight === 0
    ? document.body.clientHeight
  : document.documentElement.clientHeight
}

getStyleValue这个函数可以把number类型的参数、或者string类型的参数转化成统一px为单位的长度

最后修改:2022 年 04 月 15 日 04 : 52 PM
如果觉得我的文章对你有用,请随意赞赏