1、点击弹出层,回弹出模态框,并且显示半透明的遮挡层
2、点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层
3、鼠标放到模态框最上面一行,可以按住鼠标拖拽模态框在页面中移动
4、鼠标松开,可以停止拖动模态框移动
鼠标放到最上面一行的时候,变成十字形状 load.style.cursor = 'move';
主要是拖动效果的实现
在页面中拖拽的过程:鼠标按下(mousedown)并且移动(mosemove)(怎么实现两个事件必须同时发生--鼠标移动事件写在鼠标按下事件的里面),之后松开鼠标(moseup) ?????
拖拽过程:鼠标在移动过程中,获得最新的值赋值给模态框的left和top值(自己未用到),这样模态就可以跟着鼠标走了
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>h1 {margin: 0 auto;text-align: center;}.box {display: none;background-color: white;width: 500px;height: 200px;position: relative;margin: auto}.box p {font-size: 20px;text-align: center;padding-top: 10px}table {text-align: right;}input {width: 360px;height: 30px}button {width: 200px;height: 40px;background-color: white;border: 1px solid rgb(204, 195, 195);margin-left: 140px;margin-top: 10px}.yuan {border-radius: 40px 40px;width: 40px;height: 40px;text-align: center;line-height: 40px;position: absolute;top: -20px;left: 480px;background-color: white;}</style>
</head><body><h1>点击,弹出登录框</h1><div class="box"><p class="load">登录会员</p><form action=""><table><tr><td>用户名:</td><td><input type="text" placeholder="请输入用户名"></td></tr><tr><td>登录密码:</td><td> <input type="password" placeholder="请输入登录密码"></td></tr></table></form><button>登录会员</button><!-- 按钮写在表单域里面的话,相当于提交表单,按下按钮,box盒子也会消失?? --><div class="yuan">关闭</div></div><script>//点击登录框,整个body颜色变灰var h1 = document.querySelector('h1')var body = document.bodyvar box = document.querySelector('.box')var off = document.querySelector('.yuan')h1.addEventListener('click', function() {body.style.backgroundColor = 'grey'// body.style.backgroun = ' rbga(0, 0, 0, 0.3)'//点击登录框,弹出模态框box.style.display = 'block'})//点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层off.addEventListener('click', function() {box.style.display = 'none';body.style.backgroundColor = ''})//当鼠标当道模态框上面一行是,可以按住鼠标拖拽模态框在页面中移动 margin-left: 200px; margin-top: 200px 修改模态框相对于页面的位置var load = document.querySelector('.load')load.addEventListener('mouseover', function() {load.style.cursor = 'move';load.addEventListener('mousedown', function() {//怎么让盒子跟随鼠标移动load.addEventListener('mousemove', function(e) {/* 后面JS设计的时候就只能实现x轴的移动 */box.style.marginLeft = e.pageX - 250 + 'px'box.style.marginTop = e.pageY - 100 + 'px'})})})</script>
</body></html>
拖拽那边自己无法实现:主要是box盒子的css设置就出了问题;明天再试试
margin:改变的是盒子与盒子之间的距离
鼠标按下:首先获得其相对于盒子的坐标
然后鼠标移动:由于要保持鼠标相对于盒子的坐标不变(可以根据这个原理,列写等式,然后把关系弄清楚),那么盒子就需要移动
<div class="box"><h1>登录会员</h1></div><script>var h1 = document.querySelector('h1')var div = document.querySelector('.box')//当鼠标放在h1上时,变成十字// h1.addEventListener('mouseover', function() {// this.style.cursor = 'move'// // h1.addEventListener('mousedown', function() {// })h1.addEventListener('mouseover', function() {this.style.cursor = 'move'})h1.addEventListener('mousedown', function(e1) {var x1 = e1.pageX - div.offsetLeft //多画画 获取元素的位置多用offset,更改元素的位置多用stylevar y1 = e1.pageY - div.offsetToph1.addEventListener('mousemove', fn)h1.addEventListener('mouseup', function() {h1.removeEventListener('mousemove', fn) //移除函数的话,就需要函数名称,因此前面就不能使用匿名函数 注意写法 移除的应该是整个mousedown事件})function fn(e2) {div.style.left = e2.pageX - x1 + 'px'div.style.top = e2.pageY - y1 + 'px'}})// })</script><style>.box {width: 200px;height: 100px;background-color: pink;border: 1px solid red;position: absolute;}</style>
根据原理,对最开始的程序做出修改:
<script>//点击登录框,整个body颜色变灰var h1 = document.querySelector('h1')var body = document.bodyvar box = document.querySelector('.box')var off = document.querySelector('.yuan')h1.addEventListener('click', function() {body.style.backgroundColor = 'grey'// body.style.backgroun = ' rbga(0, 0, 0, 0.3)'//点击登录框,弹出模态框box.style.display = 'block'})//点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层off.addEventListener('click', function() {box.style.display = 'none';body.style.backgroundColor = ''})//当鼠标当道模态框上面一行是,可以按住鼠标拖拽模态框在页面中移动 margin-left: 200px; margin-top: 200px 修改模态框相对于页面的位置var load = document.querySelector('.load')load.addEventListener('mouseover', function() {load.style.cursor = 'move';})load.addEventListener('mousedown', function(e1) {var x1 = e1.pageX - box.offsetLeft //多画画 获取元素的位置多用offset,更改元素的位置多用stylevar y1 = e1.pageY - box.offsetTop//怎么让盒子跟随鼠标移动load.addEventListener('mousemove', fn)load.addEventListener('mouseup', function() {load.removeEventListener('mousemove', fn) //移除函数的话,就需要函数名称,因此前面就不能使用匿名函数 注意写法 移除的应该是整个mousedown事件})function fn(e) {/* 后面JS设计的时候就只能实现x轴的移动 */box.style.marginLeft = e.pageX - x1 + 'px'box.style.marginTop = e.pageY - y1 - 43 + 'px' //减43是因为上面的h1高度为41.6px左右}})</script>
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {display: none;width: 400px;height: 240px;background-color: white;position: absolute;top: 50px;left: 30px}.mention {text-align: center;width: 400px;height: 50px;line-height: 50px;position: relative;}.mention p {font-size: 20px;margin: 0px}.off {width: 40px;height: 40px;border-radius: 40px 40px;position: absolute;top: -20px;right: -20px;font-size: 14px;border: none;background-color: white;}.name,.password {width: 300px;height: 50px;}.name span,.password span {width: 100px;text-align: right;display: inline-block;}input {height: 30px;width: 250px}.sub {background-color: white;border: 1px solid grey;margin-left: 130px;width: 220px;height: 40px;font-size: 17px;margin-top: 20px;border: 1px solid rgb(151, 147, 147)}h1 {text-align: center;}</style>
</head><body><div class="box1"><h1>点击,弹出登录框</h1><div class="box"><div class="mention"><p class="wenz"> 登录会员</p><button class="off">关闭 </button></div><form action=""><div class="name"><span>用户名:</span> <span><input type="text" placeholder="请输入用户名"></span></div><div class="password"><span> 登录密码:</span><span> <input type="text" placeholder="请输入登录密码"></span></div><input type="submit" name="" id="" value="登录会员" class="sub"></form></div></div><script>var h1 = document.querySelector('h1')//点击h1背景色变灰,登录框弹出var body = document.bodyvar box = document.querySelector('.box')h1.addEventListener('click', function() {body.style.backgroundColor = 'grey'box.style.display = 'block'})//点击关闭按钮 背景恢复,关闭登录框var off = document.querySelector('.off')off.addEventListener('click', function() {body.style.backgroundColor = ''box.style.display = 'none'})//鼠标放在登录会员上的时候,箭头变成移动形状var wenz = document.querySelector('.wenz')wenz.addEventListener('mouseover', function() {this.style.cursor = 'move'})//鼠标点击,获得点击时候相对于box个盒子的坐标wenz.addEventListener('mousedown', function(e) {var x = e.pageX - box.offsetLeftvar y = e.pageY - box.offsetTop//鼠标点击之后,再鼠标移动,来更改box盒子相对于页面的位置,保证移动后的鼠标相对于盒子的坐标是不变的wenz.addEventListener('mousemove', fn)function fn(e2) {box.style.top = e2.pageY - y + 'px'box.style.left = e2.pageX - x + 'px'}//鼠标松开后,删除移动事件wenz.addEventListener('mouseup', function() {wenz.removeEventListener('mousemove', fn)})})</script>
</body></html>
终于完成,在CSS样式哪里还要多练习