简体   繁体   English

Box-shadow 不适用于元素,如何解决?

[英]Box-shadow is not applied to element, how to fix that?

I have three bars of player status, ie health, experience, and mana.我有三个玩家状态栏,即生命值、经验值和法力值。 Health and Mana bars have a normal size and exp bar is minimized.健康和法力条具有正常大小,并且经验条最小化。

I apply box-shadow for each of them, styles are almost the same for all bars, however box shadow is not displayed for minimized bar.我为它们中的每一个应用了 box-shadow,styles 对于所有条形几乎都相同,但是对于最小化的条形不会显示框阴影。 I tried to fix that and noticed that when I change spread radius to positive value, box shadow appears.我试图解决这个问题,并注意到当我将传播半径更改为正值时,会出现框阴影。 If the spread radius is negative, box shadow is not displayed at all.如果散布半径为负,则根本不显示框阴影。

The code of PARENT element looks this way: PARENT 元素的代码如下所示:

            <div className={styles.barContainer}>
              <Bar
                color={healthBar.color}
                height={healthBar.height}
                currentValue={healthCurrent}
                maxValue={healthMax}
              />
            </div>
            <div className={classNames(styles.barContainer, styles.barContainerSmall)}>
              <Bar
                size="small"
                color={expBar.color}
                height={expBar.height}
                currentValue={expCurrent}
                maxValue={expMax}
              />
            </div>
            <div className={styles.barContainer}>
              <Bar color={manaBar.color} height={manaBar.height} currentValue={10} maxValue={10} />
            </div>

The Bar component looks this way: Bar 组件看起来是这样的:

      <div
        className={classNames(styles.bar, {
          [styles.smallBar]: size === 'small',
        })}
      >
        <div
          className={styles.barInner}
          style={{
            backgroundColor: color,
            width: `${Math.min(widthPercentage, 100)}%`,
          }}
        />
        {size !== 'small' && (
          <span className={styles.text} style={{ fontSize }}>
            <span className={styles.biggerNumber}>{currentValue}</span>/<span>{maxValue}</span>
          </span>
        )}
      </div>

And styles for the Bar:和 styles 为酒吧:

    .bar {
  position: relative;
  background: rgba(255, 255, 255, 0.25);
  border-radius: 5px;
  height: 10px;
}

.smallBar {
  height: 5px;
}

.barInner {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  border-radius: 4px;
  background: linear-gradient(to right, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.5));
  transition: 0.4s linear;
  transition-property: width;
  animation: progressAnimation 0.5s;
  box-shadow: 0 4px 4px -4px rgba(0, 0, 0, 0.75);
}

.text {
  position: absolute;
  top: -3px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  /** TODO: Change font color depending on contrast: https://css-tricks.com/reverse-text-color-mix-blend-mode/ */
  color: #fff;

}

.text .biggerNumber {
  font-size: 20px;
  font-weight: bolder;
}

Based on MDN docs , spread radius has the effect of growing or shrinking box shadows relative to the element's dimensions:基于MDN 文档,传播半径具有相对于元素尺寸增大或缩小框阴影的效果:

Positive values will cause the shadow to expand and grow bigger, negative values will cause the shadow to shrink.正值会导致阴影扩大并变大,负值会导致阴影缩小。

Since you're using a spread radius of -4px , this means that the edges of the shadow will be inset 4px from the edges of the element.由于您使用的扩展半径为-4px ,这意味着阴影的边缘将从元素的边缘插入 4px 。 If your element has insufficient dimensions (ie width <8px or height <8px, as 4px * 2 = 8px ), the box shadow will not show since it will have a dimension of <0px in either axes.如果您的元素尺寸不足(即宽度 <8px 或高度 <8px,如4px * 2 = 8px ),则不会显示框阴影,因为它在任一轴上的尺寸都为 <0px。

Here is a proof-of-concept example, using the same box-shadow properties (but I increased the y offset to make it more obvious).这是一个概念验证示例,使用相同的box-shadow属性(但我增加了 y 偏移以使其更明显)。 Notice that the box shadow will only be rendered at elements of width >=9px:请注意,盒子阴影只会在宽度 >=9px 的元素处呈现:

 html, body { padding: 0; margin: 0; } #stage { display: flex; align-items: center; justify-content: space-around; height: 100vh; }.box { display: flex; align-items: center; justify-content: center; width: 100px; height: 100px; background-color: #ddd; box-shadow: 0 100px 4px -4px rgba(0, 0, 0, 0.75); }
 <div id="stage"> <div class="box"></div> <div class="box" style="width: 80px;"></div> <div class="box" style="width: 60px;"></div> <div class="box" style="width: 40px;"></div> <div class="box" style="width: 20px;"></div> <div class="box" style="width: 10px;"></div> <div class="box" style="width: 9px;"></div> <:-- No box shadow expected --> <div class="box" style="width; 8px;"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM