关于拖拽和定位的问题
資深大佬 : aglsv 7
设置了绝对定位让 div 在页面水平垂直居中,完了在设置了拖拽之后出了问题,拖拽的时候会飘,去掉 top: 0;left: 0;right: 0;bottom: 0;的时候就可以正常拖拽了,这种情况该怎么办啊
#login{ width: 500px; height: 300px; text-align: center; border: blueviolet 1px solid; position: absolute; margin: auto; top: 0; left: 0; right: 0; bottom: 0; display: none; }
var login = document.getElementById('login') login.onmousedown = function(e){ var l = e.clientX - login.offsetLeft var t = e.clientY - login.offsetTop document.documentElement.onmousemove = function(e){ var x = e.clientX - l; var y = e.clientY - t if(x <= 0){ x = 0 } if(y<=0){ y = 0 } if(x >= document.documentElement.clientWidth - login.clientWidth){ x = document.documentElement.clientWidth - login.clientWidth } if(y >= document.documentElement.clientHeight - login.clientHeight){ y = document.documentElement.clientHeight - login.clientHeight } login.style.left = x + 'px' login.style.top = y + 'px' } document.documentElement.onmouseup = function(e){ document.documentElement.onmousemove = null } }
大佬有話說 (0)