vue實(shí)現(xiàn)吸壁懸浮球
本文實(shí)例為大家分享了vue實(shí)現(xiàn)吸壁懸浮球的具體代碼,供大家參考,具體內(nèi)容如下
最近接到一個(gè)需求,需要一個(gè)客服懸浮按鈕,可以拖拽并吸壁到頁(yè)面周邊,我們一起看看吧
1.創(chuàng)建一個(gè)template,定義客服按鈕,自定義v-drag指令
<template>
? <div class="container">
? ? <div
? ? ? class="btn"
? ? ? id="btn"
? ? ? :style="{ visibility: dialogVisible ? 'hidden' : 'visible' }"
? ? ? v-drag="{ set: openDialog }"
? ? ? draggable="false"
? ? >
? ? ? <img src="@/assets/images/cust-service.png" alt="" />
? ? ? <span>聯(lián)<br />系<br />客<br />服</span>
? ? </div>
? ? <chat-dialog v-model="dialogVisible" @on-close="closeDialog"></chat-dialog>
? </div>
</template>2.drag指令主要監(jiān)聽el的onmousedown、onmousemove、onmouseup事件,在onmouseup事件通過偏移量判斷是點(diǎn)擊還是拖拽,如果是拖拽計(jì)算偏移量從而判斷吸附于哪個(gè)壁
<script>
import chatDialog from "./chatDialog";
export default {
? components: { chatDialog },
? data() {
? ? return {
? ? ? dialogVisible: false,
? ? };
? },
? mounted() {
? ? // 初始化按鈕位置
? ? let clientW = document.documentElement.clientWidth;
? ? let clientH = document.documentElement.clientHeight;
? ? let div = document.getElementById("btn");
? ? div.style.top = clientH / 2 + "px";
? ? div.style.left = clientW - 80 + "px";
? },
? methods: {
? ? openDialog() {
? ? ? this.dialogVisible = true;
? ? },
? ? closeDialog() {
? ? ? this.dialogVisible = false;
? ? },
? },
? directives: {
? ? drag(el, binding) {
? ? ? let oDiv = el;
? ? ? // 禁止選擇網(wǎng)頁(yè)上的文字
? ? ? document.onselectstart = function () {
? ? ? ? return false;
? ? ? };
? ? ? oDiv.onmousedown = function (e) {
? ? ? ? // 鼠標(biāo)按下,計(jì)算當(dāng)前元素距離可視區(qū)的距離
? ? ? ? let disX = e.clientX - oDiv.offsetLeft;
? ? ? ? let disY = e.clientY - oDiv.offsetTop;
? ? ? ? // 記錄初始位置
? ? ? ? let firstX = oDiv.offsetLeft;
? ? ? ? let firstY = oDiv.offsetTop;
? ? ? ? let l = oDiv.offsetLeft;
? ? ? ? let t = oDiv.offsetTop;
? ? ? ? document.onmousemove = function (e) {
? ? ? ? ? // 通過事件委托,計(jì)算移動(dòng)的距離
? ? ? ? ? l = e.clientX - disX;
? ? ? ? ? t = e.clientY - disY;
? ? ? ? ? // 移動(dòng)當(dāng)前元素
? ? ? ? ? oDiv.style.left = l + "px";
? ? ? ? ? oDiv.style.top = t + "px";
? ? ? ? };
? ? ? ? document.onmouseup = function (e) {
? ? ? ? ? // 點(diǎn)擊事件
? ? ? ? ? if (Math.abs(l - firstX) < 3 || Math.abs(t - firstY) < 3) {
? ? ? ? ? ? binding.value.set(true);
? ? ? ? ? } else {
? ? ? ? ? ? // 拖拽事件
? ? ? ? ? ? let clientW = document.documentElement.clientWidth;
? ? ? ? ? ? let clientH = document.documentElement.clientHeight;
? ? ? ? ? ? // 判斷偏移量,并吸壁
? ? ? ? ? ? // 垂直偏移量大于水平偏移量,左右吸壁,垂直控制范圍
? ? ? ? ? ? if (Math.abs(t - firstY) > Math.abs(l - firstX)) {
? ? ? ? ? ? ? if (l > clientW / 2 - 80) {
? ? ? ? ? ? ? ? l = clientW - 80;
? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? l = 80;
? ? ? ? ? ? ? }
? ? ? ? ? ? ? if (t < 40) t = 40;
? ? ? ? ? ? ? else if (t > clientH - 140) t = clientH - 140;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? // 上下吸壁,水平控制范圍
? ? ? ? ? ? ? if (t > clientH / 2 - 140) {
? ? ? ? ? ? ? ? t = clientH - 140;
? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? t = 40;
? ? ? ? ? ? ? }
? ? ? ? ? ? ? if (l < 80) l = 80;
? ? ? ? ? ? ? else if (l > clientW - 80) l = clientW - 80;
? ? ? ? ? ? }
? ? ? ? ? ? oDiv.style.top = t + "px";
? ? ? ? ? ? oDiv.style.left = l + "px";
? ? ? ? ? }
? ? ? ? ? document.onmousemove = null;
? ? ? ? ? document.onmouseup = null;
? ? ? ? };
? ? ? ? // return false不加的話可能導(dǎo)致黏連,就是拖到一個(gè)地方時(shí)div粘在鼠標(biāo)上不下來(lái),相當(dāng)于onmouseup失效
? ? ? ? return false;
? ? ? };
? ? },
? },
};
</script>3.樣式設(shè)置z-index始終保持在頁(yè)面頂層,定義position定位
<style lang="scss" scoped>
.container {
? .btn {
? ? z-index: 9999;
? ? position: fixed;
? ? width: 40px;
? ? height: 130px;
? ? overflow: hidden;
? ? background: linear-gradient(210deg, #f3f5f8, #ffffff);
? ? border: 2px solid #ffffff;
? ? box-shadow: 3px 5px 10px 0px rgba(0, 0, 0, 0.1);
? ? border-radius: 20px;
? ? text-align: center;
? ? img {
? ? ? margin: 5px 0;
? ? ? width: 30px;
? ? ? height: 30px;
? ? }
? ? span {
? ? ? font-size: 14px;
? ? ? color: #333333;
? ? ? cursor: pointer;
? ? ? font-family: PingFang SC;
? ? }
? }
}
</style>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-pdf插件實(shí)現(xiàn)pdf文檔預(yù)覽方式(自動(dòng)分頁(yè)預(yù)覽)
這篇文章主要介紹了vue-pdf插件實(shí)現(xiàn)pdf文檔預(yù)覽方式(自動(dòng)分頁(yè)預(yù)覽),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
如何使用Vue3.2+Vite2.7從0快速打造一個(gè)UI組件庫(kù)
構(gòu)建工具使用vue3推薦的vite,下面這篇文章主要給大家介紹了關(guān)于如何使用Vue3.2+Vite2.7從0快速打造一個(gè)UI組件庫(kù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能
這篇文章主要介紹了vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-11-11
vue3配置Element及element-plus/lib/theme-chalk/index.css報(bào)錯(cuò)的解決
這篇文章主要介紹了vue3配置Element及element-plus/lib/theme-chalk/index.css報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue通過tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定
這篇文章主要介紹了vue通過tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定,文中給大家介紹了一些常用類名語(yǔ)法記錄,對(duì)vue動(dòng)態(tài)綁定class相關(guān)知識(shí)感興趣的朋友一起看看吧2023-07-07
利用FetchEventSource在大模型流式輸出的應(yīng)用方式
這篇文章主要介紹了利用FetchEventSource在大模型流式輸出的應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue使用iframe實(shí)現(xiàn)瀏覽器打印兼容性優(yōu)化
在前端開發(fā)中,打印功能是一個(gè)常見的需求,但不同瀏覽器對(duì)打印樣式的支持差異較大,本文我們來(lái)看看Vue如何使用iframe實(shí)現(xiàn)瀏覽器打印兼容性優(yōu)化吧2025-04-04

