CSS动画,仅在特定元素上渲染覆盖层

3

我制作了这个加载占位符的CSS动画。在白色背景上,它看起来正确,因为动态/移动的渐变是具有20%不透明度的白色。

然而,有时候占位符会出现在不同颜色的背景上,移动部分也会显示在背景上,而不仅仅是在颜色加深的区域上(这是不希望的)。请参见下面的代码片段:

.ph-item {
  position: relative;
  margin-bottom: 10px;
  overflow: hidden;
  border-radius: 2px;
}

.ph-item,
.ph-item *,
.ph-item ::after,
.ph-item ::before {
  box-sizing: border-box;
}

.ph-item::before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 50%;
  z-index: 1;
  width: 500%;
  margin-left: -250%;
  animation: phAnimation 0.8s linear infinite;
  background: linear-gradient(to right, rgba(255, 255, 255, 0) 46%, rgba(255, 255, 255, 0.35) 50%, rgba(255, 255, 255, 0) 54%) 50% 50%;
}

.ph-item > * {
  padding-right: 10px;
}

.ph-row {
  margin-bottom: 5px;
}

.ph-row div {
  border-radius: 3px;
  height: 10px;
  background-color: rgba(0, 0, 0, 0.2);
}

.ph-row .standard,
.ph-row.big div {
  height: 20px;
  margin-right: 10px;
}


.ph-float-right {
  float: right;
  padding-left: 10px;
  margin-right: auto;
}

.ph-col-2 {
  width: 16.66667%;
  display: inline-block;
}

.ph-col-4 {
  width: 33.33333%;
  display: inline-block;
}

.ph-col-6 {
  width: 50%;
  display: inline-block;
}

.ph-col-8 {
  width: 66.66667%;
  display: inline-block;
}

.ph-col-10 {
  width: 83.33333%;
  display: inline-block;
}

.ph-col-12 {
  width: 100%;
  display: inline-block;
}

.ph-avatar {
  position: relative;
  width: 100%;
  min-width: 50px;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 50%;
  overflow: hidden;
}

.ph-avatar::before {
  content: "";
  display: block;
  padding-top: 100%;
}

@keyframes phAnimation {
  0% {
    transform: translate3d(-30%, 0, 0);
  }

  100% {
    transform: translate3d(30%, 0, 0);
  }
}
<div style="width: 500px; height: 50px; background-color: darkblue; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: white; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>

我的问题是,是否有可能以某种方式遮盖动画,只在变暗的部分(ph-avatarph-col-xx)上呈现?

这是如何实现的?


一个类似的效果但不同的想法:https://dev59.com/wbPma4cB1Zd3GeqPoElB#55710038 - Temani Afif
1
@TemaniAfif 看起来对我来说是重复的,即使最初的问题不是用相同的术语。最终结果需要相同的方法才能完全且轻松地运作。 - G-Cyrillus
2个回答

4

编辑 这实际上是 背景动画性能 的一个副本

另一种方法可以使用带有线性渐变重置的 background-position 的伪元素。 为了保持每个伪元素与渐变的连贯性, background-attachment 将把它们放在一起。通过设置 background-size 也可改进。

以下是该想法的演示。CSS中已注释更新或修改的内容。

.ph-item {
  position: relative;
  margin-bottom: 10px;
  overflow: hidden;
  border-radius: 2px;
}

.ph-item,
.ph-item *,
.ph-item ::after,
.ph-item ::before {
  box-sizing: border-box;
}
   /* selector removed 

.ph-item::before 

       and replaced by : */
.ph-avatar::after,
.ph-row .standard::after,
.ph-row .ph-col-10::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 50%;
  z-index: 1;
  width: 500%;
  margin-left: -250%;
  animation: phAnimation 2s linear infinite;/* duration to set to your needs */
  background: linear-gradient(
      to right,
      rgba(255, 255, 255, 0) 46%,
      rgba(255, 255, 255, 0.35) 50%,
      rgba(255, 255, 255, 0) 54%
    )
    50% 50% 
    rgba(0, 0, 0, 0.2);/* move here & added */
  background-attachment: fixed;/*  added */
  background-size: 1000px auto;/*  added */
}

