Vue 搭配GSAP实现颜色动态渐变

重点

使用reactive构造响应式的对象存储颜色,使用gsap.to操作响应式对象实现颜色渐变

代码

        let toTimeLine: Tween
        let overTimeLine: Tween
        type Color = {value: string}
        type Tween = gsap.core.Tween
        const addItemColor = reactive<Color>({
            value: 'rgb(212, 212, 212)'
        })
        const addItemColorList = reactive<Color[]>([
            {value: 'rgb(212, 212, 212)'},
            {value: 'rgb(144, 144, 144)'},
        ])
        // 鼠标移入控制颜色渐变
        const addEnter = () => {
            overTimeLine && overTimeLine.kill();
            toTimeLine = gsap.to(addItemColor, {
                value: addItemColorList[1].value,
                duration: 0.3,
            })
        }
        // 鼠标移出控制颜色渐变
        const addLeave = () => {
            toTimeLine && toTimeLine.kill();
            toTimeLine = gsap.to(addItemColor, {
                value: addItemColorList[0].value,
                duration: 0.3,
            })
        }
posted @ 2024-01-31 21:17  隔岸苦晴  阅读(297)  评论(0)    收藏  举报