不生效原因:
样式中使用了scoped
1. 模板:
<template>
<div class="box">
<div :style="styleObj">
</div>
</div>
</template>
2.设置样式:
样式使用计算属性获取
const styleObj = computed(() => {
const res: Record<string, any> = {};
if (props.rotate) {
res.animation = 'rotating 0s linear infinite';
res.animationDuration = props.duration / 1000 + 's';
}
return res;
});
3.设置旋转:
<style lang="less">
.box {
@keyframes rotating {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
}
</style>
以上操作即可设置旋转