.ph-item > * {
  padding-right: 10px;
}

.ph-row {
  margin-bottom: 5px;
}

.ph-row div {
  border-radius: 3px;
  height: 10px;
 /*background-color: rgba(0, 0, 0, 0.2); or move animation here from pseudo*/
}

.ph-row .standard,
.ph-row.big div {
  height: 20px;
  margin-right: 10px;
}

.ph-float-right {
  float: right;
  padding-left: 10px;
  margin-right: auto;
}

.ph-col-2 {
  width: 16.66667%;
  display: inline-block;
}

.ph-col-4 {
  width: 33.33333%;
  display: inline-block;
}

.ph-col-6 {
  width: 50%;
  display: inline-block;
  position: relative;/*  added */
  overflow: hidden;/*  added */
}

.ph-col-8 {
  width: 66.66667%;
  display: inline-block;
}

.ph-col-10 {
  width: 83.33333%;
  display: inline-block;
  position: relative;/*  added */
  overflow: hidden;/*  added */
}

.ph-col-12 {
  width: 100%;
  display: inline-block;
}

.ph-avatar {
  position: relative;
  width: 100%;
  min-width: 50px;
  border-radius: 50%;
  overflow: hidden;
}

.ph-avatar::before {
  content: "";
  display: block;
  padding-top: 100%;
}

@keyframes phAnimation {
  0% {
    background-position: -1000px 0;/*  modified */
  }

  100% {
    background-position: 1000px 0;/*  modified */
  }
}
<div style="width: 500px; height: 50px; background-color: darkblue; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: white; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: tomato; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: gold; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: purple; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: turquoise; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: gray; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: teal; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard"></div>
        <div class="ph-col-10"></div>
      </div>
    </div>
  </div>
</div>

注意,背景动画可以直接设置在元素内,伪类只有在具有其他用途时才是必要的。可能的选项https://codepen.io/gc-nomade/pen/byJOJa


嘿,非常感谢。但我不明白这两个问题有什么关系。我正在寻求使用CSS获取视觉结果,而另一个问题是关于一个产品的性能,尽管它与我试图实现的结果非常相似。我认为这个解决方案非常流畅,而且我不担心它的性能,因为我只会展示一个实例,并且仅仅是短暂的时间。非常感谢 :) - Rasmus Puls
@RasmusPuls,这只涉及到使用的方法(主要是background-attachment),性能是额外的,也很好知道;OP也考虑过transform,但没有实现这个想法。他认为Gradient应该相对于屏幕而不是容器,这就是我们在这里的原因;) - G-Cyrillus

2
也许一种选择是掩盖两个SVG图像并动画化掩蔽的属性。我个人不太熟悉这种技术,但也许这可以帮助您:https://tympanus.net/codrops/css_reference/mask-composite/。然而,如果您想在HTML中实现它(我个人也更喜欢这种方法),您可以通过一些诡计实现该效果。

.ph-item {
  position: relative;
  margin-bottom: 10px;
  overflow: hidden;
  border-radius: 2px;
}

.ph-item,
.ph-item *,
.ph-item ::after,
.ph-item ::before {
  box-sizing: border-box;
}

.highlighted {
  position: relative;
  overflow: hidden;
}

.highlighted::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 1;
  width: 100px;
  animation: 1s linear infinite;
  animation-name: phAnimation;
  background: red;
/*   background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.35) 50%, rgba(255, 255, 255, 0) 100%) 50% 50%; */
}

.ph-avatar.highlighted::before {
  animation-name: phAnimationAvatar;
}

.ph-item > * {
  padding-right: 10px;
}

