欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能(拖動(dòng)過(guò)快失效問(wèn)題解決方法)

 更新時(shí)間:2018年08月24日 16:40:29   作者:進(jìn)軍的蝸牛  
這篇文章主要介紹了vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能,文中給大家介紹了鼠標(biāo)移動(dòng)過(guò)快拖動(dòng)就失效問(wèn)題的解決方法,需要的朋友可以參考下

今天用vue+原生js的mousemove事件,寫(xiě)了個(gè)拖動(dòng),發(fā)現(xiàn)只能慢慢拖動(dòng)才行,鼠標(biāo)只要移動(dòng)快了,就失效,不能拖動(dòng)了;

搞了半天在,總算解決了,但是問(wèn)題的深層原理還沒(méi)搞清楚,知道的大俠可以留言分享,下面直接上代碼:

只能慢速拖動(dòng)的代碼:

<!DOCTYPE html>
<html>
<head>
 <title>vue結(jié)合原生js實(shí)現(xiàn)拖動(dòng)</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<div class="ctn ctn1">
 <div class="sub sub1" v-for="(site, index) in list1">
   <div class="dragCtn fixed" @mousedown="mousedown(site, $event)" @mousemove.prevent='mousemove(site, $event)' @mouseup='mouseup(site, $event)'>
   {{ site.name }}
   </div>
 </div>
</div>
<div class="ctn ctn2">
 <div class="sub sub2" v-for="(site, index) in list2">
   <div class="dragCtn">
    {{ index }} : {{ site.name }}
   </div>
 </div> 
</div> 
</div>
<script>
new Vue({
 el: '#app',
 data: {
 list1: [{name:'拖動(dòng)我', index:0}],
 list2: [{name:'a', index:0}, {name:'b', index:1}, {name:'c', index: 2}, {name:'d', index: 3}],
 vm:'',
 sb_bkx: 0,
 sb_bky: 0,
 is_moving: false
 },
 methods: {
  mousedown: function (site, event) {
  var startx=event.x;
  var starty=event.y;
  this.sb_bkx=startx - event.target.offsetLeft;
  this.sb_bky=starty - event.target.offsetTop;
  this.is_moving = true;
  },
  mousemove: function (site, event) {
   var endx=event.x - this.sb_bkx;
  var endy=event.y - this.sb_bky;
  var _this = this
  if(this.is_moving){
   event.target.style.left=endx+'px';
   event.target.style.top=endy+'px';
  }
  },
  mouseup: function (e) {
  this.is_moving = false;
  }
 }
})
</script>
<style>
 .ctn{
  line-height: 50px;
  cursor: pointer;
  font-size: 20px;
  text-align: center;
  float: left;
 }
 .sub:hover{
  background: #e6dcdc;
  color: white;
  width: 100px;
 }
  .ctn1{
   border: 1px solid green;
   width: 100px;
  }
  .ctn2{
   border: 1px solid black;
   width: 100px;
   margin-left: 50px;
  }
  .fixed{
   width: 100px;
   height: 100px;
  position: fixed;
  background: red;
  left: 10px;
  top: 10px;
  cursor: move;
  }
</style>
</body>
</html>

可以快速拖動(dòng)的代碼:

<!DOCTYPE html>
<html>
<head>
 <title>vue結(jié)合原生js實(shí)現(xiàn)拖動(dòng)</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<div class="ctn ctn1">
<!-- draggable=true -->
 <div class="sub sub1" v-for="(site, index) in list1">
 <!-- @mousemove.prevent='mousemove(site, $event)' -->
   <div class="dragCtn fixed" @mousedown="mousedown(site, $event)" @mouseup='mouseup(site, $event)'>
    {{ site.name }}
   </div>
 </div>
</div>
<div class="ctn ctn2">
 <div class="sub sub2" v-for="(site, index) in list2">
   <div class="dragCtn">
    {{ index }} : {{ site.name }}
   </div>
 </div> 
</div> 
</div>
<script>
new Vue({
 el: '#app',
 data: {
 list1: [{name:'拖動(dòng)我', index:0}],
 list2: [{name:'a', index:0}, {name:'b', index:1}, {name:'c', index: 2}, {name:'d', index: 3}],
 vm:'',
 sb_bkx: 0,
 sb_bky: 0,
 },
 methods: {
  mousedown: function (site, event) {
  var event=event||window.event;
  var _target = event.target
  var startx=event.clientX;
  var starty=event.clientY;
  var sb_bkx=startx-event.target.offsetLeft;
  var sb_bky=starty-event.target.offsetTop;
  var ww=document.documentElement.clientWidth;
  var wh = window.innerHeight;
  if (event.preventDefault) {
   event.preventDefault();
  } else{
   event.returnValue=false;
  };
  document.onmousemove=function (ev) {
   var event=ev||window.event;
   var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;
   if (event.clientY < 0 || event.clientX < 0 || event.clientY > wh || event.clientX > ww) {
    return false;
   };
   var endx=event.clientX-sb_bkx;
   var endy=event.clientY-sb_bky;
   _target.style.left=endx+'px';
   _target.style.top=endy+'px';
  }
  },
  mouseup: function (e) {
  document.onmousemove=null;
  }
 }
})
</script>
<style>
 .ctn{
  line-height: 50px;
  cursor: pointer;
  font-size: 20px;
  text-align: center;
  float: left;
 }
 .sub:hover{
  background: #e6dcdc;
  color: white;
  width: 100px;
 }
  .ctn1{
   border: 1px solid green;
   width: 100px;
  }
  .ctn2{
   border: 1px solid black;
   width: 100px;
   margin-left: 50px;
  }
  .fixed{
  width: 100px;
   height: 100px;
  position: fixed;
  background: red;
  left: 10px;
  top: 15px;
  cursor: move;
  }
