offsetparent javascript 中 offsetWidth 是什么意思
亲爱的读者们,你是否对offsetparent和javascript 中 offsetWidth 是什么意思的关系感到好奇?在本文中,我将深入探讨它们之间的联系,让你对此有更深刻的理解。
js的offset什么意思
offset有offsetParent、offsetTop、offsetLeft、offsetWidth、offsetHeight;
offsetParent:当前容器的父级并且是已进行过CSS定位的容器元素。如果这个容器元素未进行CSS定位,则offsetParent属性的取值为根元素
offsetWidth:元素的可视宽度,包括元素的边框(border),水平padding,元素本身宽度
offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(不包括元素的边框和父容器的边框)
前端基本功:JS(八):offset家族
offset自己的
目的: js中有一套方便的获取元素尺寸的办法就是offset家族;
div{ width:220px; border-left:2px solid red; padding:10px;}
div.offsetWidth= 220+ 2+ 20
为什么不用 div.style.width因为东西只能得到行内的数值
如果父级都没有定位则以body为准
这里的父级指的是所上一级不仅仅指的是父亲还可以是爷爷曾爷爷曾曾爷爷。。。。
offsetParent返回该对象的父级(带有定位)不一定是亲的爸爸
前面学过一个返回父亲(亲的) parentNode有所区别
如果当前元素的父级元素没有进行CSS定位(position为absolute或relative),offsetParent为body。
如果当前元素的父级元素中有CSS定位(position为absolute或relative),offsetParent取最近的那个父级元素。
1.最大区别在于 offsetLeft可以返回没有定位盒子的距离左侧的位置。而 style.top不可以只有定位的盒子才有 left top right
2.offsetTop返回的是数字,而 style.top返回的是字符串,除了数字外还带有单位:px。
3.offsetTop只读,而 style.top可读写。
4.如果没有给 HTML元素指定过 top样式,则 style.top返回的是空字符串。
5.最重要的区别 style.left只能得到行内样式 offsetLeft随便
我们学过一些事件: onmouseover onmouseout onclick.....
event单词翻译过来事件的意思
event就是事件的对象指向的是事件是 onclick
在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含着所有与事件有关的信息。所有浏览器都支持event对象,但支持的方式不同。
比如鼠标操作时候,会添加鼠标位置的相关信息到事件对象中。
普通浏览器支持 event
ie 678支持 window.event
是以我们的电脑屏幕为基准点测量
以我们的文档(绝对定位)的基准点对齐
ie678不认识
以可视区域为基准点类似于固定定位
onmouseover onmouseout onclick
onmousemove当鼠标移动的时候就是说,鼠标移动一像素就会执行的事件
div.onmousemove= function(){语句}
当鼠标再div身上移动的时候,就会执行。
他们相同点都是经过 div才会触发
div.onmouseover只触发一次
div.onmousemove每移动一像素,就会触发一次
onmouseup当鼠标弹起
onmousedown当鼠标按下的时候
1.拖动原理==鼠标按下接着移动鼠标。
bar.onmousedown= function(){
document.onmousemove= function(){
}
}
2.当我们按下鼠标的时候,就要记录当前鼠标的位置-大盒子的位置
算出 bar当前在大盒子内的距离。
我们知道按下鼠标然后拖拽可以选择文字的。
清除选中的内容
offset自己的偏移
offsetWidth得到自己的宽度
offsetHeight
构成: width+ padding+ border
div width 200 border 3px padding-right: 15px
div offsetWidth= 200+ 6+ 15= 221
◆ offsetLeft和 offsetTop
div.offsetLeft
得到距离这个 div最近的带有定位的父盒子左边距离
◆ offsetParent返回自己的父亲元素(带有定位的)
parentNode这个返回亲父亲不管父亲是否带有定位
◆ style.top和 offsetTop
offsetTop只读只可以得到结果但是不能赋值
style.top能得到(行内式)但是可以给值
style.top得到的是 25px
offsetTop得到的是 25
◆事件对象 event
div.onclick= function(event){} event是点击的事件对象
event集合点击事件的相关信息
pageX文档的参考点
clientX可视区域
ScreenX屏幕
◆常用事件
onmouseover经过
onmouseout离开
onmousemove鼠标移动
var num= 0;
div.onmouseover= function(){ num++; console.log(num))} 1
div.onmousemove= function(){ num++; console.log(num))}
onmousedown按下鼠标
onmouseup弹起鼠标
拖拽:先按下鼠标然后移动鼠标
bar.onmousedown= funtion(){
document.onmousemove= function(){}
}
最大 window对象 document对象
今日案例:筋斗云、点击跟随鼠标、放大镜案例素材获取
链接: http://pan.baidu.com/s/1jINmiOI密码:homu
javascript 中 offsetWidth 是什么意思
clientWidth是对象可见的宽度,不包滚动条等边线,会随窗口的显示大小改变。 offsetWidth是对象的可见宽度,包滚动条等边线,会随窗口的显示大小改变。一个scrollWidth和clientWidth的例子:�0�2�0�2�0�2<div id="demo"
style="overflow: hidden; width: 460px; color#ffffff; height: 120px"
<table height="120" cellPadding="0" width="600" align="right" border="0"
cellspacing="0"<tr<td id=demo1 vAlign=top width=543
<table borderColor="#ffffff" cellSpacing="2" cellPadding="0"
width="50" border="1"<tr<tddddd</td</tr</table</td<td id=demo2 vAlign=top width=47</td</tr</table</div<script type="text/javascript"
var speed= 1;//速度数值越大速度越慢
demo2.innerHTML= demo1.innerHTML;
function Marquee(){
if(demo2.offsetWidth- demo.scrollLeft<= 0){
/* scrollLeft设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离。*/
demo.scrollLeft-= demo1.offsetWidth;} else{demo.scrollLeft++;}}var MyMar= setInterval(Marquee, speed);
demo.onmouseover= function(){
clearInterval(MyMar);}demo.onmouseout= function(){
MyMar= setInterval(Marquee, speed);}</script�0�2�0�2�0�2�0�2clientWidth、offsetWidth、clientHeight..区别�0�2IE6.0、FF1.06+:
clientWidth= width+ padding
clientHeight= height+ padding
offsetWidth= width+ padding+ border
offsetHeight= height+ padding+ border
IE5.0/5.5:
clientWidth= width- border
clientHeight= height- border
offsetWidth= width
offsetHeight= height
(需要提一下:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)
offsetwidth:是元素相对父元素的偏移宽度。等于border+padding+width
clientwidth:是元素的可见宽度。等于padding+width
文章到此结束,希望我们对于offsetparent的问题能够给您带来一些启发和解决方案。如果您需要更多信息或者有其他问题,请随时联系我们。