V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Dive into HTML5
http://diveintohtml5.org/
aglsv
V2EX  ›  HTML

关于拖拽和定位的问题

  •  
  •   aglsv · 2020-08-27 21:55:01 +08:00 · 1863 次点击
    这是一个创建于 1309 天前的主题,其中的信息可能已经有所发展或是发生改变。

    设置了绝对定位让 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
                }
            }
    
    
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1176 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:27 · PVG 02:27 · LAX 11:27 · JFK 14:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.