国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vuejs移動端實現div拖拽移動

2019-11-19 11:08:00
字體:
來源:轉載
供稿:網友

vue移動端實現div拖拽移動,供大家參考,具體內容如下

本文講述,在使用VUE的移動端實現類似于iPhone的懸浮窗的效果。

相關知識點

  • touchstart 當在屏幕上按下手指時觸發
  • touchmove 當在屏幕上移動手指時觸發
  • touchend 當在屏幕上抬起手指時觸發
  • mousedown mousemove mouseup對應的是PC端的事件
  • touchcancel 當一些更高級別的事件發生的時候(如電話接入或者彈出信息)會取消當前的touch操作,即觸發touchcancel。一般會在touchcancel時暫停游戲、存檔等操作。

效果圖

實現步驟

(一)html

總結了一下評論,好像發現大家都碰到了滑動的問題。就在這里提醒一下吧。可將該懸浮 DIV 同你的 scroller web 同級。 ---- (log: 2018-08-21)

html結構: <template> <div>你的web頁面</div> <div>懸浮DIV</div> </template>

<template> <div id="webId">  <div>你的web頁面</div>  <!-- 1.1 如果碰到滑動問題,請檢查這里是否屬于同一點。 -->  <!-- 懸浮的HTML -->  <div v-if="!isShow" class="xuanfu" id="moveDiv"  @mousedown="down" @touchstart="down"  @mousemove="move" @touchmove="move"  @mouseup="end" @touchend="end"  >  <div class="yuanqiu">   {{pageInfo.totalPage}}  </div>  </div> </div></template>

(二)JS

<script>data() { return { flags: false, position: { x: 0, y: 0 }, nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '', }}methods: { // 實現移動端拖拽 down(){ this.flags = true; var touch; if(event.touches){  touch = event.touches[0]; }else {  touch = event; } this.position.x = touch.clientX; this.position.y = touch.clientY; this.dx = moveDiv.offsetLeft; this.dy = moveDiv.offsetTop; }, move(){ if(this.flags){  var touch ;  if(event.touches){   touch = event.touches[0];  }else {   touch = event;  }  this.nx = touch.clientX - this.position.x;  this.ny = touch.clientY - this.position.y;  this.xPum = this.dx+this.nx;  this.yPum = this.dy+this.ny;  moveDiv.style.left = this.xPum+"px";  moveDiv.style.top = this.yPum +"px";  //阻止頁面的滑動默認事件  document.addEventListener("touchmove",function(){ // 1.2 如果碰到滑動問題,請注意是否獲取到 touchmove   event.preventDefault();//jq 阻止冒泡事件   // event.stopPropagation(); // 如果沒有引入jq 就用 stopPropagation()  },false); } },//鼠標釋放時候的函數 end(){ this.flags = false; },}</script>

(三)CSS

<style> .xuanfu { height: 4.5rem; width: 4.5rem; /*1.3 如果碰到滑動問題,請檢查 z-index。z-index需比web大一級*/ z-index: 999; position: fixed; top: 4.2rem; right: 3.2rem; border-radius: 0.8rem; background-color: rgba(0, 0, 0, 0.55); } .yuanqiu { height: 2.7rem; width: 2.7rem; border: 0.3rem solid rgba(140, 136, 136, 0.5); margin: 0.65rem auto; color: #000000; font-size: 1.6rem; line-height: 2.7rem; text-align: center; border-radius: 100%; background-color: #ffffff; }</style>

實現好JS邏輯,基本上,問題不大。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 本溪市| 禹州市| 建瓯市| 建宁县| 上犹县| 洪江市| 台安县| 昌邑市| 仪陇县| 漳州市| 岳西县| 红安县| 新乡市| 佛山市| 鲁甸县| 罗定市| 阳东县| 武宣县| 杨浦区| 嘉定区| 玉门市| 九寨沟县| 桑日县| 罗甸县| 固原市| 千阳县| 海盐县| 科技| 象州县| 揭阳市| 神农架林区| 宁明县| 南丹县| 随州市| 沂南县| 饶阳县| 偃师市| 铜川市| 永仁县| 疏勒县| 阿克苏市|