1.代码

 var disX=0;
    var disY=0;
    $('.private_area').on('mousedown',function(event) {
        disX=event.clientX-$('.private_area')[0].offsetLeft;
        disY=event.clientY-$('.private_area')[0].offsetTop;

        $(document).mousemove(function(event) {
            var l=event.clientX-disX;
            var t=event.clientY-disY;
            if(l<0)
            {
                l=0;
            }
            else if(l>document.documentElement.clientWidth-$('.private_area')[0].offsetWidth)
            {
                l=document.documentElement.clientWidth-$('.private_area')[0].offsetWidth;
            }
            if(t<0)
            {
                t=0;
            }
            else if(t>document.documentElement.clientHeight-$('.private_area')[0].offsetHeight)
            {
                t=document.documentElement.clientHeight-$('.private_area')[0].offsetHeight;
            }
            $('.private_area').css({
                left: l,
                top: t,
                marginTop:0,
                marginLeft:0
            });
        });
        $(document).mouseup(function(event) {
            $(document).off('mousemove');
            $(document).off('mouseup');
        });
        event.stopPropagation();
    });
    
    注:绑定class元素即可

mmy123456
376 声望17 粉丝

有项目请联系:15201970281(毛毛)