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

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

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

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 附上事件

準(zhǔn)備四個變量

1)、屏幕長

var screenHeight = window.screen.height

2)、屏幕寬

var screenWidth = window.screen.width

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

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

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

down(event){
 this.flags = true;
 var touch ;
 if(event.touches){
 touch = event.touches[0];
 }else {
 touch = event;
 }
 console.log('鼠標(biāo)點所在位置', 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
}

拖拽結(jié)束

//鼠標(biāo)釋放時候的函數(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>
// 鼠標(biāo)位置和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('鼠標(biāo)點所在位置', 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
 }
 //阻止頁面的滑動默認(rèn)事件
 document.addEventListener("touchmove",function(){
 event.preventDefault();
 },false);
 }
 },
 //鼠標(biāo)釋放時候的函數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue2中使用tailwindCss的詳細(xì)教程

    Vue2中使用tailwindCss的詳細(xì)教程

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

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

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

    Vue.js每天必學(xué)之構(gòu)造器與生命周期

    Vue.js每天必學(xué)之構(gòu)造器與生命周期,告訴大家什么是Vue.js構(gòu)造器與生命周期,感興趣的小伙伴們可以參考一下
    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,以及基于它構(gòu)建的 iView UI 框架。本文給大家分享基于 Vue.js 之 iView UI 框架非工程化實踐記錄,需要的朋友參考下吧
    2017-11-11
  • vue.js內(nèi)部自定義指令與全局自定義指令的實現(xiàn)詳解(利用directive)

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

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

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

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

    vue使用echarts圖表自適應(yīng)的幾種解決方案

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

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

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

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

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

最新評論

南宫市| 鄂州市| 乐昌市| 旌德县| 惠东县| 色达县| 阳曲县| 桓台县| 南汇区| 清水河县| 北海市| 新兴县| 西乡县| 乐山市| 揭东县| 盘锦市| 平阳县| 黄山市| 瓦房店市| 冷水江市| 鹤山市| 苏州市| 安泽县| 金湖县| 宾川县| 巢湖市| 建平县| 汝州市| 望奎县| 元朗区| 冕宁县| 灌南县| 遵义县| 客服| 乐至县| 卢氏县| 白水县| 巢湖市| 兴文县| 渭源县| 白城市|