js你真的了解offsetWidth吗
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吗的更多相关文章
- JQuery中width和JS中JS中关于clientWidth offsetWidth scrollWidth 等的含义
JQuery中: width()方法用于获得元素宽度: innerWidth()方法用于获得包括内边界(padding)的元素宽度: outerWidth()方法用于获得包括内边界(padding)和 ...
- JS之clientWidth、offsetWidth等属性介绍
一.clientXXX 属性 代码演示 // css 部分 <style> .test{ width:100px; height:100px; border:1px solid red; ...
- JS中关于clientWidth offsetWidth scrollWidth 等的含义
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- JS中关于clientWidth offsetWidth srollWidth等的含义
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- js中的clientWidth offsetWidth scrollWidth等的含义
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- JS中关于clientWidth offsetWidth scrollWidth 的区别及意义
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- JS中关于clientWidth offsetWidth scrollWidth 等的区别
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- JS中关于clientWidth offsetWidth scrollWidth 等的含义的详细介绍
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- offsetWidth相关js属性
js你真的了解offsetWidth吗 offsetWidth是什么? 答:它可以获取物体宽度的数值 那么就只是这样吗! html部分 <div id="div1"> ...
随机推荐
- JavaBean在DAO设计模式简介
一.信息系统开发框架 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层是client,简单的来说就是浏览器. 2.显示层:JSP/Se ...
- JQuery插件datatables相关api
学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...
- Android利用反射获取状态栏(StatusBar)高度
MainActivity如下: package cc.teststatusbarheight; import java.lang.reflect.Field; import android.os.Bu ...
- 【LeetCode】Reorder List 解题报告
Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do th ...
- 使用注解实现 bean 转 csv
csv 文件是 aaa,bbb,ccc aaa,bbb,ccc 保存 这里的要求是 List<T> 线性表的类型 转换成 类别似 html 中 table的格式,即第一行是 head 后面 ...
- gcc的bug? c++模板类中友元函数的訪问权限问题
原文地址:http://stackoverflow.com/q/23171337/3309790 在c++中,模板类中能够直接定义一个友元函数.该函数拥有訪问该模板类非public成员的权限. 比方: ...
- POJ 2524 :Ubiquitous Religions
id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...
- 开启本地MySql数据库远程连接
解决MySQL不允许从远程访问的方法 开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 33 ...
- java.net.SocketException: Connection reset 解决方法
java.net.SocketException: Connection reset 解决方法 最近纠结致死的一个java报错java.net.SocketException: Connection ...
- linux解压多个文件
方法很多种, 根据实际文件类型,位置情况进行变通: 1. for查询:for tar in *.tar.gz; do tar xvf $tar; done2. 列出文件列表,然后xargs 逐一解压: ...