css 计算元素的方框阴影宽度

fjnneemd  于 2023-01-18  发布在  其他
关注(0)|答案(3)|浏览(134)

你可能知道box-shadow不是box-model的一部分,那么计算box-shadow的宽度的好方法是什么呢?
更新:我需要知道一个元素的总宽度,包括阴影宽度。

wsxa1bj1

wsxa1bj11#

你可以简单地加上一个等于阴影的边距。例如:

box-shadow: 0 0 10px #008800;
margin: 10px;

如果您在框阴影上使用X和Y偏移,请将该值添加到阴影的长度。例如:

box-shadow: 5px 5px 10px #080;
margin: 5px 15px 15px 5px;

这里的偏移量是5px,加上10px的长度。2在扩展的情况下,我们可以继续增加边距值来考虑这个因素。

box-shadow: 5px 5px 10px 7px #080;
margin: 12px 21px 21px 12px;

使用页边距可以防止阴影与页面上的其他对象重叠。
确切的宽度会因浏览器而异。每个浏览器渲染的阴影都不一样。如果我必须给予一个艰难的计算对象,我猜它会是这样的东西(css属性供参考)

box-shadow: h-shadow v-shadow blur spread color;

箱模型偏移为

top = (spread - v_shadow + 0.5*blur)
right = (spread + h_shadow + 0.5*blur)
bottom = (spread + v_shadow + 0.5*blur)
left = (spread - h_shadow + 0.5*blur)

模糊系数是一个估计值,可能需要稍微调整。我个人不喜欢使用偏移量,但在这里显示它将被使用
这里有一个jsfiddle来查看它在http://jsfiddle.net/YvqZV/4/中运行情况

b5lpy0ml

b5lpy0ml2#

只是通过创建一个完整的函数来扩展@samuel.molinski的答案,该函数接受一个方框阴影并返回宽度。

function getBoxShadowWidths(boxShadow) {
  // not supporting multiple box shadow declarations for now
  if ((boxShadow.match(/(rgb|#)/g) || []).length > 1) {
    return false;
  }

  const regEx = /(\d(?=(px|\s)))/g;
  const matches = [];

  // box-shadow can have anywhere from 2-4 values, including horizontal offset, vertical offset,
  // blur, and spread. Below finds each one and pushes it into an array (regEx.exec when used in succession
  // with a global regex will find each match.
  let match = regEx.exec(boxShadow);
  while (match != null) {
    matches.push(match[0]);
    match = regEx.exec(boxShadow);
  }

  // default blur & spread to zero px if not found by the regex
  const [hOffset = 0, vOffset = 0, blur = 0, spread = 0] = matches.map(parseFloat);

  // calculate approximate widths by the distance taken up by each side of the box shadow after normalizing
  // the offsets with the spread and accounting for the added distance resulting from the blur
  // See https://msdn.microsoft.com/en-us/hh867550.aspx - "the blurring effect should approximate the
  // Gaussian blur with a standard deviation equal to HALF of the blur radius"
  const top = spread - vOffset + 0.5 * blur;
  const right = spread + hOffset + 0.5 * blur;
  const bottom = spread + vOffset + 0.5 * blur;
  const left = spread - hOffset + 0.5 * blur;

  return { top, right, bottom, left };
}
h7appiyu

h7appiyu3#

感谢@Joey提供的函数。我添加了对多个值的支持:

function getBoxShadowWidths(boxShadowValues) {
  const regEx = /(\d(?=(px|\s)))/g
  const widths = { top: 0, right: 0, bottom: 0, left: 0 }

  boxShadowValues.split(/\s*,\s*/).forEach(boxShadowValue => {
    const matches = []

    // box-shadow can have anywhere from 2-4 values, including horizontal offset, vertical offset, blur, and spread.
    // Below finds each one and pushes it into an array (regEx.exec when used in succession with a global regex will find each match.
    let match = regEx.exec(boxShadowValue)
    while (match != null) {
      matches.push(match[0])
      match = regEx.exec(boxShadowValue)
    }

    // default blur & spread to zero px if not found by the regex
    const [hOffset = 0, vOffset = 0, blur = 0, spread = 0] = matches.map(parseFloat)

    // calculate approximate widths by the distance taken up by each side of the box shadow after normalizing
    // the offsets with the spread and accounting for the added distance resulting from the blur
    // See https://msdn.microsoft.com/en-us/hh867550.aspx - "the blurring effect should approximate the
    // Gaussian blur with a standard deviation equal to HALF of the blur radius"
    const actualWidths = {
      top: spread - vOffset + 0.5 * blur,
      right: spread + hOffset + 0.5 * blur,
      bottom: spread + vOffset + 0.5 * blur,
      left: spread - hOffset + 0.5 * blur,
    }

    Object.keys(actualWidths).forEach(side => {
      widths[side] = Math.max(widths[side], actualWidths[side])
    })
  })

  return widths
}

相关问题