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

vue實(shí)現(xiàn)移動(dòng)端觸屏拖拽功能

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

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

1 首先創(chuàng)建一個(gè)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'}">
 獎(jiǎng)勵(lì)規(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)備四個(gè)變量

1)、屏幕長(zhǎng)

var screenHeight = window.screen.height

2)、屏幕寬

var screenWidth = window.screen.width

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

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

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

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

拖拽進(jìn)行時(shí),將觸控點(diǎn)的位置賦值給 div

// 定位滑塊的位置
this.position.x = touch.clientX - dx;
this.position.y = touch.clientY - dy;
// 限制滑塊超出頁(yè)面
// 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í)候的函數(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'}">
 獎(jiǎng)勵(lì)規(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: {
 // 實(shí)現(xiàn)移動(dòng)端拖拽
 down(event){
 this.flags = true;
 var touch ;
 if(event.touches){
 touch = event.touches[0];
 }else {
 touch = event;
 }
 console.log('鼠標(biāo)點(diǎn)所在位置', 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;
 // 限制滑塊超出頁(yè)面
 // 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
 }
 //阻止頁(yè)面的滑動(dòng)默認(rèn)事件
 document.addEventListener("touchmove",function(){
 event.preventDefault();
 },false);
 }
 },
 //鼠標(biāo)釋放時(shí)候的函數(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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕(操作方法)

    vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕(操作方法)

    這篇文章主要介紹了vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • vue3?ref獲取組件實(shí)例詳細(xì)圖文教程

    vue3?ref獲取組件實(shí)例詳細(xì)圖文教程

    在Vue3中可以使用ref函數(shù)來創(chuàng)建一個(gè)響應(yīng)式的變量,通過將ref函數(shù)應(yīng)用于一個(gè)組件實(shí)例,我們可以獲取到該組件的實(shí)例對(duì)象,這篇文章主要給大家介紹了關(guān)于vue3?ref獲取組件實(shí)例的詳細(xì)圖文教程,需要的朋友可以參考下
    2023-10-10
  • vue實(shí)現(xiàn)井字棋游戲

    vue實(shí)現(xiàn)井字棋游戲

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)井字棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 詳解前后端分離之VueJS前端

    詳解前后端分離之VueJS前端

    本篇文章主要介紹了詳解前后端分離之VueJS前端,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • vue基礎(chǔ)之ElementUI表格詳解

    vue基礎(chǔ)之ElementUI表格詳解

    這篇文章主要為大家詳細(xì)介紹了vue的ElementUI表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • element-ui復(fù)雜table表格動(dòng)態(tài)新增列、動(dòng)態(tài)調(diào)整行以及列順序詳解

    element-ui復(fù)雜table表格動(dòng)態(tài)新增列、動(dòng)態(tài)調(diào)整行以及列順序詳解

    這篇文章主要給大家介紹了關(guān)于element-ui復(fù)雜table表格動(dòng)態(tài)新增列、動(dòng)態(tài)調(diào)整行以及列順序的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • vue 引用自定義ttf、otf、在線字體的方法

    vue 引用自定義ttf、otf、在線字體的方法

    這篇文章主要介紹了vue 引用自定義ttf、otf、在線字體的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Vue頁(yè)面骨架屏注入方法

    Vue頁(yè)面骨架屏注入方法

    這篇文章主要介紹了Vue頁(yè)面骨架屏注入的操作,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • 使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法

    使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法

    這篇文章主要介紹了使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法,需要的朋友可以參考下
    2017-12-12
  • vue中拆分組件的實(shí)戰(zhàn)案例

    vue中拆分組件的實(shí)戰(zhàn)案例

    最近在學(xué)vue,試著寫個(gè)單頁(yè)應(yīng)用,在寫組件,拆分組件時(shí)遇到一些困惑,下面這篇文章主要給大家介紹了關(guān)于vue中拆分組件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12

最新評(píng)論