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属性来提示浏览器提前优化动画元素

相关推荐
UIUV18 分钟前
JavaScript 遍历方法详解
前端·javascript·代码规范
Zyx20071 小时前
React 中的 Props:组件通信与复用的核心机制
前端
海云前端11 小时前
大模型Function Calling的函数如何调用的?
前端
ohyeah1 小时前
防抖与节流:前端性能优化的两大利器
前端·javascript
Zyx20071 小时前
React Hooks:函数组件的状态与副作用管理艺术
前端
曲幽1 小时前
手把手搞定FastAPI静态文件:安全、上传与访问
css·python·fastapi·web·js·favicon·staticfiles
让我上个超影吧1 小时前
基于SpringBoot和Vue实现CAS单点登录
前端·vue.js·spring boot
军军君012 小时前
Three.js基础功能学习五:雾与渲染目标
开发语言·前端·javascript·学习·3d·前端框架·three
程序员爱钓鱼2 小时前
Node.js 编程实战:RESTful API 设计
前端·后端·node.js