zoukankan      html  css  js  c++  java
  • Vue directive自定义指令

    1. Vue 自定义指令实现拖拽

    <script src="vue.js"></script>
    <script>
    Vue.directive('drag',function(){
    var oDiv=this.el;//相当于dom对象
    oDiv.onmousedown=function(ev){
    var disX=ev.clientX-oDiv.offsetLeft;
    var disY=ev.clientY-oDiv.offsetTop;

    document.onmousemove=function(ev){
    var l=ev.clientX-disX;
    var t=ev.clientY-disY;
    oDiv.style.left=l+'px';
    oDiv.style.top=t+'px';
    };
    document.onmouseup=function(){
    document.onmousemove=null;
    document.onmouseup=null;
    };
    };
    });

    window.onload=function(){
    var vm=new Vue({
    el:'#box',
    data:{
    msg:'welcome'
    }
    });
    };

    </script>
    </head>
    <body>
    <div id="box">
    <div v-drag :style="{'100px', height:'100px', background:'blue', position:'absolute', right:0, top:0}"></div>
    <div v-drag :style="{'100px', height:'100px', background:'red', position:'absolute', left:0, top:0}"></div>
    </div>

    </body>

    2. vue自定义指令传参

    <script src="vue.js"></script>
    <script>
    Vue.directive('red',function(color){
    this.el.style.background=color;
    });

    window.onload=function(){
    var vm=new Vue({
    el:'#box'
    });
    };

    </script>
    </head>
    <body>
    <div id="box">
    <span v-red="'blue'">
    asdfasd
    </span>
    </div>

    </body>

  • 相关阅读:
    java 字节流与字符流的区别
    什么是缓冲区
    java流输入输出
    Apache安装配置
    Maven学习
    Redis
    数据结构与算法
    pig ERROR 2997: Encountered IOException. File or directory null does not exist.
    hadoop学习路线(转)
    86标准SQL与92标准SQL用法区别
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/9492094.html
Copyright © 2011-2022 走看看