您当前的位置:首页 > 网站建设 > javascript
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax | jQuery | 小程序 |

如何利用vue实现css过渡和动画

51自学网 2022-02-21 13:36:32
  javascript

一、过渡和动画的区别

过渡:通常用来表示元素上属性状态的变化。

动画:通常用来表示元素运动的情况。

二、使用Vue实现基础得css过渡与动画

1. 动画

/* css */@keyframes leftToRight {    0% {        transform: translateX(-100px);    }    50% {        transform: translateX(-50px);    }    100% {        transform: translateX(0);    }}.animation {    animation: leftToRight 3s;}
// jsconst app = Vue.createApp({    data() {        return {            animate: {                animation: true            }        }    },    methods: {        handleClick(){            this.animate.animation = !this.animate.animation        }    },    template: `        <div :class='animate'>hello</div>        <button @click='handleClick'>切换</button>    `});

2. 过渡

/* css */.transition {    transition: background-color 3s linear 0s;} .gold {    background-color: gold;} .cyan {    background-color: cyan;}
// jsconst app = Vue.createApp({    data() {        return {            animate: {                transition: true,                gold: true,                cyan: false            }        }    },    methods: {        handleClick() {            this.animate.gold = !this.animate.gold;            this.animate.cyan = !this.animate.cyan;        }    },    template: `        <div :class='animate'>hello</div>        <button @click='handleClick'>切换</button>    `});

以上是通过设置class属性实现的,同样通过设置style属性也可以实现:

/* css */.transition {    transition: background-color 3s linear 0s;}
// jsdata() {    return {        transition: 'transition',        styleObject: {            backgroundColor: 'gold'        }    }},methods: {    handleClick() {        if(this.styleObject.backgroundColor === 'gold'){            this.styleObject.backgroundColor = 'cyan';        }else{            this.styleObject.backgroundColor = 'gold';        }    }},template: `    <div :class='transition' :style='styleObject'>hello</div>    <button @click='handleClick'>切换</button>`

三、使用transition标签实现单元素/组件的过渡和动画效果

1. transition 的基本介绍

万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。