使用vue怎么實(shí)現(xiàn)一個(gè)可移動(dòng)的懸浮按鈕?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
Vue具體輕量級(jí)框架、簡單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗(yàn)。
具體內(nèi)容如下
1.html代碼
<div class="callback float" @click="onClick" @mousedown="down" @touchstart="down" @mousemove="move" @touchmove="move" @mouseup="end" @touchend="end" ref="fu" > <!-- <p @click="callback">返回</p> --> <img @click="callback" src="@/assets/images/callbs.jpg" alt /> </div>
2.再data中定義
data() { return { isLoading: false, flags: false, //控制使用 position: { x: 0, y: 0, }, nx: "", ny: "", dx: "", dy: "", xPum: "", yPum: "", }; },
3.js代碼
methods: { 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) { 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"; // } //阻止頁面的滑動(dòng)默認(rèn)事件 document.addEventListener( "touchmove", function () { event.preventDefault(); }, false ); } }, //鼠標(biāo)釋放時(shí)候的函數(shù) end() { this.flags = false; }, onClick() { //在這里我是作為子組件來使用的 this.$emit("click"); }, },
4.style樣式
<style scoped> .callback p { font-size: 16px; color: #fff; background: rgba(56, 57, 58, 0.5); border-radius: 50%; text-align: center; width: 50px; height: 50px; line-height: 50px; font-family: PingFang SC; font-weight: 600; box-shadow: 0 0 10px #fff; } .callback img { display: block; width: 50px; height: 50px; box-shadow: 0 0 10px rgb(133, 129, 129); border-radius: 50%; background: #fff; } .callback { position: fixed; top: 40px; left: 20px; z-index: 99999; } .float { position: fixed; right: 20px; top: 60%; touch-action: none; text-align: center; width: 50px; height: 50px; border-radius: 24px; line-height: 48px; color: white; } </style>
關(guān)于使用vue怎么實(shí)現(xiàn)一個(gè)可移動(dòng)的懸浮按鈕問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
當(dāng)前題目:使用vue怎么實(shí)現(xiàn)一個(gè)可移動(dòng)的懸浮按鈕-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://chinadenli.net/article32/hcosc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)