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

vue實現(xiàn)移動端觸屏拖拽功能

 更新時間:2020年08月21日 17:20:10   作者:meteorshower2013  
這篇文章主要為大家詳細介紹了vue實現(xiàn)移動端觸屏拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

vue實現(xiàn)移動端可拖拽浮球,供大家參考,具體內(nèi)容如下

1 首先創(chuàng)建一個div

<div class="floatball" id="floatball"
 @mousedown="down" @touchstart.stop="down"
 @mousemove="move" @touchmove.stop="move"
 @mouseup="end" @touchend.stop="end" @click="showRewardDesc"
 :style="{top:position.y+'px', left:position.x+'px'}">
 獎勵規(guī)則
</div>

2 給 div 附上樣式

<style>
 .floatball{
 color:white;
 height:50px;
 width: 50px;
 padding: 5px;
 z-index: 990;
 position: fixed;
 top: 60px;
 right: 320px;
 border-radius: 50%;
 background-color: rgba(29, 157, 237,0.8);
 }

</style>

3 給 div 附上事件

準備四個變量

1)、屏幕長

var screenHeight = window.screen.height

2)、屏幕寬

var screenWidth = window.screen.width

3)、初始觸控點 距離 div 左上角的橫向距離 dx

4)、初始觸控點 距離 div 左上角的豎向距離 dy

在開始拖拽時,計算出鼠標點(初始觸控點)和 div左上角頂點的距離

down(event){
 this.flags = true;
 var touch ;
 if(event.touches){
 touch = event.touches[0];
 }else {
 touch = event;
 }
 console.log('鼠標點所在位置', touch.clientX,touch.clientY)
 console.log('div左上角位置', event.target.offsetTop,event.target.offsetLeft)
 dx = touch.clientX - event.target.offsetLeft
 dy = touch.clientY - event.target.offsetTop
},

拖拽進行時,將觸控點的位置賦值給 div

// 定位滑塊的位置
this.position.x = touch.clientX - dx;
this.position.y = touch.clientY - dy;
// 限制滑塊超出頁面
// console.log('屏幕大小', screenWidth, screenHeight)
if (this.position.x < 0) {
 this.position.x = 0
} else if (this.position.x > screenWidth - touch.target.clientWidth) {
 this.position.x = screenWidth - touch.target.clientWidth
}
if (this.position.y < 0) {
 this.position.y = 0
} else if (this.position.y > screenHeight - touch.target.clientHeight) {
 this.position.y = screenHeight - touch.target.clientHeight
}

拖拽結束

//鼠標釋放時候的函數(shù)
end(){
 console.log('end')
 this.flags = false;
},

全部代碼

<template>
 <div class="floatball" id="floatball"
 @mousedown="down" @touchstart.stop="down"
 @mousemove="move" @touchmove.stop="move"
 @mouseup="end" @touchend.stop="end"
 :style="{top:position.y+'px', left:position.x+'px'}">
 獎勵規(guī)則
 </div>
</template>

<script>
// 鼠標位置和div的左上角位置 差值
var dx,dy
var screenWidth = window.screen.width
var screenHeight = window.screen.height

export default {
 data() {
 return {
 flags: false,
 position: {
 x: 320,
 y: 60
 },
 }
 },
 

 methods: {
 // 實現(xiàn)移動端拖拽
 down(event){
 this.flags = true;
 var touch ;
 if(event.touches){
 touch = event.touches[0];
 }else {
 touch = event;
 }
 console.log('鼠標點所在位置', touch.clientX,touch.clientY)
 console.log('div左上角位置', event.target.offsetTop,event.target.offsetLeft)
 dx = touch.clientX - event.target.offsetLeft
 dy = touch.clientY - event.target.offsetTop
 },
 move() {
 if (this.flags) {
 var touch ;
 if (event.touches) {
 touch = event.touches[0];
 } else {
 touch = event;
 }
 // 定位滑塊的位置
 this.position.x = touch.clientX - dx;
 this.position.y = touch.clientY - dy;
 // 限制滑塊超出頁面
 // console.log('屏幕大小', screenWidth, screenHeight )
 if (this.position.x < 0) {
 this.position.x = 0
 } else if (this.position.x > screenWidth - touch.target.clientWidth) {
 this.position.x = screenWidth - touch.target.clientWidth
 }
 if (this.position.y < 0) {
 this.position.y = 0
 } else if (this.position.y > screenHeight - touch.target.clientHeight) {
 this.position.y = screenHeight - touch.target.clientHeight
 }
 //阻止頁面的滑動默認事件
 document.addEventListener("touchmove",function(){
 event.preventDefault();
 },false);
 }
 },
 //鼠標釋放時候的函數(shù)
 end(){
 console.log('end')
 this.flags = false;
 },

 }
 
}
</script>

