vue實(shí)現(xiàn)移動(dòng)端可拖拽式icon圖標(biāo)
本文實(shí)例為大家分享了vue實(shí)現(xiàn)移動(dòng)端可拖拽式icon圖標(biāo)的具體代碼,供大家參考,具體內(nèi)容如下
需求描述:在移動(dòng)端頁(yè)面懸浮一個(gè)可隨便拖拽的圖標(biāo),類(lèi)似于蘋(píng)果手機(jī)的輔助觸控小圓點(diǎn),并且可隨意拖動(dòng)。
預(yù)覽:

組件代碼如下:
<template>
? ? <div class="ys-float-btn" :style="{'width':itemWidth+'px','height':itemHeight+'px','left':left+'px','top':top+'px'}"
? ? ? ? ?ref="div"
? ? ? ? ?@click ="onBtnClicked">
? ? ? ? <slot name="icon"></slot>
? ? ? ? <SvgIcon :iconClass="'changjianwentijieda'"
? ? ? ? ? ? ? ? ?:style="{'width':itemWidth+'px','height':itemHeight+'px'}"/>
? ? </div>
</template>
?
?
<script>
? ? export default {
? ? ? ? name: "DragIcon",
? ? ? ? props:{
? ? ? ? ? ? itemWidth:{
? ? ? ? ? ? ? ? type:Number,
? ? ? ? ? ? ? ? default:40
? ? ? ? ? ? },
? ? ? ? ? ? itemHeight:{
? ? ? ? ? ? ? ? type:Number,
? ? ? ? ? ? ? ? default:40
? ? ? ? ? ? },
? ? ? ? ? ? gapWidth:{
? ? ? ? ? ? ? ? type:Number,
? ? ? ? ? ? ? ? default:10
? ? ? ? ? ? },
?
? ? ? ? ? ? coefficientHeight:{
? ? ? ? ? ? ? ? type:Number,
? ? ? ? ? ? ? ? default:0.8
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? created(){
? ? ? ? ? ? this.clientWidth = document.documentElement.clientWidth;
? ? ? ? ? ? this.clientHeight = document.documentElement.clientHeight;
? ? ? ? ? ? this.left = this.clientWidth - this.itemWidth - this.gapWidth;
? ? ? ? ? ? this.top = this.clientHeight*this.coefficientHeight;
? ? ? ? },
? ? ? ? mounted(){
? ? ? ? ? ? this.$nextTick(()=>{
? ? ? ? ? ? ? ? const div = this.$refs.div;
? ? ? ? ? ? ? ? div.addEventListener("touchstart",(e)=>{
? ? ? ? ? ? ? ? ? ? e.stopPropagation();
? ? ? ? ? ? ? ? ? ? div.style.transition = 'none';
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? div.addEventListener("touchmove",(e)=>{
? ? ? ? ? ? ? ? ? ? ? ? e.stopPropagation();
? ? ? ? ? ? ? ? ? ? ? ? if (e.targetTouches.length === 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? let touch = event.targetTouches[0];
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.left = touch.clientX - this.itemWidth/2;
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.top = touch.clientY - this.itemHeight/2;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? false
? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? div.addEventListener("touchend",(e)=>{
? ? ? ? ? ? ? ? ? ? e.stopPropagation();
? ? ? ? ? ? ? ? ? ? div.style.transition = 'all 0.3s';
? ? ? ? ? ? ? ? ? ? if(this.left>this.clientWidth/2){
? ? ? ? ? ? ? ? ? ? ? ? this.left = this.clientWidth - this.itemWidth - this.gapWidth;
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? this.left = this.gapWidth;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if(this.top<=36)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? this.top=36+this.gapWidth
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else{
? ? ? ? ? ? ? ? ? ? ? ? let bottom=this.clientHeight-50-this.itemHeight-this.gapWidth
? ? ? ? ? ? ? ? ? ? ? ? console.log(bottom,this.top)
? ? ? ? ? ? ? ? ? ? ? ? if(this.top>=bottom)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.top=bottom
? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? },
?
? ? ? ? methods:{
? ? ? ? ? ? onBtnClicked(){
? ? ? ? ? ? ? ? this.$emit("onFloatBtnClicked");
? ? ? ? ? ? },
?
? ? ? ? },
? ? ? ? data(){
? ? ? ? ? ? return{
? ? ? ? ? ? ? ? timer:null,
? ? ? ? ? ? ? ? currentTop:0,
? ? ? ? ? ? ? ? clientWidth:0,
? ? ? ? ? ? ? ? clientHeight:0,
? ? ? ? ? ? ? ? left:0,
? ? ? ? ? ? ? ? top:0,
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>
?
<style lang="scss" scoped>
? ? .ys-float-btn{
? ? ? ? background:rgba(56,181,77,1);
? ? ? ? box-shadow:0 2px 10px 0 rgba(0,0,0,0.1);
? ? ? ? border-radius:50%;
? ? ? ? color: #666666;
? ? ? ? z-index: 20;
? ? ? ? transition: all 0.3s;
? ? ? ? display: flex;
? ? ? ? flex-direction: column;
? ? ? ? justify-content: center;
? ? ? ? align-items: center;
? ? ? ? position: fixed;
? ? ? ? bottom: 20vw;
?
? ? ? ? img{
? ? ? ? ? ? width: 50%;
? ? ? ? ? ? height: 50%;
? ? ? ? ? ? object-fit: contain;
? ? ? ? ? ? margin-bottom: 3px;
? ? ? ? }
? ? }
? ? .su_img{
? ? ? ? width: 40px;
? ? ? ? height: 40px;
? ? ? ? margin: 8px 0 0 0;
? ? }
?
</style>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用vue3搭建后臺(tái)系統(tǒng)的詳細(xì)步驟
這篇文章主要介紹了使用vue3搭建后臺(tái)系統(tǒng)的過(guò)程記錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
解決vue中使用history.replaceState?更改url?vue?router?無(wú)法感知的問(wèn)題
這篇文章主要介紹了vue中使用history.replaceState?更改url?vue?router?無(wú)法感知的問(wèn)題,本文給大家分享修復(fù)這個(gè)問(wèn)題的方法,需要的朋友可以參考下2022-09-09
Vuejs 用$emit與$on來(lái)進(jìn)行兄弟組件之間的數(shù)據(jù)傳輸通信
本篇文章主要介紹了Vuejs 用$emit 與 $on 來(lái)進(jìn)行兄弟組件之間的數(shù)據(jù)傳輸示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
解決vue router使用 history 模式刷新后404問(wèn)題
這篇文章主要介紹了解決vue router使用 history 模式刷新后404問(wèn)題,需要的朋友可以參考下2017-07-07
vue項(xiàng)目中使用window的onresize事件方式
這篇文章主要介紹了vue項(xiàng)目中使用window的onresize事件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue+elementui實(shí)現(xiàn)選項(xiàng)卡功能
這篇文章主要為大家詳細(xì)介紹了vue+elementui實(shí)現(xiàn)選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
springboot?vue接口測(cè)試前端模塊樹(shù)和接口列表
這篇文章主要為大家介紹了springboot?vue接口測(cè)試前端模塊樹(shù)和接口列表,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
vue3中ace-editor的簡(jiǎn)單使用方法實(shí)例
這篇文章主要給大家介紹了關(guān)于vue3中ace-editor簡(jiǎn)單使用的相關(guān)資料,ace-editor是一種代碼編輯器,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10

