首页编程offsetleft JS中的offsetLeft属性具体有什么用

offsetleft JS中的offsetLeft属性具体有什么用

编程之家 2023-11-02 181次浏览

大家好,感谢邀请,今天来为大家分享一下offsetleft的问题,以及和JS中的offsetLeft属性具体有什么用的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!

offsetleft JS中的offsetLeft属性具体有什么用

style.left 与offsetLeft之间的区别

offsetLeft获取的是相对于父对象的左边距

left获取或设置相对于具有定位属性(position定义为relative)的父对象的左边距

如果父div的position定义为relative,子div的position定义为absolute,那么子div的style.left的值是相对于父div的值,

这同offsetLeft是相同的,区别在于:

1. style.left返回的是字符串,如28px,offsetLeft返回的是数值28,如果需要对取得的值进行计算,

还用offsetLeft比较方便。

offsetleft JS中的offsetLeft属性具体有什么用

2. style.left是读写的,offsetLeft是只读的,所以要改变div的位置,只能修改style.left。

3. style.left的值需要事先定义,否则取到的值为空。而且必须要定义在html里,我做过试验,如果定义在

css里,style.left的值仍然为空,这就是我刚开始碰到的问题,总是取不到style.left的值。

offsetLeft则仍然能够取到,无需事先定义div的位置。

为什么我写的无缝滚动offsetleft一直是0

<!DOCTYPE>

<html>

offsetleft JS中的offsetLeft属性具体有什么用

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>

<title>向上下左右不间断无缝滚动效果(兼容火狐和IE)</title>

</head>

<body>

<divid="colee"style="overflow:hidden;height:253px;width:410px;">

<divid="colee1">

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

<p>此处放文字。。。。。。。。。</p>

</div>

<divid="colee2"></div>

</div>

<script>

varspeed=30;

varcolee2=document.getElementById("colee2");

varcolee1=document.getElementById("colee1");

varcolee=document.getElementById("colee");

colee2.innerHTML=colee1.innerHTML;//克隆colee1为colee2

functionMarquee1(){

//当滚动至colee1与colee2交界时

if(colee2.offsetTop-colee.scrollTop<=0){

colee.scrollTop-=colee1.offsetHeight;//colee跳到最顶端

}else{

colee.scrollTop++

}

}

varMyMar1=setInterval(Marquee1,speed)//设置定时器

//鼠标移上时清除定时器达到滚动停止的目的

colee.onmouseover=function(){clearInterval(MyMar1)}

//鼠标移开时重设定时器

colee.onmouseout=function(){MyMar1=setInterval(Marquee1,speed)}

</script>

<!--向上滚动代码结束-->

<br>

<!--下面是向下滚动代码-->

<divid="colee_bottom"style="overflow:hidden;height:253px;width:410px;">

<divid="colee_bottom1">

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

<p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p>

</div>

<divid="colee_bottom2"></div>

</div>

<script>

varspeed=30

varcolee_bottom2=document.getElementById("colee_bottom2");

varcolee_bottom1=document.getElementById("colee_bottom1");

varcolee_bottom=document.getElementById("colee_bottom");

colee_bottom2.innerHTML=colee_bottom1.innerHTML

colee_bottom.scrollTop=colee_bottom.scrollHeight

functionMarquee2(){

if(colee_bottom1.offsetTop-colee_bottom.scrollTop>=0)

colee_bottom.scrollTop+=colee_bottom2.offsetHeight

else{

colee_bottom.scrollTop--

}

}

varMyMar2=setInterval(Marquee2,speed)

colee_bottom.onmouseover=function(){clearInterval(MyMar2)}

colee_bottom.onmouseout=function(){MyMar2=setInterval(Marquee2,speed)}

</script>

<!--向下滚动代码结束-->

<br>

<!--下面是向左滚动代码-->

<divid="colee_left"style="overflow:hidden;width:500px;">

<tablecellpadding="0"cellspacing="0"border="0">

<tr><tdid="colee_left1"valign="top"align="center">

<tablecellpadding="2"cellspacing="0"border="0">

<tralign="center">

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

</tr>

</table>

</td>

<tdid="colee_left2"valign="top"></td>

</tr>

</table>

</div>

<script>

//使用div时,请保证colee_left2与colee_left1是在同一行上.

varspeed=30//速度数值越大速度越慢

