1.offsetLeft和offsetTop获取元素偏移
<!--offset翻译过来就是偏移量,我们使用offset系列相关属性可以动态的得到该元素的位置(偏移量)、大小等获得元素距离带有定位父元素的位置获得元素自身的大小(宽度和高度)注意:返回的数值都不带单位offset系列常用属性:offset系列属性 作用element.offsetParent 返回作为该元素带有定位的父级元素,如果父级元素都没有定位则返回bodyelement.offsetTop 返回元素相对带有定位元素上方的偏移,如果没有父级或父级元素都没有定位则返回bodyelement.offsetLeft 返回元素相对带有定位元素左边框的偏移,如果没有父级或父级元素都没有定位则返回bodyelement.offsetWidth 返回自身包括padding、边框、内容区的宽度,返回数值不带单位element.offsetHeight 返回自身包括padding、边框、内容区的高度,返回数值不带单位--><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>offsetLeft和offsetTop获取元素偏移</title><style>* {margin: 0;padding: 0;}.father {/* position: relative; */width: 200px;height: 200px;background-color: pink;margin: 150px;}.son {width: 100px;height: 100px;background-color: purple;margin-left: 45px;}.w {height: 200px;background-color: skyblue;margin: 0 auto 200px;padding: 10px;border: 15px solid red;}</style></head><body><div class="father"><div class="son"></div></div><div class="w"></div><script>// offset 系列var father = document.querySelector('.father');var son = document.querySelector('.son');// 1.可以得到元素的偏移 位置 返回的不带单位的数值console.log(father.offsetTop);console.log(father.offsetLeft);// 它以带有定位的父亲为准 如果么有父亲或者父亲没有定位 则以 body 为准console.log(son.offsetLeft);var w = document.querySelector('.w');// 2.可以得到元素的大小 宽度和高度 是包含padding + border + widthconsole.log(w.offsetWidth);console.log(w.offsetHeight);// 3. 返回带有定位的父亲 否则返回的是bodyconsole.log(son.offsetParent); // 返回带有定位的父亲 否则返回的是bodyconsole.log(son.parentNode); // 返回父亲 是最近一级的父亲 亲爸爸 不管父亲有没有定位</script></body></html>
2.offset与style区别
<!--offset可以得到任意样式表中的样式值offset系列获得的数值是没有单位的offsetWidth包含padding+border+widthoffset等属性是只读属性,只能获取不能赋值style只能得到行内样式表中的样式值style.width获得的是带有单位的字符串style.width获得不包含padding和border的值style.width是可读属性,可以获得也可以赋值所以,我们想要获取元素大小位置,用offset更合适想要给元素更改值,则需要用style改变--><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>offset与style区别</title><style>.box {width: 200px;height: 200px;background-color: pink;padding: 10px;}</style></head><body><div class="box" style="width: 200px;"></div><script>// offset与style的区别var box = document.querySelector('.box');console.log(box.offsetWidth);console.log(box.style.width);// box.offsetWidth = '300px';box.style.width = '300px';</script></body></html>
3.获取鼠标在盒子内的坐标
<!--获取鼠标在盒子里的坐标:用鼠标在页面下的坐标e.pageX和e.pageY减去盒子的坐标this.offsetLeft和this.offsetTop--><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>获取鼠标在盒子内的坐标</title><style>div{width: 300px;height: 300px;background-color: violet;margin:200px;}</style></head><body><div></div><script>var div=document.querySelector('div');div.addEventListener('mousemove',function (e) {console.log(e.pageX);console.log(e.pageY);console.log(div.offsetLeft);console.log(div.offsetTop);var x=e.pageX-this.offsetLeft;var y=e.pageY-this.offsetTop;this.innerHTML='x坐标是'+x+',y坐标是'+y;})</script></body></html>
4.拖动模态框
<!--1.点击弹出层,静态框和遮挡层就会显示出来display:block;2.点击关闭按钮,模态框和遮挡层就会隐藏起来display:none;在页面的拖拽原理:鼠标按下并移动,之后松开鼠标触发事件是鼠标按下mousedown,鼠标移动mousemove鼠标松开mouseup拖拽过程:鼠标移动过程中,获得最新的值赋值给模拟框的left和top值,这样模拟框就可以跟着鼠标走了鼠标按下触发的事件源是 最上面一行,就是id为title鼠标移动,把鼠标在页面中的坐标,减去 鼠标在盒子内的坐标,就是模态框的left和top值鼠标弹起,就让鼠标移动事件移除--><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>拖动模态框</title><style>.login-header {width: 100%;text-align: center;height: 30px;font-size: 24px;line-height: 30px;}ul,li,ol,dl,dt,dd,div,p,span,h1,h2,h3,h4,h5,h6,a {padding: 0px;margin: 0px;}.login {display: none;width: 512px;height: 280px;position: fixed;border: #ebebeb solid 1px;left: 50%;top: 50%;background: #ffffff;box-shadow: 0px 0px 20px #ddd;z-index: 9999;transform: translate(-50%, -50%);}.login-title {width: 100%;margin: 10px 0px 0px 0px;text-align: center;line-height: 40px;height: 40px;font-size: 18px;position: relative;cursor: move;}.login-input-content {margin-top: 20px;}.login-button {width: 50%;margin: 30px auto 0px auto;line-height: 40px;font-size: 14px;border: #ebebeb 1px solid;text-align: center;}.login-bg {display: none;width: 100%;height: 100%;position: fixed;top: 0px;left: 0px;background: rgba(0, 0, 0, .3);}a {text-decoration: none;color: #000000;}.login-button a {display: block;}.login-input input.list-input {float: left;line-height: 35px;height: 35px;width: 350px;border: #ebebeb 1px solid;text-indent: 5px;}.login-input {overflow: hidden;margin: 0px 0px 20px 0px;}.login-input label {float: left;width: 90px;padding-right: 10px;text-align: right;line-height: 35px;height: 35px;font-size: 14px;}.login-title span {position: absolute;font-size: 12px;right: -20px;top: -30px;background: #ffffff;border: #ebebeb solid 1px;width: 40px;height: 40px;border-radius: 20px;}</style></head><body><div class="login-header"><a id="link" href="javascript:;">点击,弹出登录框</a></div><div id="login" class="login"><div id="title" class="login-title">登录会员<span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span></div><div class="login-input-content"><div class="login-input"><label>用户名:</label><input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input"></div><div class="login-input"><label>登录密码:</label><input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input"></div></div><div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div></div><!-- 遮盖层 --><div id="bg" class="login-bg"></div><script>var login=document.querySelector('.login');var mask=document.querySelector('.login-bg');var link=document.querySelector('#link');var closeBtn=document.querySelector('#closeBtn');//点击弹出层这个链接link 让mask和login显示出来link.addEventListener('click',function () {login.style.display='block';mask.style.display='block';})//点击closeBtn就隐藏mask和logincloseBtn.addEventListener('click',function () {mask.style.display='none';login.style.display='none';})//拖拽页面var title=document.querySelector('#title');title.addEventListener('mousedown',function (e) {var x=e.pageX-login.offsetLeft;var y=e.pageY-login.offsetTop;//鼠标移动,把鼠标在页面中的坐标,减去 鼠标在盒子内的坐标,就是模态框的left和top值document.addEventListener('mousemove',move);function move(e) {login.style.left=e.pageX-x+'px';login.style.top=e.pageY-y+'px';}//鼠标弹起,就让鼠标移动事件移除document.addEventListener('mouseup',function (e) {document.removeEventListener('mousemove',move);})})</script></body></html>
5.client系列
动态获取该元素的边框大小、元素大小等
element.clientTop 返回元素上边框的大小
element.clientLeft 返回元素左边框的大小
element.clientWidth 返回自身包括padding、内容区的宽度,不含边框,返回数值不带单位
element.clientHeight 返回自身包含padding、内容区的高度,不含边框,返回数值不带单位
offset系列获得的数值是没有单位的
offsetWidth包含padding+border+width
6.立即执行函数
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>立即执行函数</title></head><body><script>//立即执行函数:不需要调用,立马能够自己执行的函数function fn(){console.log(1);}fn();//写法// (function() {}) () 或者(function(){}());(function (a) {console.log(a);})(2);//第二个小括号可以看做是调用函数,可以传参数(function (a,b) {console.log(a+b);} (2,3))//立即执行函数最大的作用就是 独立创建了一个作用域,里面的所有变量都是局部变量,不会影响运行</script></body></html>
2021.11.07 16:15
