前端学习笔记day14 offset client scroll 应用实例一----登录窗口
<!-- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #box { width: 200px; height: 170px; position: absolute; margin: 100px; background-color: yellow; } #title { width: 100%; height: 50px; background-color: grey; text-align: center; line-height: 50px; } #footer { width: 100%; height: 120px; background-color: pink; } </style> </head> <body> <div id='box'> <div id='title'>登录窗口</div> <div id='footer'></div> </div> <script> var box = document.getElementById('box'); var title = document.getElementById('title'); var footer = document.getElementById('footer'); title.onmousedown = function(e) { e = e || window.event; var x = e.pageX - box.offsetLeft; var y = e.pageY - box.offsetTop; document.onmousemove = function(e) { var boxX = e.pageX - x; var boxY = e.pageY - y; console.log(boxX); console.log(boxY); box.style.left = boxX - 100 + 'px'; box.style.top = boxY -100 + 'px'; } } document.onmouseup = function(e) { document.onmousemove = null; } </script> </body> </html> --> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #header { position: absolute; width: 160px; height: 90px; left: 650px; top: 20px; } #link { font-size: 20px; text-decoration: none; color: #000; text-align: center; } #box { width: 460px; height: 250px; background-color: lightgrey; position: absolute; margin-left: 500px; margin-top: 100px; z-index: 2; display: none; } #box div { margin-left: 30px; } #box #title { width: 100%; height: 40px; font-size: 18px; text-align: center; line-height: 40px; margin-left: 0px; } #box div:nth-child(2) { margin-top: 40px; width: 300px; height: 60px; } input { width: 200px; height: 26px; border: 0px; outline: none; } #box div:nth-child(4) input { margin: 35px 131px; height: 29px; width: 111px; border: 0px; outline: none; } #close { width: 40px; height: 40px; background: rgba(226,219,219,0.6); position: absolute; right: -9px; top: -15px; border-radius: 50%; font-size: 16px; line-height: 40px; text-align: center; } #bg { width: 100%; height: 100%; position: absolute; z-index: 1; background-color: rgba(226,219,219,0.6); display: none; } </style> </head> <body> <div id='header'><a href="javascript:void(0);" id='link'>点击,弹出登录框</a></div> <div id='box'> <div id='title'>登录会员</div> <div>用户名: <input type="text" name="" placeholder='请输入用户名'></div> <div>密码: <input type="text" placeholder='请输入密码'></div> <div><input type="button" name=""value='登录会员'></div> <div id='close'>关闭</div> </div> <div id='bg'></div> <script> var link = document.getElementById('link'); var box = document.getElementById('box'); var bg = document.getElementById('bg'); var close = document.getElementById('close'); link.onclick = function() { box.style.display = 'block'; bg.style.display = 'block'; } box.onmousedown = function(e) { var x = e.pageX - box.offsetLeft; var y = e.pageY - box.offsetTop; document.onmousemove = function(e) { var boxX = e.pageX - x; var boxY = e.pageY - y; box.style.left = boxX - 420 + 'px'; box.style.top = boxY - 100 + 'px'; } } document.onmouseup = function() { document.onmousemove = null; } close.onclick = function() { box.style.display = 'none'; bg.style.display = 'none'; } </script> </body> </html>
运行结果:
talk is cheap,show me the code