最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+mousemove實現(xiàn)鼠標(biāo)拖動功能(拖動過快失效問題解決方法)

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

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

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

只能慢速拖動的代碼:

<!DOCTYPE html>
<html>
<head>
 <title>vue結(jié)合原生js實現(xiàn)拖動</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:'拖動我', 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>

可以快速拖動的代碼:

<!DOCTYPE html>
<html>
<head>
 <title>vue結(jié)合原生js實現(xiàn)拖動</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:'拖動我', 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)相對dom元素本身的位置:

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

再通過document.onmousemove事件計算dom元素左上角相對 視口的距離:

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實現(xiàn)鼠標(biāo)拖動功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue路由緩存的幾種實現(xiàn)方式小結(jié)

    vue路由緩存的幾種實現(xiàn)方式小結(jié)

    這篇文章主要介紹了vue路由緩存的幾種實現(xiàn)方式,結(jié)合實例形式詳細(xì)分析了vue.js路由緩存常見實現(xiàn)方式、使用技巧與操作注意事項,需要的朋友可以參考下
    2020-02-02
  • 利用vue + koa2 + mockjs模擬數(shù)據(jù)的方法教程

    利用vue + koa2 + mockjs模擬數(shù)據(jù)的方法教程

    這篇文章主要給大家介紹了關(guān)于利用vue + koa2 + mockjs模擬數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • vue-cli 項目打包完成后運(yùn)行文件路徑報錯問題

    vue-cli 項目打包完成后運(yùn)行文件路徑報錯問題

    這篇文章主要介紹了vue-cli 項目打包完成后運(yùn)行文件路徑報錯問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Vue實現(xiàn)渲染數(shù)據(jù)后控制滾動條位置(推薦)

    Vue實現(xiàn)渲染數(shù)據(jù)后控制滾動條位置(推薦)

    這篇文章主要介紹了Vue實現(xiàn)渲染數(shù)據(jù)后控制滾動條位置,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Vue中computed和watch的區(qū)別

    Vue中computed和watch的區(qū)別

    在vue項目中我們常常需要用到computed和watch,那么我們究竟在什么場景下使用computed和watch呢?他們之間又有什么區(qū)別呢?本將給大家詳細(xì)的介紹一下,需要的朋友可以參考下
    2023-05-05
  • 簡單了解vue中的v-if和v-show的區(qū)別

    簡單了解vue中的v-if和v-show的區(qū)別

    這篇文章主要介紹了簡單了解vue中的v-if和v-show的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • vue?mixins代碼復(fù)用的項目實踐

    vue?mixins代碼復(fù)用的項目實踐

    本文主要介紹了vue?mixins代碼復(fù)用的項目實踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • webpack vue 項目打包生成的文件,資源文件報404問題的修復(fù)方法(總結(jié)篇)

    webpack vue 項目打包生成的文件,資源文件報404問題的修復(fù)方法(總結(jié)篇)

    這篇文章主要介紹了解決webpack vue 項目打包生成的文件,資源文件報404問題的修復(fù)方法,需要的朋友可以參考下
    2018-01-01
  • vue中使用cookies和crypto-js實現(xiàn)記住密碼和加密的方法

    vue中使用cookies和crypto-js實現(xiàn)記住密碼和加密的方法

    這篇文章給大家介紹一下關(guān)于vue中使用cookies和crypto-js如何實現(xiàn)密碼的加密與記住密碼,有一定的參考價值,有需要的朋友可以參考一下,希望對你們有所幫助。
    2018-10-10
  • vue前端如何接收后端傳過來的帶list集合的數(shù)據(jù)

    vue前端如何接收后端傳過來的帶list集合的數(shù)據(jù)

    這篇文章主要介紹了vue前端如何接收后端傳過來的帶list集合的數(shù)據(jù),前后端交互,文中的示例Json報文,前端采用vue進(jìn)行接收,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-02-02

最新評論

台安县| 淳安县| 同江市| 陆良县| 邯郸市| 周口市| 芦山县| 邢台市| 涪陵区| 平阴县| 鹤壁市| 宜昌市| 株洲市| 米易县| 德江县| 慈利县| 定襄县| 塔城市| 井研县| 绵阳市| 孝感市| 靖安县| 定襄县| 皋兰县| 奉化市| 绥江县| 霍林郭勒市| 新竹市| 道真| 南投县| 天台县| 临高县| 河曲县| 高雄市| 梁河县| 长寿区| 新丰县| 塘沽区| 抚州市| 文化| 泉州市|