使用vue2写一个太极图,并且点击旋转

下面是我自己写的一个代码,命名有些不规范,大家不要介意。

html 复制代码
<template>
  <div class="qq">
    <div class="app" :style="{ transform: rotateStyle }">
      <div class="app1">
        <div class="app3">
          <div class="app5"></div>
        </div>
      </div>
      <div class="app2">
        <div class="app4">
          <div class="app6"></div>
        </div>
      </div>
    </div>
    <!-- 点击按钮 -->
    <button class="app7" @click="toggleRotation">点击开始/停止旋转</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rotationSpeed: 1, // 初始旋转速度
      rotationCount: 0, // 旋转次数
      isRotating: false, // 是否正在旋转
      animationFrameId: null // 用于保存 requestAnimationFrame 返回的 ID
    };
  },
  computed: {
    rotateStyle() {
      return `rotate(-${this.rotationCount * this.rotationSpeed}deg)`;
    }
  },
  methods: {
    toggleRotation() {
      if (this.isRotating) {
        this.stopRotation();
      } else {
        this.startRotation();
      }
    },
    startRotation() {
      this.rotationCount = 0; // 重置旋转次数
      this.isRotating = true; // 开始旋转
      this.rotate();
    },
    stopRotation() {
      this.isRotating = false; // 停止旋转
      cancelAnimationFrame(this.animationFrameId); // 清除动画帧
    },
    rotate() {
      if (this.isRotating) {
        this.rotationCount++;
        this.rotationSpeed *= 1.002; // 加速旋转
        this.animationFrameId = requestAnimationFrame(this.rotate);
      }
    }
  }
};
</script>


<style>
.qq {
  width: 800px;
  height: 800px;
  background-color: gray;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app {
  display: flex;
  width: 310px;
  height: 310px;
  align-items: center;
  justify-content: center;
}

.app1 {
  width: 150px;
  height: 300px;
  background-color: black;
  border-radius: 150px 0 0 150px;
}

.app2 {
  width: 150px;
  height: 300px;
  background-color: white;
  border-radius: 0 150px 150px 0;
}

.app3 {
  width: 150px;
  height: 150px;
  background-color: black;
  border-radius: 50%;
  position: absolute;
  margin-left: 75px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app4 {
  width: 150px;
  height: 150px;
  background-color: white;
  border-radius: 50%;
  position: absolute;
  margin-top: 150px;
  margin-left: -75px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app5 {
  width: 50px;
  height: 50px;
  background-color: white;
  border-radius: 50%;

}

.app6 {
  width: 50px;
  height: 50px;
  background-color: black;
  border-radius: 50%;
}

.app7 {
  margin-left: 100px;
}
</style>
  • 在 Vue.js 组件的 data 部分,定义了一些数据属性,包括 rotationSpeed(旋转速度)、rotationCount(旋转次数)、isRotating(是否正在旋转)和 animationFrameId(保存 requestAnimationFrame 返回的 ID)。
  • 通过 computed 属性 rotateStyle 计算样式,用于控制旋转的角度。
  • 定义了三个方法:
    • toggleRotation:切换旋转状态,如果正在旋转,则停止;如果未旋转,则开始。
    • startRotation:开始旋转,重置旋转次数,设置 isRotatingtrue,并调用 rotate 方法。
    • stopRotation:停止旋转,设置 isRotatingfalse,并清除动画帧。
    • rotate:递归调用的方法,用于模拟旋转动画。每次调用会增加旋转次数和旋转速度,然后通过 requestAnimationFrame 请求下一帧的调用。
相关推荐
一个水瓶座程序猿.3 分钟前
ES6数组的`flat()`和`flatMap()`函数用法
前端·ecmascript·es6
袁煦丞19 分钟前
AI直接出答案!Perplexica开源搜索引擎:cpolar内网穿透实验室第534个成功挑战
前端·程序员·远程工作
Hilaku21 分钟前
用“人话”讲明白10个最常用的正则表达式
前端·javascript·正则表达式
木叶丸29 分钟前
跨平台方案该如何选择?
android·前端·ios
LL.。32 分钟前
同步回调和异步回调
开发语言·前端·javascript
网络点点滴37 分钟前
Vue如何处理数据、v-HTML的使用及总结
前端·vue.js·html
运维咖啡吧38 分钟前
周一才上线的网站,单单今天已经超过1000访问了
前端·程序员·ai编程
世界哪有真情41 分钟前
用虚拟IP扩容端口池:解决高并发WebSocket端口耗尽问题
前端·后端·websocket
前端世界41 分钟前
打造一个可维护、可复用的前端权限控制方案(含完整Demo)
前端
LeQi1 小时前
当!important成为代码毒瘤:你的项目是不是也中了招?
前端·css·程序员