代码实现如下:

<style>
#my_div{position:fixed;display:block;top:0;}
</style>
<div id="my_div"></div>
<script type="text/javascript">
$(function(){
var cont=$("#my_div");
var contW=$("#my_div").width();
var contH=$("#my_div").height();
var startX,startY,sX,sY,moveX,moveY;
var winW=$(window).width();
var winH=$(window).height();
cont.on({ //绑定事件
touchstart:function(e){
startX = e.originalEvent.targetTouches[0].pageX; //获取点击点的X坐标
startY = e.originalEvent.targetTouches[0].pageY; //获取点击点的Y坐标
//console.log("startX="+startX+"************startY="+startY);
sX=$(this).offset().left;//相对于当前窗口X轴的偏移量
sY=$(this).offset().top;//相对于当前窗口Y轴的偏移量
//console.log("sX="+sX+"***************sY="+sY);
leftX=startX-sX;//鼠标所能移动的最左端是当前鼠标距div左边距的位置
rightX=winW-contW+leftX;//鼠标所能移动的最右端是当前窗口距离减去鼠标距div最右端位置
topY=startY-sY;//鼠标所能移动最上端是当前鼠标距div上边距的位置
bottomY=winH-contH+topY;//鼠标所能移动最下端是当前窗口距离减去鼠标距div最下端位置
},
touchmove:function(e){
e.preventDefault();
moveX=e.originalEvent.targetTouches[0].pageX;//移动过程中X轴的坐标
moveY=e.originalEvent.targetTouches[0].pageY;//移动过程中Y轴的坐标
//console.log("moveX="+moveX+"************moveY="+moveY);
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,
})
}
})
})
</script>
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。