js进阶之js三大家族:offset,scroll,client

offset与style的区别
offset可以得到任意样式表中的样式值
offset系列的数值没有单位
offsetWidth包含padding+border+width
offsetwidth等属性是只读属性,只能获取,不能赋值
所以,我们想要获取元素大小的位置,用offset更合适
style
style只能获取到行内样式表
stylewidth获得的是带有单位的字符串
style.width不包括padding和border的值
style.width是可读属性,可以获取也可以赋值

点语法与offset获取元素的区别
点语法特点:1.可以获取行内标准属性(主要)
2.可以获取语法动态添加的属性
3.无法获取行内自定义属性
4.无法获取行内属性
offset获取特点:1.可以获取行内外属性
2.获取属性值为number类型,而且不带单位
3.获取到的是页面显示的元素的真实宽高(width/height+padding+border)

三大家族区别(三大家族总结)
1.2.1 Width和height
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
scrollWidth = 内容宽度(不包含border)width + padding
scrollHeight = 内容高度(不包含border)
1.2.2 top和left
offsetTop/offsetLeft:
调用者:任意元素。(盒子为主)
作用:距离父系盒子中带有定位的距离。
scrollTop/scrollLeft:(盒子也可以调用,必须有滚动条)
调用者:document.body.scrollTop/.....(window)
作用:浏览器无法显示的部分(被卷去的部分)。
clientY/clientX:(clientTop/clientLeft 值的是border)
调用者:event.clientX(event)
作用:鼠标距离浏览器可视区域的距离(左、上)。
// width和height
// offset带border
// scroll不带border,内容的宽高
// client不带border

// top和left
// offset距离父系盒子带有定位的盒子之间的距离
// scroll被卷去的部分的距离
// clientborder的宽高

//clientX和clientY
// event调用,鼠标距离浏览器的可视区域的距离
client家族特殊用法之:检浏览器宽/高度(可视区域)


Onresize事件
只要浏览器的大小改变,哪怕1像素,都会触动这个事件。
事件总结
区分:
1.window.onscroll 屏幕滑动
2.window.onresize 浏览器大小变化
3.window.onload 页面加载完毕
4.div.onmousemove 鼠标在盒子上移动
(注意:不是盒子移动!!!)
5.onmouseup/onmousedown == onclick获得屏幕宽高
window.screen.width
分辨率是屏幕图像的精密度,指显示器所能显示的像素有多少。
我们的电脑一般:横向1280个像素点,纵向960个像素点。
我们看电影的时候是满屏和半屏的,就是这。

offset家族:获取元素真实的宽高和位置
console.log(box.offsetWidth);
console.log(box.offsetHeight);
scroll家族: scrollWidth,scrollHeight,scrollLeft,scrollTop
作用是:scrollWidth,scrollHeight获取元素内容的宽高(IE8)
scrollLeft,scrollTop获取元素内容真实的位置
console.log(box.scrollWidth);
console.log(box.scrollHeight);
console.log(box.scrollLeft);
console.log(box.scrollTop);
滚动条滚动事件:onscroll

猜你喜欢

转载自www.cnblogs.com/zycs/p/12655987.html
今日推荐