js 페이지에서 요소 임의로 드래그

1686 단어 프런트 엔드
function moveanyway () {

//  



var block = document.getElementById('myCoups')

if (block) {

var oW, oH

//  touchstart 

block.addEventListener('touchstart', function (e) {

var touches = e.touches[0]

oW = touches.clientX - block.offsetLeft

oH = touches.clientY - block.offsetTop

//  

document.addEventListener('touchmove', defaultEvent, { passive: false })

}, false)



block.addEventListener('touchmove', function (e) {

var touches = e.touches[0]

var oLeft = touches.clientX - oW

var oTop = touches.clientY - oH

if (oLeft < 10) {

oLeft = 10

if (oTop <= 10) {

oTop = 10

} else if (oTop >= document.documentElement.clientHeight - block.offsetHeight - 10) {

oTop = document.documentElement.clientHeight - block.offsetHeight - 10

}

} else if (oLeft > document.documentElement.clientWidth - block.offsetWidth - 10) {

oLeft = (document.documentElement.clientWidth - block.offsetWidth - 10)

if (oTop <= 10) {

oTop = 10

} else if (oTop >= document.documentElement.clientHeight - block.offsetHeight - 10) {

oTop = document.documentElement.clientHeight - block.offsetHeight - 10

}

} else if (oTop < 10) {

oTop = 10

} else if (oTop > document.documentElement.clientHeight - block.offsetHeight - 10) {

oTop = document.documentElement.clientHeight - block.offsetHeight - 10

}

block.style.left = oLeft + 'px'

block.style.top = oTop + 'px'

}, false)



block.addEventListener('touchend', function () {

document.removeEventListener('touchmove', defaultEvent, { passive: false })

}, false)

}



function defaultEvent (e) {

e.preventDefault()

}

좋은 웹페이지 즐겨찾기