目录
一、元素偏移量offset系列
1. offset概述
offset翻译过来就是偏移量,我们使用offset系列相关属性可以动态的得到该元素的位置(偏移)、大小等。
- 获取元素距离带有定位父元素的位置
- 获得元素自身大小
- 注意:返回的值都不带单位
2. 常见属性
<!-- offset位置 -->
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.father {
position: relative;
width: 200px;
height: 200px;
background-color: pink;
margin: 150px;
}
.son {
width: 50px;
height: 50px;
background-color: blue;
margin: 50px;
padding: 10px;
border: 2px solid red;
}
</style>
<body>
<div class="father">
<div class="son">
</div>
</div>
<script type="text/javascript">
var father = document.querySelector('.father');
var son = document.querySelector('.son');
console.log(father.offsetTop);
console.log(father.offsetLeft);
//1.它是以有定位的父亲为准,若无父亲或无定位的父亲 以body为准
console.log(son.offsetLeft);
//2.可以得到元素的大小,宽高 包含padding+border+width
console.log(son.offsetHeight);
console.log(son.offsetWidth);
//3. 返回带有定位的父亲,若无返回body
console.log(son.offsetParent);
console.log(son.parentNode); //返回父亲 是最近一级的父亲 不管有没有定位
</script>
</body>
3. offset与style区别
案例——鼠标在盒子内坐标
<!-- 鼠标在盒子内坐标 -->
<style type="text/css">
.box {
width: 200px;
height: 200px;
background-color: pink;
margin: 100px 100px;
}
</style>
<body>
<div class="box"></div>
<script type="text/javascript">
var box = document.querySelector('.box');
box.addEventListener('mousemove', function(e) {
// console.log(e.pageX);
// console.log(e.pageY);
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
this.innerHTML = 'x坐标是' + x + 'y坐标是' + y;
})
</script>
</body>
案例——拖动模态框
<!-- 拖动模态框 -->
<style type="text/css">
.login-header {
text-align: center;
}
a {
margin: 0;
padding: 0;
}
.login {
display: none;
width: 512px;
height: 280px;
position: fixed;
border: 1px solid #EBEBEB;
left: 50%;
top: 50%;
background-color: #FFFFFF;
box-shadow: 0 0 20px #DDDDDD;
z-index: 9999;
transform: translate(-50%, -50%);
}
.login-title {
width: 100%;
margin: 10px 0 0 0;
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 0 auto;
line-height: 40px;
font-size: 14px;
border: 1px solid #EBEBEB;
text-align: center;
}
.login-bg {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background-color: 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: 1px solid #EBEBEB;
text-indent: 5px;
}
.login-input {
overflow: hidden;
margin: 0 0 20px 0;
}
.login-input label {
float: left;
width: 90px;
padding-right: 10px;
text-align: center;
line-height: 35px;
height: 35px;
font-size: 14px;
}
.login-title span {
position: absolute;
font-size: 12px;
right: -20px;
top: -30px;
background-color: #FFFFFF;
border: #EBEBEB solid 1px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
<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" name="info[username]" id="username" placeholder="请输入用户名" 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>
//1.获取事件
var login = document.querySelector('.login');
var mask = document.querySelector('.login-bg');
var link = document.querySelector('#link');
var closeBtn = document.querySelector('#closeBtn');
var title = document.querySelector('#title');
//2.点击弹出,显示登录
link.addEventListener('click', function() {
mask.style.display = 'block';
login.style.display = 'block';
})
//3.点击关闭
closeBtn.addEventListener('click', function() {
mask.style.display = 'none';
login.style.display = 'none';
})
//4.开始拖拽
//(1)当鼠标按下,获得鼠标在盒子内坐标
title.addEventListener('mousedown', function(e) {
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
//(2)鼠标移动式,把鼠标在页面中的坐标,减去鼠标在盒子内的坐标(不变的)
function move(e) {
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
document.addEventListener('mousemove', move);
//(3)鼠标弹起,就让鼠标移动事件移除
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move);
})
})
</script>
案例——京东放大镜
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.preview_img {
position: relative;
border: 1px solid #CCCCCC;
height: 400px;
width: 400px;
}
.preview img {}
.mask {
display: none;
position: absolute;
top: 0;
left: 0;
height: 300px;
width: 300px;
background-color: #fede4f;
border: 1px solid #CCCCCC;
opacity: .5;
cursor: move;
}
.big {
display: none;
position: absolute;
top: 0;
left: 410px;
width: 500px;
height: 500px;
border: 1px solid #CCCCCC;
overflow: hidden;
}
.big img {
position: absolute;
top: 0;
left: 0;
}
</style>
<body>
<div class="preview_img">
<img src="img/load.png" />
<div class="mask">1</div>
<div class="big">
<img src="img/load-big.jpg" class="bigImg">
</div>
</div>
</body>
<script type="text/javascript">
var preview_img = document.querySelector('.preview_img');
var mask = document.querySelector('.mask');
var big = document.querySelector('.big');
//1.当鼠标经过preview_img 就显示隐藏mask遮挡层,和big大盒子
preview_img.addEventListener('mouseover', function() {
mask.style.display = 'block';
big.style.display = 'block';
})
preview_img.addEventListener('mouseout', function() {
mask.style.display = 'none';
big.style.display = 'none';
})
//2.鼠标移动让黄色盒子跟着鼠标走
preview_img.addEventListener('mousemove', function(e) {
//(1)先计算出鼠标在盒子内的坐标
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
//(2)减去盒子高度的一般,保证鼠标在中间
var maskX = x - mask.offsetWidth / 2;
var maskY = y - mask.offsetHeight / 2;
//(3)黄色盒子不能出小盒子范围。
//(3).1 如果x坐标小于0就让停在0的位置,大于小盒子宽度减去遮挡层宽度
//遮挡层最大移动距离(正方形,长宽一样)
var maskMax = preview_img.offsetWidth - mask.