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"> ...
随机推荐
- 开源mp3播放器--madplay 编译和移植 简记
madplay是一款开源的mp3播放器. http://madplay.sourcearchive.com/ 下面简单记录一下madplay的编译与移植到ARM开发板上的过程 一.编译x86版本的ma ...
- switch case语句里面不能定义对象,有语法错误,除非加一个花括号
最近发现一个问题呢 发现在switch的case里面不能去定义对象了,一定义对象就会报错了 仔细了解了一下在C或者C++中,只要是在任何一对花括号 “{ }”中定义的对象,那么该对象的作用域就局限在这 ...
- sd nfrmtl
http://www.zhihu.com/collection/24337307 http://www.zhihu.com/collection/24337259 http://www.zhihu.c ...
- SSDTHook实例--编写稳定的Hook过滤函数
解说怎样写Hook过滤函数,比方NewZwOpenProcess.打开进程. 非常多游戏保护都会对这个函数进行Hook. 因为我们没有游戏保护的代码,无法得知游戏公司是怎样编写这个过滤函数. 我看到非 ...
- 关于Platinum库的MediaRender具体C++代码实现探讨
接上篇博文 NDK下 将Platinum SDK 编译成so库 (android - upnp) 讲述了如何利用该代码库编译给android程序调用的so库,其中也提到了,在使用sample-upnp ...
- CSDN开源夏令营 百度数据可视化实践 ECharts(8)问题分析
ECharts问题描写叙述: 问题就是折线图上的点是显示的,有人问能不能一開始不显示,当你点击的时候或者是当鼠标移动到上面的时候,折线上的点才显示? 例如以下图所看到的: 分析:让折线上的点不显示能够 ...
- 关于http接口开发中json格式数据编码问题处理
关于http接口开发中json格式数据编码问题处理 在实际工作中,接口很多时候返回json格式,但有时返回的格式会有编码问题 假设如下接口:http://service.test.com/interf ...
- Cantor的数表 【找规律】
小俞同学,近期勤学苦练数学,对一种数表产生了兴趣. 数表例如以下: 1/1 1/2 1/3 1/4 1/5 2/1 2/2 2/3 2/4 3/1 3/2 3/3 4/1 4/2 5/1 她冥思苦相了 ...
- Java EE (10) - 资源服务器的整合
加密(Encryption)和数字签名(Digital Signature)通常被用于保护通讯--加密用来防止数据传输过程中的窃听--数字签名用来防止数据传输过程中的篡改 JDBC: 整合关系型数据库 ...
- 基于MVC3下拉列表联动(JQuery)
上次项目中遇到一个需要多个下拉列表联动的操作,今天有空将实现方式整理以便以后参考. 要达到的效果是,点击一个下拉框,则另一个下拉框的值发生对应变化.如:选择中国,则另个一下拉框里显示中国各个省份. 传 ...