当前位置:首页 > VUE

vue实现冰墩墩

2026-03-06 17:06:57VUE

Vue 实现冰墩墩动画

使用 Vue 结合 CSS 动画或 Canvas 可以实现冰墩墩的动画效果。以下是两种实现方式:

vue实现冰墩墩

使用 CSS 动画

通过 Vue 的模板和样式绑定,结合 CSS 动画实现冰墩墩的简单动画。

vue实现冰墩墩

<template>
  <div class="bing-dwen-dwen">
    <div class="head"></div>
    <div class="body"></div>
    <div class="arms"></div>
    <div class="legs"></div>
  </div>
</template>

<style>
.bing-dwen-dwen {
  position: relative;
  width: 200px;
  height: 300px;
}

.head {
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: white;
  border-radius: 50%;
  top: 0;
  left: 50px;
  animation: bounce 2s infinite;
}

.body {
  position: absolute;
  width: 120px;
  height: 150px;
  background-color: white;
  border-radius: 60px 60px 0 0;
  top: 90px;
  left: 40px;
}

.arms, .legs {
  position: absolute;
  background-color: white;
}

.arms {
  width: 20px;
  height: 80px;
  top: 100px;
  left: 20px;
}

.legs {
  width: 30px;
  height: 60px;
  top: 240px;
  left: 85px;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}
</style>

使用 Canvas 绘制

通过 Vue 的 ref 和生命周期钩子,使用 Canvas 绘制更复杂的冰墩墩动画。

<template>
  <canvas ref="canvas" width="300" height="400"></canvas>
</template>

<script>
export default {
  mounted() {
    const canvas = this.$refs.canvas;
    const ctx = canvas.getContext('2d');

    function drawBingDwenDwen() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);

      // 绘制头部
      ctx.beginPath();
      ctx.arc(150, 100, 50, 0, Math.PI * 2);
      ctx.fillStyle = 'white';
      ctx.fill();

      // 绘制身体
      ctx.beginPath();
      ctx.ellipse(150, 200, 60, 80, 0, 0, Math.PI * 2);
      ctx.fill();

      // 绘制四肢
      ctx.fillRect(100, 150, 20, 60);
      ctx.fillRect(180, 150, 20, 60);
      ctx.fillRect(130, 280, 20, 40);
      ctx.fillRect(150, 280, 20, 40);
    }

    function animate() {
      drawBingDwenDwen();
      requestAnimationFrame(animate);
    }

    animate();
  }
};
</script>

使用 SVG 和 Vue 绑定

通过 SVG 和 Vue 的数据绑定实现动态效果。

<template>
  <svg width="300" height="400">
    <circle cx="150" cy="100" r="50" fill="white" />
    <ellipse cx="150" cy="200" rx="60" ry="80" fill="white" />
    <rect x="100" y="150" width="20" height="60" fill="white" />
    <rect x="180" y="150" width="20" height="60" fill="white" />
    <rect x="130" y="280" width="20" height="40" fill="white" />
    <rect x="150" y="280" width="20" height="40" fill="white" />
  </svg>
</template>

以上方法可以根据需求选择,CSS 动画适合简单效果,Canvas 适合复杂绘制,SVG 适合矢量图形和动态绑定。

标签: vue冰墩墩
分享给朋友:

相关文章

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件,…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{ 'bl…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…