</style>
</body>
</html>

補(bǔ)充:vue 自定義指令-拖拽

主要思想: 獲取拖拽的dom元素,在oDiv.onmousedown事件內(nèi)獲取鼠標(biāo)相對(duì)dom元素本身的位置:

 var disX=ev.clientX-oDiv.offsetLeft;
 var disY=ev.clientY-oDiv.offsetTop;

再通過(guò)document.onmousemove事件計(jì)算dom元素左上角相對(duì) 視口的距離:

var l=ev.clientX-disX;
var t=ev.clientY-disY;
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';

完整代碼:

 <script>
  /* vue-自定義指令-拖拽 */
  Vue.directive('drag',function(){
   var oDiv=this.el;
   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="{width:'100px', height:'100px', background:'blue', position:'absolute', right:0, top:0}"></div>
  <div v-drag :style="{width:'100px', height:'100px', background:'red', position:'absolute', left:0, top:0}"></div>
 </div>
</body>

總結(jié)

以上所述是小編給大家介紹的vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vue學(xué)習(xí)之Vuex的使用詳解

    Vue學(xué)習(xí)之Vuex的使用詳解

    這篇文章主要介紹了Vue中的插件:Vuex。本文將圍繞它的優(yōu)缺點(diǎn)、使用場(chǎng)景和示例展開(kāi)詳細(xì)的說(shuō)明,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-01-01
  • 解決vue偵聽(tīng)器watch,調(diào)用this時(shí)出現(xiàn)undefined的問(wèn)題

    解決vue偵聽(tīng)器watch,調(diào)用this時(shí)出現(xiàn)undefined的問(wèn)題

    這篇文章主要介紹了解決vue偵聽(tīng)器watch,調(diào)用this時(shí)出現(xiàn)undefined的問(wèn)題,具有很好的參考
    2020-10-10
  • vue2.0實(shí)現(xiàn)檢測(cè)無(wú)用的代碼并刪除

    vue2.0實(shí)現(xiàn)檢測(cè)無(wú)用的代碼并刪除

    這篇文章主要介紹了vue2.0實(shí)現(xiàn)檢測(cè)無(wú)用的代碼并刪除方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Vue登錄頁(yè)面的動(dòng)態(tài)粒子背景插件實(shí)現(xiàn)

    Vue登錄頁(yè)面的動(dòng)態(tài)粒子背景插件實(shí)現(xiàn)

    本文主要介紹了Vue登錄頁(yè)面的動(dòng)態(tài)粒子背景插件實(shí)現(xiàn),將登錄組件背景設(shè)置為 "粒子背景",具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Vue2 Watch監(jiān)聽(tīng)操作方法

    Vue2 Watch監(jiān)聽(tīng)操作方法

    這篇文章主要介紹了Vue2 Watch監(jiān)聽(tīng),通過(guò)watch監(jiān)聽(tīng)器,我們可以實(shí)時(shí)監(jiān)控?cái)?shù)據(jù)的變化,并且在數(shù)據(jù)發(fā)生改變時(shí)進(jìn)行相應(yīng)的操作,需要的朋友可以參考下
    2023-12-12
  • Vue金融數(shù)字格式化(并保留小數(shù))數(shù)字滾動(dòng)效果實(shí)現(xiàn)

    Vue金融數(shù)字格式化(并保留小數(shù))數(shù)字滾動(dòng)效果實(shí)現(xiàn)

    這篇文章主要介紹了Vue金融數(shù)字格式化(并保留小數(shù)) 數(shù)字滾動(dòng)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • Vue+Element的后臺(tái)管理框架的整合實(shí)踐

    Vue+Element的后臺(tái)管理框架的整合實(shí)踐

    本文主要介紹了Vue+Element的后臺(tái)管理框架,在框架上,領(lǐng)導(dǎo)要用AdminLTE這套模板,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Vue自定義指令簡(jiǎn)介和基本使用示例

    Vue自定義指令簡(jiǎn)介和基本使用示例

    同時(shí)Vue也支持讓開(kāi)發(fā)者,自己注冊(cè)一些指令,這些指令被稱為自定義指令,每個(gè)指令都有自己各自獨(dú)立的功能,這篇文章主要介紹了Vue自定義指令簡(jiǎn)介和基本使用,需要的朋友可以參考
    2024-03-03
  • Vue CLI3.0中使用jQuery和Bootstrap的方法

    Vue CLI3.0中使用jQuery和Bootstrap的方法

    這篇文章主要介紹了Vue CLI3.0中使用jQuery和Bootstrap的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • vuex的module模塊用法示例

    vuex的module模塊用法示例

    這篇文章主要介紹了vuex的module模塊用法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11

最新評(píng)論