切换动画
完整源码
let shards = document.querySelectorAll('.shard') const res = [] shards.forEach(ele => { const allStyles = window.getComputedStyle(ele) res.push({ clipPath: allStyles.getPropertyValue('clip-path'), backgroundColor: allStyles.getPropertyValue('background-color') }) }) console.log(JSON.stringify(res)) <template> <Button @click="changeAnimal">切换</Button> <div class="animal-container"> <template v-for="(item, index) in currentData" :key="index"> <div class="clip-item" :style="{ backgroundColor: item.backgroundColor, clipPath: item.clipPath, transitionDelay: `${index * 15}ms`, }" ></div> </template> </div> </template> <script lang="ts" setup> import { ref } from 'vue'; import { Button } from 'ant-design-vue'; import data from './data.json'; const currentIndex = ref(0); const currentData = ref(data[0]); const length = data.length; const changeAnimal = () => { currentIndex.value++; // 索引到最后了,重置为 0 if (currentIndex.value >= length) { currentIndex.value = 0; } currentData.value = data[currentIndex.value]; }; </script> <style scoped lang="less"> .animal-container { width: 800px; height: 600px; position: relative; .clip-item { position: absolute; width: 100%; height: 100%; // 堆代码 duidaima.com // 动画过渡效果 transition-property: all; transition-duration: 1000ms; transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); } } </style>