Vue實(shí)現(xiàn)簡(jiǎn)單的拖拽效果
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)簡(jiǎn)單拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
自定義指令v-drag
l 存在時(shí) 只能橫向拖拽
t 存在時(shí) 只能縱向拖拽
lt都存在時(shí) 可以任意方向拖拽
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>拖拽</title> <style> *{ margin: 0; padding: 0; } #box{ background: red; width: 100px; height: 100px; position: absolute; } </style> <script src='http://www.piao2010.com/bcjs/vue.js'></script></head><body> <div id='app'> <div v-drag.l.t='flag'></div> </div> <script> Vue.directive('drag',(el,{modifiers,value})=>{ let{l,t}=modifiers; el.addEventListener('mousedown',handleDownCb) let disX,disY; function handleDownCb(e){ disX=e.offsetX; disY=e.offsetY; // console.log(disX,disY) document.addEventListener('mousemove',handleMoveCb); document.addEventListener('mouseup',handleUpCb); } function handleMoveCb(e){ let x=e.clientX-disX; let y=e.clientY-disY; if((l&&t) && value){ el.style.left=x+'px'; el.style.top=y+'px'; return; } if(l&&value){ el.style.left=x+'px'; return; } if(t&&value){ el.style.top=y+'px'; return; } } function handleUpCb(){ document.removeEventListener('mousemove',handleMoveCb); document.removeEventListener('mouseup',handleUpCb); } }) let vm=new Vue({ el:'#app', data:{ flag:true } }) </script></body></html>
注:
改變v-drag.l v-drag.t v-drag.l.t 即可實(shí)現(xiàn)橫向 縱向 任意方向的拖拽
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲(chóng)beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實(shí)現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測(cè)試工具Locust安裝及使用7. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式8. 使用Python解析Chrome瀏覽器書(shū)簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
