当前位置:首页 > VUE

vue实现冰墩墩

2026-03-06 17:06:57VUE

Vue 实现冰墩墩动画

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

使用 CSS 动画

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

<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 的数据绑定实现动态效果。

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 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue无限菜单怎么实现

vue无限菜单怎么实现

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

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…

vue滑块实现

vue滑块实现

Vue滑块实现方法 使用原生HTML5 range input Vue中可以绑定原生HTML5的range类型input元素实现基础滑块功能: <template> <div&…

vue源码实现

vue源码实现

Vue 源码实现解析 Vue.js 的核心实现可以分为响应式系统、虚拟 DOM、模板编译、组件化等几个关键部分。以下是对这些核心机制的详细解析。 响应式系统 Vue 的响应式系统基于 Object.…