首页 > 解决方案 > 使 CSS Shimmer Effect 工作于已加载的图像

问题描述

我有以下微光效果在元素上使用时效果很好,p但它不适用于任何元素divimg. 那么,我应该进行哪些修改以确保微光效果在任何元素上发挥作用。

片段如下

 .shimmer {
      display: inline-block;
      color:grey;
      background: #acacac -webkit-gradient(linear, 100% 0, 0 0, from(#acacac), color-stop(0.5, #ffffff), to(#acacac));
      background-position: -50rem top; /*50px*/
      background-repeat: no-repeat;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      -webkit-animation-name: shimmer;
      -webkit-animation-duration: 2.2s;
      -webkit-animation-iteration-count: infinite;
      -webkit-background-size: 50rem 100%; /*50px*/
      font-size: 90px;
    }
    
    @-webkit-keyframes shimmer {
        0% {
            background-position: -50rem top; /*50px*/
        }
        70% {
            background-position: 12.5rem top; /*200px*/
        }
        100% {
            background-position: 12.5rem top; /*200px*/
        }
    }
<div>
<p class="shimmer">Shimmering Text</p>

<div>
<img src="https://i.stack.imgur.com/MeQxk.png" width=100 height=100 alt="Image Should Shimmer.Unfortunately not working "/>
</div>
</div>

谢谢

标签: css

解决方案


使用面具

.shimmer {
  color: grey;
  display:inline-block;
  -webkit-mask:linear-gradient(-60deg,#000 30%,#0005,#000 70%) right/300% 100%;
  background-repeat: no-repeat;
  animation: shimmer 2.5s infinite;
  font-size: 50px;
  max-width:200px;
}

@keyframes shimmer {
  100% {-webkit-mask-position:left}
}
<p class="shimmer">Shimmering Text</p>
<img src="https://i.stack.imgur.com/MeQxk.png"class="shimmer" />


推荐阅读