const box = document.querySelector('#box')
let dragging = false
let position = null
box.addEventListener('mousedown', function (e) {
dragging = true
position = [e.clientX, e.clientY]
console.log(1)
})
document.addEventListener('mousemove', function (e) {
if (dragging === false) { return }
const x = e.clientX
const y = e.clientY
const diffX = x - position[0]
const diffY = y - position[1]
let left = parseInt(box.style.left || 0)
let top = parseInt(box.style.top || 0)
if (left < 0) {
left = 0
} else if (left > window.innerWidth - box.offsetWidth) {
left = window.innerWidth - box.offsetWidth
}
if (top < 0) {
top = 0
} else if (top > window.innerHeight - box.offsetHeight) {
top = window.innerHeight - box.offsetHeight
}
box.style.left = diffX + left + 'px'
box.style.top = diffY + top + 'px'
position = [x, y]
console.log(2)
})
document.addEventListener('mouseup', function (e) {
dragging = false
console.log(3)
})