vue 基础学习 第五天

41 css 动画

<!-- 1、引用 animate.css -->
<link rel="stylesheet" href="animate.css">

 <div id="app">
    <button @click="flag=!flag">切换</button>
    <!-- 2、用一个 <transition> 标签把动画影响的元素包裹起来 -->
    <!-- 3、给 <transition> 属性 enter-active-class="指定元素显示时的动画" leave-active-class="元素隐藏时的动画" -->
    <transition enter-active-class="animated fadeIn" leave-active-class="animated fadeOut">
        <h1 v-if="flag">测试</h1>
    </transition>
</div>

<script>
    var app = new Vue({
        el: '#app',
        data: {
            flag: true,
        },
    });
</script>
  • 使用 <transition> 标签将要受到动画影响的元素包起来
  • <transition> 标签中指定属性 enter-active-class="显示过程动画样式类"...
  • 一共有这么几个属性:
# enter="开始进入的样式"
# enter-active="进入过程中"
# enter-to="进完成后"
# leave-active="离开过程中"
# leave="完全离开后"

42 directive 自定义指令

  • 代码
<div id="app">
    <!-- <span v-star>测试</span> -->
    <input type="text" v-model="title" v-bindandupdate.focus="title">
</div>

<script>
    // Vue.directive('star', {
    //     // 绑定
    //     bind(el, bind) {
    //         console.log(bind);
    //     }
    // })
    // Vue.directive('test', {
    //     // 更新
    //     update(el, bind) {
    //         console.log(bind);
    //     }
    // })
    Vue.directive('bindandupdate', function(el, bind) {
        console.log(bind);
    })
    var app = new Vue({
        el: '#app',
        data: {
            title: "ceshi",
        },
    });
</script>
  • 使用 Vue.directive('指令名称', { 动作() { //触发时执行的代码 } })
  • 在标签上使用 v-指令名称 来将指令绑定到元素上
  • bind() 动作是指元素被载入后就触发的动作
  • update() 动作是指元素被更新时触发的操作
  • 通常我们就使用 bind 和 update 两个动作,所有很多时候我们可以简写为 Vue.directive('指令名称', function() { //同时绑定update 和 bind 动作 })

43 在组件中使用指令

  • 代码
<div id="app">
    <h1 v-test="color">字</h1>
    <input type="text" v-model="color" v-focus>
</div>

<script>
    var app = new Vue({
        el: '#app',
        data: {
            color: 'red',
        },
        directives: {
            // 这个相当于 bind() + update() 动作
            test(el, bind) {
                var color = bind.value ? bind.value : red;
                el.style.cssText = "color:" + color;
            },
            // 这个相当于其他动作
            focus: {
                // 比如 inserted : 当子元素插入到父元素的时候调用
                inserted(el, bind) {
                    el.focus();
                }
            }
        }
    });
</script>
  • 指令都写在 directives 属性中
  • 同样,直接写指令() {} 是直接绑定 bind 和 update 两个动作
  • 如果要用其他动作,则可以使用 指令: { 动作() { //代码 } }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 10,452评论 0 29
  • 1.安装 可以简单地在页面引入Vue.js作为独立版本,Vue即被注册为全局变量,可以在页面使用了。 如果希望搭建...
    Awey阅读 13,785评论 4 129
  • Vue 实例 属性和方法 每个 Vue 实例都会代理其 data 对象里所有的属性:var data = { a:...
    云之外阅读 6,644评论 0 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,301评论 19 139
  • 主妇A:唉,最近都没钱花了,这次回家又给了我妈三千。 主妇B:啊?那我妈比你妈好多了,我妈才不瞎要钱。 主妇B:唉...
    羅蕜阅读 1,046评论 0 1