js 实现手机端任意拖拽悬浮窗,在手机屏幕范围任意移动,始终居右显示

1.css:
#demo{
border-radius: 1rem;
position: fixed;
height: 1rem;
width: 1rem;
right: 0;
z-index: 999999;
bottom: .35rem;
text-align: center;
font-size: 0;
}
2.页面:


3.js代码:
function initDialog(){
let demo= $("#demo");
let contW = $("#demo").width();
let contH = $("#demo").height();
let startX, startY, sX, sY, moveX, moveY, disX, disY;
let winW = $(window).width();
let winH = $(window).height();
demo.on({
touchstart: function(e) {
startX = e.originalEvent.targetTouches[0].pageX;
startY = e.originalEvent.targetTouches[0].pageY;
sX = $(this).offset().left;
sY = $(this).offset().top;
leftX = startX - sX;
rightX = winW - contW + leftX;
topY = startY - sY
bottomY = winH - contH + topY -20;
},
touchmove: function(e) {
e.preventDefault();
moveX = e.originalEvent.targetTouches[0].clientX;
moveY = e.originalEvent.targetTouches[0].clientY;
if (moveX < leftX) {
moveX = leftX;
}
if (moveX > rightX) {
moveX = rightX;
}
if (moveY < topY) {
moveY = topY;
}
if (moveY > bottomY) {
moveY = bottomY;
}
$(this).css({
“left”: moveX + sX - startX,
“top”: moveY + sY - startY,
});
},
touchend:function(e){
$(this).css({
“left”: “”,
“top”: moveY + sY - startY,
“right”:“0”
});
}
});
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值