<style>
 .floatball{
 color:white;
 height:50px;
 width: 50px;
 padding: 5px;
 z-index: 990;
 position: fixed;
 top: 60px;
 right: 320px;
 border-radius: 50%;
 background-color: rgba(29, 157, 237,0.8);
 }

</style>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Vue2中使用tailwindCss的詳細教程

    Vue2中使用tailwindCss的詳細教程

    Tailwind CSS是一個流行的前端CSS框架,它基于原子設計原則,提供了一套預構建的CSS樣式類,旨在幫助開發(fā)者快速地創(chuàng)建響應式、可定制的用戶界面,本文給大家介紹了Vue2中使用tailwindCss的詳細教程,需要的朋友可以參考下
    2024-09-09
  • Vue3中創(chuàng)建異步組件的流程步驟

    Vue3中創(chuàng)建異步組件的流程步驟

    在現(xiàn)代前端開發(fā)中,組件的重用性和異步加載是提升用戶體驗和優(yōu)化性能的關鍵因素,在Vue 3中,創(chuàng)建異步組件變得更為便利,本文將探討如何在Vue 3中使用setup語法糖來創(chuàng)建異步組件,感興趣的小伙伴跟著小編一起來看看吧
    2024-09-09
  • Vue.js每天必學之構造器與生命周期

    Vue.js每天必學之構造器與生命周期

    Vue.js每天必學之構造器與生命周期,告訴大家什么是Vue.js構造器與生命周期,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Vue配合Vant使用時area省市區(qū)選擇器的使用方式

    Vue配合Vant使用時area省市區(qū)選擇器的使用方式

    這篇文章主要介紹了Vue配合Vant使用時area省市區(qū)選擇器的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 基于 Vue.js 之 iView UI 框架非工程化實踐記錄(推薦)

    基于 Vue.js 之 iView UI 框架非工程化實踐記錄(推薦)

    為了快速體驗 MVVM 模式,我選擇了非工程化方式來起步,并選擇使用 Vue.js,以及基于它構建的 iView UI 框架。本文給大家分享基于 Vue.js 之 iView UI 框架非工程化實踐記錄,需要的朋友參考下吧
    2017-11-11
  • vue.js內(nèi)部自定義指令與全局自定義指令的實現(xiàn)詳解(利用directive)

    vue.js內(nèi)部自定義指令與全局自定義指令的實現(xiàn)詳解(利用directive)

    這篇文章主要給大家介紹了關于vue.js內(nèi)部自定義指令與全局自定義指令的實現(xiàn)方法,vue.js中實現(xiàn)自定義指令的主要是利用directive,directive這個單詞是我們寫自定義指令的關鍵字,需要的朋友們下面跟著小編來一起學習學習吧。
    2017-07-07
  • Vue中保存用戶登錄狀態(tài)實例代碼

    Vue中保存用戶登錄狀態(tài)實例代碼

    本篇文章主要介紹了Vue中保存用戶登錄狀態(tài)實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。、
    2017-06-06
  • vue使用echarts圖表自適應的幾種解決方案

    vue使用echarts圖表自適應的幾種解決方案

    這篇文章主要給大家介紹了關于vue使用echarts圖表自適應的幾種解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 詳解vue-router 命名路由和命名視圖

    詳解vue-router 命名路由和命名視圖

    這篇文章主要介紹了詳解vue-router 命名路由和命名視圖,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • vue實現(xiàn)axios圖片上傳功能

    vue實現(xiàn)axios圖片上傳功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)axios圖片上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08

最新評論

丰城市| 金堂县| 小金县| 历史| 金乡县| 永兴县| 章丘市| 玛沁县| 察雅县| 周至县| 雅安市| 丽江市| 彰化市| 寿宁县| 龙陵县| 新乐市| 泸西县| 建昌县| 甘谷县| 通榆县| 木里| 泰兴市| 高安市| 阿拉善盟| 花垣县| 大厂| 耿马| 枝江市| 鲜城| 乌拉特前旗| 麦盖提县| 广灵县| 本溪市| 比如县| 松江区| 枝江市| 历史| 呈贡县| 贞丰县| 错那县| 昂仁县|