.ph-row {
  margin-bottom: 5px;
}

.ph-row div {
  border-radius: 3px;
  height: 10px;
  background-color: rgba(0, 0, 0, 0.2);
}

.ph-row .standard,
.ph-row.big div {
  height: 20px;
  margin-right: 10px;
}


.ph-float-right {
  float: right;
  padding-left: 10px;
  margin-right: auto;
}

.ph-col-2 {
  width: 16.66667%;
  display: inline-block;
}

.ph-col-4 {
  width: 33.33333%;
  display: inline-block;
}

.ph-col-6 {
  width: 50%;
  display: inline-block;
}

.ph-col-8 {
  width: 66.66667%;
  display: inline-block;
}

.ph-col-10 {
  width: 83.33333%;
  display: inline-block;
}

.ph-col-12 {
  width: 100%;
  display: inline-block;
}

.ph-avatar {
  position: relative;
  width: 100%;
  min-width: 50px;
  height: 50px;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 50%;
  overflow: hidden;
}


@keyframes phAnimationAvatar {
  0% {
    transform: translate3d(-100px, 0, 0);
  }
  
  10% {
    transform: translate3d(-50px, 0, 0);
  }
  
  20% {
    transform: translate3d(0px, 0, 0);
  }
  
  30% {
    transform: translate3d(50px, 0, 0);
  }

  100% {
    transform: translate3d(100px, 0, 0);
  }
}

@keyframes phAnimation {
  0% {
    transform: translate3d(-100px, 0, 0);
  }
  
  11% {
    transform: translate3d(-100px, 0, 0);
  }

  50% {
    transform: translate3d(85px, 0, 0);
  }
  
  100% {
    transform: translate3d(335px, 0, 0);
  }
}
<div style="width: 500px; height: 50px; background-color: darkblue; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar highlighted"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard highlighted"></div>
        <div class="ph-col-10 highlighted"></div>
      </div>
    </div>
  </div>
</div>
<div style="width: 500px; height: 50px; background-color: white; padding: 20px;">
  <div class="ph-item" style="max-width: 360px;">
    <div class="ph-col-2">
      <div class="ph-avatar highlighted"></div>
    </div>
    <div class="ph-col-10 ph-float-right">
      <div class="ph-row">
        <div class="ph-col-6 standard highlighted"></div>
        <div class="ph-col-10 highlighted"></div>
      </div>
    </div>
  </div>
</div>

缺点是您需要为不同的占位符重新校准动画,但仍然比为每个占位符元素使用静态SVG要好。
编辑:提醒一下,Safari似乎存在呈现问题,但Chrome和Firefox可以正常工作。

1
是的,似乎Safari忽略了before元素上的边框半径/圆形。除此之外,它看起来符合要求。唯一的缺点是它不是非常通用。如果我需要在其他地方放置一个灰色的div,那么它也需要自己独特的动画来对齐滚动渐变元素的流程。这就是为什么我希望有一种掩蔽属性,可以使像素仅在具有此属性的元素上呈现,而动画在父级中运行。我是CSS的新手,但在大多数图像编辑软件(如Photoshop)中,这很容易实现。 - Rasmus Puls
1
是的,我知道你的意思。我曾经测试过在HTML代码中呈现掩蔽的SVG(这样以后就容易对其组件进行动画化/修改),但浏览器似乎无法识别该掩蔽。不幸的是,CSS掩蔽属性似乎只能与外部图像一起使用,所以我不知道是否可以将HTML中的SVG添加到其中。 - Eugene Ghanizadeh Khoub
有趣。我想知道所有这些占位符(许多网站使用它们,包括Facebook等)是否在不同的背景颜色下看起来都很傻,或者他们已经解决了这个问题。也许另一种方法是改变覆盖元素的颜色以匹配背景颜色,而不是强制使用白色半透明覆盖层。 - Rasmus Puls

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接