offsetWidth是什么?

答:它可以获取物体宽度的数值

那么就只是这样吗!

html部分

<div id="div1"></div>

<style>
#div1 { width:200px; height:200px; border:1px solid red; padding:2px; margin:2px; background:green;}
</style>

请看上面的html,你知道div1的offsetWidth是多少吗?

是不是200啊

哈哈,错了

div1的offsetWidth是206

为什么?

答:offsetWidth实际获取的是盒模型(width+border + padding)

200+2+4=206

扩展:那么offsetLeft和offsetTop呢

答: offsetLeft = left + marginLeft

offsetTop = top +marginTop

示例:让div变窄

现象:onmouseover时,div变窄

原理:

oDiv.style.width = oDiv.offsetWidth - 1 + "px";

js部分

<script>
window.onload = function(){
var oDiv = document.getElementById("div1"); oDiv.onmouseover = function(){
document.title = oDiv.offsetWidth;
setInterval(function(){
oDiv.style.width = oDiv.offsetWidth - 1 + "px";
},30);
} }
</script>

运行上面示例后,你会发现一个奇怪的现象:

div在变宽

我们不是要div变窄的吗!它怎么越来越宽了呢?

那么这个问题,怎么解决呢?

解决方式:

用oDiv.style.width = parseInt(oDiv.style.width) - 1 + "px";

但是发现,onmouseover时,div它不动呢?

原因:oDiv.style.width,它只能获取行间的样式

所以需要调整成

<div id="div1" style="width:200px;"></div>

其实,我们还可以写成一个通用的方法,可以获取任意一个样式

方法:function getStyle(obj,name)

注意:此时样式可以不是行间样式,也能获取

知识点:

IE写法:currentStyle

非IE写法: getComputedStyle

完整代码,如下

<div id="div1"></div>

<style>
#div1 { width:200px; height:200px; border:1px solid red; padding:2px; margin:2px; background:green;}
</style>

  

<script>
window.onload = function(){
var oDiv = document.getElementById("div1"); oDiv.onmouseover = function(){
document.title = oDiv.offsetWidth;
setInterval(function(){
//oDiv.style.width = oDiv.offsetWidth - 1 + "px";
//oDiv.style.width = parseInt(oDiv.style.width) - 1 + "px";
oDiv.style.width = parseInt(getStyle(oDiv,"width"))- + "px";
},);
} } //获取行间任意样式
function getStyle(obj,name){
if(obj.currentStyle){ //IE
return obj.currentStyle[name];
} else {
return getComputedStyle(obj,false)[name]; //非IE
} } </script>

js你真的了解offsetWidth吗的更多相关文章

  1. JQuery中width和JS中JS中关于clientWidth offsetWidth scrollWidth 等的含义

    JQuery中: width()方法用于获得元素宽度: innerWidth()方法用于获得包括内边界(padding)的元素宽度: outerWidth()方法用于获得包括内边界(padding)和 ...

  2. JS之clientWidth、offsetWidth等属性介绍

    一.clientXXX 属性 代码演示 // css 部分 <style> .test{ width:100px; height:100px; border:1px solid red; ...

  3. JS中关于clientWidth offsetWidth scrollWidth 等的含义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  4. JS中关于clientWidth offsetWidth srollWidth等的含义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  5. js中的clientWidth offsetWidth scrollWidth等的含义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  6. JS中关于clientWidth offsetWidth scrollWidth 的区别及意义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  7. JS中关于clientWidth offsetWidth scrollWidth 等的区别

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  8. JS中关于clientWidth offsetWidth scrollWidth 等的含义的详细介绍

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  9. offsetWidth相关js属性

    js你真的了解offsetWidth吗   offsetWidth是什么? 答:它可以获取物体宽度的数值 那么就只是这样吗! html部分 <div id="div1"> ...

随机推荐

  1. 解决Ubuntu下安装VMware错误could not open /dev/vmmon

    在安装VMware并启动新建的虚拟系统时,会出现错误could not open /dev/vmmon. 普通情况下,这是因为ubuntu系统gcc版本号的问题.我机器上是gcc-4.5,于是我将其改 ...

  2. What is the difference between JRE,JVM and JDK?

    If you are a Java developer, it is very often that you think about understanding the JRE,JVM and JDK ...

  3. Python 分析Twitter用户喜爱的推文

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-5 @author: guaguastd @name: an ...

  4. csdn肿么了,这两天写的博文都是待审核

    昨天早上8点写了一篇博文,然后点击发表,结果系统显示"待审核".于是仅仅好qq联系csdn的客服,等到9点时候,csdn的客服上线了,然后回复说是链接达到5个以上须要审核,于是回到 ...

  5. iOS8:把这些七招APP哭

    6月3日.苹果发布了新一代的高配置手机操作系统iOS 8,我们看到了很多新的功能和引人注目的新变化.它为开发人员提供了许多其他更酷能力发展.第三方输入法也开放,这使得国内的百度.搜狗输入法是不过高兴的 ...

  6. [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)

    先来说一下我们学校的网站: http://jwxt.sdu.edu.cn:7777/zhxt_bks/zhxt_bks.html 查询成绩需要登录,然后显示各学科成绩,但是只显示成绩而没有绩点,也就是 ...

  7. bash学习之环境变量

    1.查看系统存在的环境变量env 和 export env命令:查看环境变量 [CJP@CJP ~]$ env HOSTNAME=CJP SHELL=/bin/bash HISTSIZE=1000 U ...

  8. A Game of Thrones(9) - Tyrion

    Somewhere in the great stone maze(迷宫:迷惑) of Winterfell, a wolf howled. The sound hung over the castl ...

  9. 【IOS实例小计】今日开贴,记录我的ios学习生涯,留下点滴,留下快乐,成荫后人。

    今天开贴来记录自己的ios学习过程,本人目前小白一个,由于对ios感兴趣,所以开始学习,原职java程序,呵呵,勿喷. 本次的[ios实例小计]主要参考一文http://blog.sina.com.c ...

  10. XSS跨站攻击

    目录 1 XSS跨站攻击简介 1 1.1 什么是XSS 1 1.2 XSS的分类 1 1.3 XSS的危害 1 2 XSS的攻击原理 1 2.1 本地式漏洞攻击 1 2.2 存储式漏洞攻击 2 2.3 ...