varcolee_left2=document.getElementById("colee_left2");

varcolee_left1=document.getElementById("colee_left1");

varcolee_left=document.getElementById("colee_left");

colee_left2.innerHTML=colee_left1.innerHTML

functionMarquee3(){

if(colee_left2.offsetWidth-colee_left.scrollLeft<=0)//offsetWidth是对象的可见宽度

colee_left.scrollLeft-=colee_left1.offsetWidth//scrollWidth是对象的实际内容的宽,不包边线宽度

else{

colee_left.scrollLeft++

}

}

varMyMar3=setInterval(Marquee3,speed)

colee_left.onmouseover=function(){clearInterval(MyMar3)}

colee_left.onmouseout=function(){MyMar3=setInterval(Marquee3,speed)}

</script>

<!--向左滚动代码结束-->

<br>

<!--下面是向右滚动代码-->

<divid="colee_right"style="overflow:hidden;width:500px;">

<tablecellpadding="0"cellspacing="0"border="0">

<tr><tdid="colee_right1"valign="top"align="center">

<tablecellpadding="2"cellspacing="0"border="0">

<tralign="center">

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

<td><p><imgsrc="/jscss/demoimg/200907/bg3.jpg"></p></td>

</tr>

</table>

</td>

<tdid="colee_right2"valign="top"></td>

</tr>

</table>

</div>

<script>

varspeed=30//速度数值越大速度越慢

varcolee_right2=document.getElementById("colee_right2");

varcolee_right1=document.getElementById("colee_right1");

varcolee_right=document.getElementById("colee_right");

colee_right2.innerHTML=colee_right1.innerHTML

functionMarquee4(){

if(colee_right.scrollLeft<=0)

colee_right.scrollLeft+=colee_right2.offsetWidth

else{

colee_right.scrollLeft--

}

}

varMyMar4=setInterval(Marquee4,speed)

colee_right.onmouseover=function(){clearInterval(MyMar4)}

colee_right.onmouseout=function(){MyMar4=setInterval(Marquee4,speed)}

</script>

<!--向右滚动代码结束-->

</body>

</html>

offsetLeft和style.left的区别

offsetLeft获取的是相对于父对象的左边距

left获取或设置相对于具有定位属性(position定义为relative)的父对象的左边距

如果父div的position定义为relative,子div的position定义为absolute,那么子div的style.left的值是相对于父div的值,

这同offsetLeft是相同的,区别在于:

1. style.left返回的是字符串,如28px,offsetLeft返回的是数值28,如果需要对取得的值进行计算,

还用offsetLeft比较方便。

2. style.left是读写的,offsetLeft是只读的,所以要改变div的位置,只能修改style.left。

3. style.left的值需要事先定义,否则取到的值为空。而且必须要定义在html里,我做过试验,如果定义在

css里,style.left的值仍然为空,这就是我刚开始碰到的问题,总是取不到style.left的值。

offsetLeft则仍然能够取到,无需事先定义div的位置。

JS中的offsetLeft属性具体有什么用

可以判断一个物体的跟document的左边距离,也就是浏览器左边缘。写一个div获取这个div之后alert(你的div.offsetLeft)就可以距离浏览器左边的距离。

offset不单单只有Left还有offsetTop offsetWidth offsetHeight都是JS里很有用的属性。(PS:此属性当父元素没有position时有效,有父元素时将是判断距离父元素的距离)。

扩展资料:

JavaScript是一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。

它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。

Javascript被归类为直译语言,因为主流的引擎都是每次运行时加载代码并解译。V8是将所有代码解译后再开始运行,其他引擎则是逐行解译(SpiderMonkey解译过的指令暂存,以提高性能,称为实时编译)。

但由于V8的核心部分多数用Javascript撰写(而SpiderMonkey是用C++),因此在不同的测试上,两者性能互有优劣。与其相对应的是编译语言,例如C语言,以编译语言编写的程序在运行之前,必须经过编译,将代码编译为机器码,再加以运行。

参考资料来源:百度百科-javascript

非常感谢您的阅读!我们希望本文对于解决您关于offsetleft的问题提供了一些有价值的信息。如果您还有其他疑问,我们将很乐意为您提供进一步的帮助。

百度推广方案 百度知道推广方案谁有的 外网内网 什么叫做内网、外网、公网