vue 悬浮图标_vue实现移动端悬浮窗效果

本文介绍了如何在Vue移动端应用中实现类似iPhone悬浮窗的效果,详细讲解了利用touch和mouse事件来实现拖拽功能,包括关键的HTML结构、JS逻辑和CSS样式。通过监听touchstart、touchmove、touchend等事件,结合CSS固定定位,实现了可拖动的悬浮图标。
摘要由CSDN通过智能技术生成

本文讲述,在使用VUE的移动端实现类似于iPhone的悬浮窗的效果。

相关知识点

touchstart当在屏幕上按下手指时触发

touchmove 当在屏幕上移动手指时触发

touchend 当在屏幕上抬起手指时触发

mousedown mousemove mouseup对应的是PC端的事件

touchcancel当一些更高级别的事件发生的时候(如电话接入或者弹出信息)会取消当前的touch操作,即触发touchcancel。一般会在touchcancel时暂停游戏、存档等操作。

效果图

实现步骤

1.html

总结了一下评论,好像发现大家都碰到了滑动的问题。就在这里提醒一下吧。可将该悬浮 DIV 同你的 scroller web 同级。 —- (log: 2018-08-21)

html结构:

你的web页面
悬浮DIV

...

你的web页面

@mousedown="down" @touchstart="down"

@mousemove="move" @touchmove="move"

@mouseup="end" @touchend="end"

>

{{pageInfo.totalPage}}

...

2.JS

data() {

return {

flags: false,

position: { x: 0, y: 0 },

nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',

}

}

methods: {

// 实现移动端拖拽

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 = moveDiv.offsetLeft;

this.dy = moveDiv.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;

moveDiv.style.left = this.xPum+"px";

moveDiv.style.top = this.yPum +"px";

//阻止页面的滑动默认事件;如果碰到滑动问题,1.2 请注意是否获取到 touchmove

document.addEventListener("touchmove",function(){

event.preventDefault();

},false);

}

},

//鼠标释放时候的函数

end(){

this.flags = false;

},

}

3.CSS

.xuanfu {

height: 4.5rem;

width: 4.5rem;

/* 如果碰到滑动问题,1.3 请检查 z-index。z-index需比web大一级*/

z-index: 999;

position: fixed;

top: 4.2rem;

right: 3.2rem;

border-radius: 0.8rem;

background-color: rgba(0, 0, 0, 0.55);

}

.yuanqiu {

height: 2.7rem;

width: 2.7rem;

border: 0.3rem solid rgba(140, 136, 136, 0.5);

margin: 0.65rem auto;

color: #000000;

font-size: 1.6rem;

line-height: 2.7rem;

text-align: center;

border-radius: 100%;

background-color: #ffffff;

}

实现好JS逻辑,基本上,问题不大。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值