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属性设置为relative或absolute,以便动画能够正确应用。 动画性能问题:

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

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

相关推荐
时间的情敌1 小时前
Cesium 加载 3D Tiles 倾斜摄影、压平、样式设置(Vue3 实战)
vue.js·3d·cesium
IT_陈寒1 小时前
Java开发实战:从入门到精通
前端·人工智能·后端
风骏时光牛马2 小时前
生成一个AI转岗经历标题,只需要一个标题,不需要其他的任何文本。
前端
Csvn11 小时前
Monorepo
前端
Csvn11 小时前
流水线
前端
Csvn11 小时前
微前端架构
前端
Linux-lucky13 小时前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu
风骏时光牛马15 小时前
生成一个移动端标题,只需要一个标题,不需要其他的任何文本。
前端
IT_陈寒15 小时前
Redis雪崩把我坑惨了,这是5年经验的教训
前端·人工智能·后端
SameX15 小时前
HarmonyOS ArkUI List 的侧滑删除 用 ListItem 的 swipeAction + endIcon 的做法
前端