bingbingyihao 2023-03-27 11:17 采纳率: 100%
浏览 24
已结题

CSS动画的重复播放及保持原效果

遇到的问题

无法让动画重复播放,每次播放结束后都无法继续播放
如果设置为可以多次播放,则又无法保持原状态(设置多次播放是通过重写了animation属性实现;一旦重写,这个 forwards 就没用了)

相关代码

转盘小游戏,Vue3代码

<template>
    <div id="app">
        <canvas id="canvas" :width="data.radius" :height="data.radius" class="container"></canvas>
        <span class="play" @click="play">点击开启转盘</span>
    </div>
</template>

<script>
import { onBeforeMount, onMounted, reactive } from "vue";

export default {
    name: "App",
    setup() {
        onBeforeMount(() => {
            const widthOfDevice = document.body.clientWidth;
            data.radius = widthOfDevice * 0.8;
        });
        const data = reactive({
            awardList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            radius: 200
        });

        function play() {
            const randomDeg = 360 * 8 + parseInt(Math.random() * 360 * 2);
            document.styleSheets[0].insertRule(
                `@keyframes run-rotate {
                    from {
                        transform: rotateZ(0deg);
                    }
                    to {
                        transform: rotateZ(${randomDeg}deg);
                    }
                }`,
            );
        }

        onMounted(() => {
            const myCanvas = document.getElementById("canvas");
            draw(myCanvas);
        });

        const randomColorList = ["#4169e199", "#fa807299", "#98fb9899", "#f5deb399", "#dda0dd99"];
        function draw(myCanvas) {
            const ctx = myCanvas.getContext("2d");
            ctx.translate(data.radius / 2, data.radius / 2);
            for (var i = 0; i < 10; i++) {
                ctx.beginPath();
                ctx.fillStyle = "#000000";
                ctx.font = "20px 行楷";
                for (var j = 0; j < 10; j++) {
                    ctx.fillText(data.awardList[i], data.radius / 2 * 0.6, 46);
                }
                ctx.fillStyle = randomColorList[i % 5];
                ctx.moveTo(0, 0);
                ctx.arc(0, 0, data.radius / 2, (0 * Math.PI) / 180, (36 * Math.PI) / 180);
                ctx.rotate((36 * Math.PI) / 180);
                ctx.fill();
                ctx.closePath();
            }
        }

        return {
            data,
            play,
        };
    },
};
</script>

<style>
* {
    padding: 0;
    margin: 0;
}

/* 容器样式 */
.container {
    width: 80vw;
    height: 80vw;
    margin-left: 10vw;
    margin-top: 10vw;
    border-radius: 50%;
    animation: run-rotate 3s ease-in-out 1;
    animation-fill-mode: forwards;
}

/* 开启按钮样式 */
.play {
    display: block;
    width: 40vw;
    height: 10vw;
    line-height: 10vw;
    margin-left: 30vw;
    margin-top: 10vw;
    cursor: pointer;
    color: crimson;
    text-align: center;
    border: 1px solid crimson;
    font-size: 20px;
    border-radius: 4vw;
}
</style>

img

  • 写回答

1条回答 默认 最新

  • 程序yang 全栈领域优质创作者 2023-03-27 11:59
    关注

    参考这样改改看看行不行

    <canvas ref="myCanvas" :width="data.radius" :height="data.radius" class="container run-rotate"></canvas>
    
    function play() {
          // 移除 run-rotate class,以重置动画状态
          this.$refs.myCanvas.classList.remove("run-rotate");
    
          const randomDeg = 360 * 8 + parseInt(Math.random() * 360 * 2);
          document.styleSheets[0].insertRule(
            `@keyframes run-rotate {
              from {
                transform: rotateZ(0deg);
              }
              to {
                transform: rotateZ(${randomDeg}deg);
              }
            }`
          );
    
          // 使用 setTimeout 函数,延迟一段时间后再次添加 run-rotate class,以触发动画的重复播放
          setTimeout(() => {
            this.$refs.myCanvas.classList.add("run-rotate");
          }, 100);
        }
    
        onMounted(() => {
          const myCanvas = this.$refs.myCanvas;
          draw(myCanvas);
        });
    
    .run-rotate {
      animation: run-rotate 3s ease-in-out 1;
      animation-fill-mode: forwards;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月4日
  • 已采纳回答 3月27日
  • 创建了问题 3月27日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测