CSS实现的扫光效果组件


theme: lilsnake

图片和内容如有侵权,及时与我联系~

详细内容与注释:

CSS实现的扫光效果组件

代码

技术栈与框架

Vue3

CSS

扫光效果的原理

扫光效果的原理就是从左到右无限循环的一个位移动画

实现方式

适配文字扫光效果的css

css 复制代码
.shark-box { /* 文字样式 */
    font-size: 60px; 
    font-weight: normal; 
    /* 扫光样式-文字 */ 
    -webkit-text-fill-color: transparent; 
    background: linear-gradient( 45deg, rgba(255, 255, 255, 0) 40%, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0) 60% ) -100% / 50% no-repeat currentColor; 
    -webkit-background-clip: text; 
    animation: shark-box 2s infinite; } 
/* 文字 */ 
@keyframes shark-box { 
    from { background-position: -100%; } 
    to { background-position: 200%; } 
}
效果图

适配图片扫光效果的css

css 复制代码
/* 图片扫光样式 */ 
.card { 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    img { 
    display: block; border-radius: 8px; 
    } 
} 
/* 伪元素实现图片扫光 */ 
.shark-img { 
    //定位图片,超出隐藏 
    position: relative; 
    overflow: hidden; 
    img { 
        object-fit: cover; 
    } 
    &::after { 
        content: ""; 
        position: absolute; 
        inset: -20%; 
        background: linear-gradient( 45deg, rgba(255, 255, 255, 0) 40%, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0) 60% ); 
        animation: shark-img 2s infinite; transform: translateX(-100%); 
    } 
} 
/* 图片 */ 
@keyframes shark-img {
    to { transform: translateX(100%); } 
}
效果图

针对不规则样式

html 复制代码
 <!-- 针对不规则图形,增加 -webkit-mask 配置 -->
    <div
      class="shark-img card"
      v-else-if="props.src"
      :style="
        props.src.includes('png')
          ? {
              mask: `url(${props.src}) no-repeat center / cover`,
              '-webkit-mask': `url(${props.src}) no-repeat center / cover`,
            }
          : {}
      "
    >
      <img :src="props.src" alt="未找到图片" />
    </div>
效果图

组件代码

https://code.juejin.cn/pen/7394752941250314250

相关推荐
IT_陈寒15 分钟前
React useEffect依赖数组把我坑惨了,这次搞明白了
前端·人工智能·后端
gcw102415 分钟前
Cron专业工具:定时任务与开发办公的一站式在线工具箱
java·前端·数据库·工具·crontab·cron·spring cron
环境栈笔记21 分钟前
AI 浏览器任务失败怎么排查:Context、Session、Page State 和 Task Log 检查清单
前端·人工智能·后端·自动化
阳光是sunny21 分钟前
别再让 AI 瞎找代码了!CodeGraph 这工具能让它秒懂你的项目
前端·后端·ai编程
tuddy78946436 分钟前
快速创建常见的数据模型、CRUD 接口、配置文件等重复性结构
前端
小流苏生1 小时前
这是一篇关于忘记的文章
前端·程序员·ai编程
2601_963771371 小时前
Scaling Podcast RSS Feeds and Audio Streaming on WordPress Servers
前端·windows·php
名字还没想好☜3 小时前
Next.js App Router 数据获取:fetch 缓存、revalidate 与 dynamic 一次讲清
前端
成都渲染101云渲染66668 小时前
如何在3ds Max中实现更快、更高质量的渲染
前端·javascript·人工智能
闲猫9 小时前
Spring AI 对接Deepseek ChatModel 聊天对话
java·前端·spring