VUE PC端可拖動(dòng)懸浮按鈕的實(shí)現(xiàn)代碼
一、實(shí)現(xiàn)效果:

二、FloatButton.vue
<template>
<div>
<div class="sssss">
<div class="callback float" @mousedown="down" @touchstart="down" @mousemove="move" @touchmove="move" @mouseover="over" @mouseout="out"
@mouseup="end" @touchend="end" ref="fu" style="color: #1a1919;">
<el-button circle @click="screen()">
{{name}}
</el-button>
</div>
</div>
</div>
</template>
<script>
export default {
name: "suspensionBall",
props: {
name: {
type: String,
default: '打開(kāi)全屏'
},
},
data() {
return {
left: 0,
top: 40,
bg: 1,
menu: false,
isLoading: false,
flags: false, //控制使用
position: {
x: 0,
y: 0,
},
nx: "",
ny: "",
dx: "",
dy: "",
xPum: "",
yPum: "",
movb: 1,
num: 1,
};
},
created() { },
mounted() {
this.left = this.$refs.fu.offsetLeft - 750;
},
methods: {
out2() {
this.menu = false;
},
over2() { },
out() {
this.bg = 2;
},
over() {
this.menu = true;
this.num = 2;
this.bg = 1;
},
callback() {
this.$router.go(-1);
},
onRefresh() {
// window.location.reload();
setTimeout((res) => {
console.log(res);
this.isLoading = false;
}, 1000);
},
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 = this.$refs.fu.offsetLeft;
this.dy = this.$refs.fu.offsetTop;
},
move() {
if (this.flags) {
this.movb = 2;
this.menu = false;
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;
let width = window.innerWidth - this.$refs.fu.offsetWidth; //屏幕寬度減去自身控件寬度
let height = window.innerHeight - this.$refs.fu.offsetHeight; //屏幕高度減去自身控件高度
this.xPum < 0 && (this.xPum = 0);
this.yPum < 0 && (this.yPum = 0);
this.xPum > width && (this.xPum = width);
this.yPum > height && (this.yPum = height);
// if (this.xPum >= 0 && this.yPum >= 0 && this.xPum<= width &&this.yPum<= height) {
this.$refs.fu.style.left = this.xPum + "px";
this.$refs.fu.style.top = this.yPum + "px";
this.left = this.xPum - 750;
this.top = this.yPum;
// }
//阻止頁(yè)面的滑動(dòng)默認(rèn)事件
document.addEventListener(
"touchmove",
function () {
event.preventDefault();
},
false
);
}
},
//鼠標(biāo)釋放時(shí)候的函數(shù)
end() {
this.flags = false;
},
screen() {
this.$emit("changeClick");
},
},
};
</script>
<style scoped>
.callback {
position: fixed;
width: 200px;
height: 20px;
background-repeat: no-repeat;
background-size: 100% 100%;
top: 200px;
left: 90%;
z-index: 99999;
}
.float {
position: fixed;
touch-action: none;
text-align: center;
border-radius: 24px;
line-height: 48px;
color: white;
}
.menuclass {
text-align: left;
position: absolute;
color: #000;
width: 764px;
background: #ffffff;
box-shadow: 0px 6px 26px 1px rgba(51, 51, 51, 0.16);
padding: 20px;
}
.sssss {
position: relative;
background-color: #000;
right: 0;
z-index: 99999;
}
.titlea {
font-size: 18px;
font-family: Microsoft YaHei-Bold, Microsoft YaHei;
font-weight: bold;
color: #333333;
}
.boxa {
display: flex;
flex-wrap: wrap;
margin-top: 20px;
z-index: 999999;
}
.item {
width: 168px;
height: 75px;
border-radius: 4px 4px 4px 4px;
font-size: 16px;
font-family: Microsoft YaHei-Bold, Microsoft YaHei;
font-weight: bold;
color: #ffffff;
text-align: center;
margin-left: 7px;
line-height: 75px;
}
</style>三、父頁(yè)面引用
<template>
<div>
<float-button ref="floatButton" :name="buttonName" @changeClick="screen" />
</div>
</template>
<script>
import FloatButton from './chart/FloatButton'
export default {
name: 'Index',
components: {
FloatButton
},
}
</script>到此這篇關(guān)于VUE PC端可拖動(dòng)懸浮按鈕的文章就介紹到這了,更多相關(guān)vue拖動(dòng)懸浮按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3項(xiàng)目中使用自適應(yīng)Rem示例
這篇文章主要為大家介紹了Vue3項(xiàng)目中使用自適應(yīng)Rem示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
使用Vue后如何針對(duì)搜索引擎做SEO優(yōu)化
本文介紹了Vue.js在SEO優(yōu)化方面的挑戰(zhàn),并提供了一些方法來(lái)解決這些問(wèn)題,包括使用服務(wù)器端渲染、預(yù)渲染和使用VueRouter的History模式來(lái)生成靜態(tài)HTML頁(yè)面,以及添加meta信息和內(nèi)部鏈接來(lái)提高搜索引擎的索引和排名2025-02-02
Vue?quill-editor?編輯器使用及自定義toobar示例詳解
這篇文章主要介紹了Vue quill-editor編輯器使用及自定義toobar示例詳解,這里講解編輯器quil-editor的知識(shí)結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
Vue實(shí)現(xiàn)自定義組件改變組件背景色(示例代碼)
要實(shí)現(xiàn) Vue 自定義組件改變組件背景色,你可以通過(guò) props 將背景色作為組件的一個(gè)屬性傳遞給組件,在組件內(nèi)部監(jiān)聽(tīng)這個(gè)屬性的變化,并將其應(yīng)用到組件的樣式中,下面通過(guò)示例代碼介紹Vue如何實(shí)現(xiàn)自定義組件改變組件背景色,感興趣的朋友一起看看吧2024-03-03
Vue實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)el-select組件的示例代碼
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)el-select組件的方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的的可以參考一下2023-02-02
使用vite搭建ssr活動(dòng)頁(yè)架構(gòu)的實(shí)現(xiàn)
本文主要介紹了使用vite搭建ssr活動(dòng)頁(yè)架構(gòu),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Vue vxe-table使用問(wèn)題收錄小結(jié)
這篇文章主要為大家介紹了Vue vxe-table使用問(wèn)題收錄小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

