【发布时间】:2019-06-05 09:19:02
【问题描述】:
我已经制作了这个加载占位符 css 动画。在白色背景上它看起来是正确的,因为动画/移动渐变是白色的,不透明度为 20%。
但是,有时占位符将位于不同颜色的背景上,并且移动部分也会在背景上变得可见,而不仅仅是在颜色变暗的情况下(这是不希望的)。请看下面的sn-p:
.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-avatar 和 ph-col-xx)上呈现?
这是如何实现的?
【问题讨论】:
-
效果相似但思路不同:stackoverflow.com/a/55710038/8620333
-
@TemaniAfif 对我来说似乎是重复的,即使最初的问题不是同一个问题。最终的结果需要相同的方法才能完全轻松地工作。