1.1 🤍offset
offset 用于获取元素自身的位置和大小
实例:
<style>
.farther{
width: 200px;
height: 200px;
border: 1px solid greenyellow;
position: relative;
}
.son{
height: 100px;
width: 100px;
border : 1px solid rebeccapurple;
}
</style>
<div class="farther">
<div class="son"></div>
</div>
<div class="w"></div>
<script>
var farther = document.querySelector(".farther");
var son = document.querySelector(".son");
var w = document.querySelector("w");
console.log(farther.offsetTop);
console.log(farther.offsetLeft);
console.log(farther.offsetWidth);
console.log(son.offsetTop);
console.log(son.offsetParent);
</script>
1.2 offset和style的区别
实例:
<style>
.box{
width: 200px;
height: 200px;
padding: 10px;
background: silver;
}
</style>
<div class="box"></div>
<script>
var box = document.querySelector(".box");
console.log(box.offsetWidth);
console.log(box.style.width);
box.style.width="300px";
1.3 计算鼠标在盒子中的位置
<style>
.box{
width: 400px;
height: 400px;
background: skyblue;
margin: 20px;
}
</style>
实例:
<div class="box"></div>
<script>
var box = document.querySelector(".box");
box.addEventListener("mousemove",function(e){
var allx = e.pageX;
var ally = e.pageY;
var x = allx - this.offsetLeft;
var y = ally - this.offsetTop;
this.innerHTML=" x轴坐标为: "+" "+x+" y轴坐标为: "+y;
})
</script>
1.4 offset案例(拖拽一个东西)
<style>
div{
width: 200px;
height: 200px;
background: royalblue;
position: relative;
}
</style>
<div class="box"></div>
<script>
var img1 = document.querySelector(".box");
img1.addEventListener("mousedown",function(e){
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
function move(e){
img1.style.left=e.pageX-x+"px";
img1.style.top=e.pageY-y+"px";
}
document.addEventListener("mousemove",move);
document.addEventListener("mouseup",function(){
document.removeEventListener("mousemove",move)
})
})
2 client家族
实例:
<style>
.son{
width: 200px;
height: 200px;
padding: 20px;
border: 10px solid royalblue;
}
</style>
<div class="father">
<div class="son"></div>
</div>
<script>
var son = document.querySelector(".son");
console.log(son.clientHeight);
console.log(son.clientWidth);
console.log(son.clientLeft);
console.log(son.clientTop);
</script>
3 🤍scroll家族
<style>
div{
width: 300px;
height: 300px;
overflow: auto;
border: 1px solid grey;
}
</style>
<div>
新浪图片频道:以图为媒,速递新闻;在这里,每天看“有温度的视觉”。
频道致力于成为中国报道摄影师成长平台。以影像记录中国,发掘历史,
感知世界。品牌栏目有《看见》
新浪图片频道:以图为媒,速递新闻;在这里,每天看“有温度的视觉”。
频道致力于成为中国报道摄影师成长平台。以影像记录中国,发掘历史,
感知世界。品牌栏目有《看见》
新浪图片频道:以图为媒,速递新闻;在这里,每天看“有温度的视觉”。
频道致力于成为中国报道摄影师成长平台。以影像记录中国,发掘历史,
感知世界。品牌栏目有《看见》
新浪图片频道:以图为媒,速递新闻;在这里,每天看“有温度的视觉”。
频道致力于成为中国报道摄影师成长平台。以影像记录中国,发掘历史,
感知世界。品牌栏目有《看见》
新浪图片频道:以图为媒,速递新闻;在这里,每天看“有温度的视觉”。
频道致力于成为中国报道摄影师成长平台。以影像记录中国,发掘历史,
感知世界。品牌栏目有《看见》
新浪图片频道:以图为媒,速递新闻;在这里,每天看“有温度的视觉”。
频道致力于成为中国报道摄影师成长平台。以影像记录中国,发掘历史,
感知世界。品牌栏目有《看见》
新浪图片频道:以图为媒,速递新闻;在这里,每天看“有温度的视觉”。
频道致力于成为中国报道摄影师成长平台。以影像记录中国,发掘历史,
感知世界。品牌栏目有《看见》
</div>
<script>
var div = document.querySelector("div");
console.log(div.scrollHeight);
div.addEventListener("scroll",function(){
console.log(div.scrollTop);
})
</script>