vue自定義元件製作div拖動
阿新 • • 發佈:2018-12-30
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> <style type="text/css"> *{ margin: 0; padding: 0; } p{ position: absolute; top: 10px; left: 10px; width: 200px; height: 200px; background: red; overflow: hidden; } </style> </head> <body> <div id="out"> <p v-move></p> <p v-move></p> <p v-move></p> </div> </body> <script type="text/javascript"> Vue.directive('move',function(a){ a.onmousedown=function(e){ var x=e.clientX-a.offsetLeft var y=e.clientY-a.offsetTop document.onmousemove=function(e){ var l=e.clientX-x; var t=e.clientY-y; a.style.left=l+'px'; a.style.top=t+'px'; } document.onmouseup=function(){ document.onmousemove=null; document.onmousedown=null; } } }) var vm=new Vue({ el:'#out' }) </script> </html>