Vue中动态修改keyframes动画特效

使用一个简单的投票系统来做演示。
在这里插入图片描述
在这里插入图片描述

操作

主要使用的是document.styleSheets[0]
通过插入规则 insertRule 来添加动画
最后通过style进行样式修改

代码

下面是演示代码:

<template>
    <div>
        <div class="toupiao" v-if="showTest">
            <div class="yes btns" @click="changes('1')">赞同</div>
            <div class="no btns" @click="changes('0')">不赞同</div>
        </div>
        <div class="showToupiaoMsg" v-show="!showTest">
            <div class="yes btns" ref="yes">
                <div>
                    <span v-show="touVal == '1'">您选择了:</span>
                    赞同
                </div>
                <div>
                    {{ yesPeople }}
                </div>
            </div>
            <div class="no btns" ref="no">
                <div><span v-show="touVal == '0'">您选择了:</span>不赞同</div>
                <div>
                    {{ noPeople }}
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            yesPeople: 6781, //同意人数
            noPeople: 3361, //不同意人数
            showTest: true, //显示
            touVal: "" //选择的选项
        }
    },
    methods: {
        // 显示投票数据
        changes(val) {
            this.showTest = false
            this.touVal = val
        }
    },
    mounted() {
        // 百分比
        var isTrue = (this.yesPeople / (this.yesPeople + this.noPeople)) * 100
        var isFalse = (this.noPeople / (this.yesPeople + this.noPeople)) * 100
        // 规则
        var ruleTrue = ` @keyframes changeWidth3 {
                    to {
                        background-size: ${isTrue}% 100%;
                    }
            }`
        var rulesFalse = `
                     @keyframes changeWidth4 {
                    to {
                        background-size: ${isFalse}% 100%;
                    }
            }`
        // 获取规则并进行改变
        var sheet = document.styleSheets[0]
        sheet.insertRule(ruleTrue, 0)
        sheet.insertRule(rulesFalse, 0)
        this.$refs.yes.style.animation = "changeWidth3  1s  forwards"
        this.$refs.no.style.animation = "changeWidth4  1s  forwards"
    }
}
</script>

<style lang="less" scoped>
.toupiao {
    width: 100vw;
    height: 100px;
    overflow: hidden;
    background: pink;
    .btns {
        width: 94%;
        height: 2rem;
        margin: 10px;
        border: 1px solid #fff;
        border-radius: 5px;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .btns:active {
        background: orangered;
        border: 1px solid orangered;
        opacity: 0.6;
    }
}
.showToupiaoMsg {
    width: 100vw;
    height: 120px;
    overflow: hidden;
    background: pink;
    margin-top: 1px;
    .btns {
        width: 92%;
        height: 2rem;
        margin: 10px;
        border: 1px solid #fff;
        border-radius: 5px;
        color: #fff;
        display: flex;
        justify-content: space-between;
        padding: 0.2rem;
        align-items: center;
    }
    .yes {
        background-image: linear-gradient(#73ffa0, #73ffa0);
        background-repeat: no-repeat;
        background-size: 0 100%;
        background-position: 0 70%;
        animation: changeWidth 1s forwards;
    }
    .no {
        background-image: linear-gradient(#ccc, #ccc);
        background-repeat: no-repeat;
        background-size: 0 100%;
        background-position: 0 70%;
        animation: changeWidth2 1s forwards;
    }
}
</style>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hope°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值