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”
});
}
});
};