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

vue實(shí)現(xiàn)吸壁懸浮球

 更新時(shí)間:2022年04月15日 15:20:34   作者:LvyYoung  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)吸壁懸浮球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)聽(tīng)el的onmousedownonmousemove、onmouseup事件,在onmouseup事件通過(guò)偏移量判斷是點(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) {
? ? ? ? ? // 通過(guò)事件委托,計(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ù)覽)

    這篇文章主要介紹了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ù)

    如何使用Vue3.2+Vite2.7從0快速打造一個(gè)UI組件庫(kù)

    構(gòu)建工具使用vue3推薦的vite,下面這篇文章主要給大家介紹了關(guān)于如何使用Vue3.2+Vite2.7從0快速打造一個(gè)UI組件庫(kù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能

    vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能

    這篇文章主要介紹了vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • vue3.0和vue2.0的區(qū)別詳細(xì)講解

    vue3.0和vue2.0的區(qū)別詳細(xì)講解

    vue經(jīng)歷從2.0到3.0更新之后,簡(jiǎn)??之就是變得更輕更快,使?起來(lái)更加?便,下面這篇文章主要給大家介紹了關(guān)于vue3.0和vue2.0區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • vue3配置Element及element-plus/lib/theme-chalk/index.css報(bào)錯(cuò)的解決

    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通過(guò)tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定

    vue通過(guò)tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定

    這篇文章主要介紹了vue通過(guò)tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定,文中給大家介紹了一些常用類名語(yǔ)法記錄,對(duì)vue動(dòng)態(tài)綁定class相關(guān)知識(shí)感興趣的朋友一起看看吧
    2023-07-07
  • Vue3?入口文件createApp函數(shù)詳解

    Vue3?入口文件createApp函數(shù)詳解

    這篇文章主要介紹了Vue3?入口文件createApp函數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • 利用FetchEventSource在大模型流式輸出的應(yīng)用方式

    利用FetchEventSource在大模型流式輸出的應(yīng)用方式

    這篇文章主要介紹了利用FetchEventSource在大模型流式輸出的應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • vue的指令和插值問(wèn)題匯總

    vue的指令和插值問(wèn)題匯總

    Vue?是一套用于構(gòu)建用戶界面的漸進(jìn)式框架,Vue?的核心庫(kù)只關(guān)注視圖層,不僅易于上手,還便于與第三方庫(kù)或既有項(xiàng)目整合,這篇文章主要介紹了vue的指令和插值總結(jié),需要的朋友可以參考下
    2022-10-10
  • Vue使用iframe實(shí)現(xiàn)瀏覽器打印兼容性優(yōu)化

    Vue使用iframe實(shí)現(xiàn)瀏覽器打印兼容性優(yōu)化

    在前端開(kāi)發(fā)中,打印功能是一個(gè)常見(jiàn)的需求,但不同瀏覽器對(duì)打印樣式的支持差異較大,本文我們來(lái)看看Vue如何使用iframe實(shí)現(xiàn)瀏覽器打印兼容性優(yōu)化吧
    2025-04-04

最新評(píng)論

儋州市| 胶南市| 吉首市| 卓尼县| 自治县| 偏关县| 山丹县| 寿光市| 云霄县| 平潭县| 司法| 元阳县| 舟曲县| 巫山县| 奉节县| 文成县| 曲麻莱县| 会宁县| 钟山县| 武乡县| 浦城县| 广饶县| 黑龙江省| 永和县| 邹城市| 新巴尔虎左旗| 蕉岭县| 万源市| 东兰县| 新乡县| 京山县| 方正县| 且末县| 丹江口市| 乐昌市| 多伦县| 长岛县| 建湖县| 增城市| 房产| 宣城市|