$('.box').mousedown(function (e) {
let x = e.pageX;
let y = e.pageY;
let hz = x - $('.box').offset().left
let ht = y - $('.box').offset().top
$(document).mousemove(function (e) {
let xx = e.pageX - hz
let yy = e.pageY - ht
let you = $(window).width() - $('.box').width()
let tou = $(window).height() - $('.box').height()
if (xx < 0) { xx = 0 }
if (yy < 0) { yy = 0 }
if (xx > you) { xx = you }
if (yy > tou) { yy = tou }
$('.box').css('left', xx + 'px')
$('.box').css('top', yy + 'px')
console.log(xx, yy);
})
});
$('.box').mouseup(function () {
$(document).off('mousemove')
})