Vue+css实现扫描动画效果(使用@keyframes scan)

实现效果

扫描效果

参考链接

MDN Web Docs: CSS Animations

关键代码

示例代码

html 复制代码
 <div class="scanner-container">
      <div class="scanner-line"></div>
      <div class="scanner-icon">📷</div>
      <p>Scan material barcode to map to this box</p>
      <el-input v-model="form.boxStr" @keyup.enter.native="getPosNameChange()" />
</div>
css 复制代码
.scanner-container {
  border-radius: 4px;
  padding: 16px;
  text-align: center;
  margin: 16px 0;
  position: relative;
  min-height: 180px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  ::v-deep .el-input--small .el-input__inner {
    background-color: #363838;
    color: #e0e0e0;
    height: 55px;
    line-height: 55px;
  }
  p {
    font-size: 14px;
    color: #e0e0e0;
    margin-bottom: 16px;
  }
}

.scanner-line {
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #5c6bc0;
  top: 50%;
  left: 0;
  animation: scan 2s linear infinite;
  z-index: 1;
}

@keyframes scan {
  0% {
    top: 25%;
  }
  50% {
    top: 75%;
  }
  100% {
    top: 25%;
  }
}

常见问题及解决方法

动画不生效:

确保CSS选择器正确,能够选中需要应用动画的元素。

检查@keyframes定义是否正确,关键帧的百分比和样式是否正确。

确保元素的position属性设置为relativeabsolute,以便动画能够正确应用。 动画性能问题:

尽量避免在动画中使用复杂的CSS属性,如box-shadowborder-radius等。

使用will-change属性来提示浏览器提前优化动画元素

相关推荐
m0_679927205 分钟前
练习小项目7:天气状态切换器
前端·javascript·css·html
OK_boom18 分钟前
React-改变当前页class默认的样式
前端·javascript·react.js
Jason秀啊21 分钟前
项目中使用到了多个UI组件库,也使用了Tailwindcss,如何确保新开发的组件样式隔离?
css·tailwindcss·component·组件开发
哎呦你好22 分钟前
【background】CSS 背景全解析:从基础属性到视觉魔法
前端·css·人工智能·python
yuanyxh1 小时前
commonmark.js 源码阅读(二) - Inline Parser
前端·javascript·html
韩明君1 小时前
前端学习笔记——Promis.All
前端·笔记·学习
斯~内克1 小时前
Vite + Vue 工程中,为什么需要关注 `postcss.config.ts`?
前端·vue.js·postcss
蜗牛快跑2131 小时前
使用 Cursor 从 0 到 1 开发一个全栈 chatbox 项目
前端·人工智能·ai·ai编程
Python涛哥2 小时前
前端流行框架Vue3教程:24.动态组件
前端·javascript·vue.js
劲爽小猴头2 小时前
HTML5快速入门-表单&实用标签
前端·html·html5