关于拖拽和定位的问题

2020-08-27 21:55:01 +08:00
 aglsv

设置了绝对定位让 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
            }
        }


1890 次点击
所在节点    HTML
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/702030

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX