getcomputedstyle()获取border像素差异问题
getComputedStyle()方法返回的是一个CSS样式声明对象--CSSStyleDeclaration对象(与style属性的类型相同),包含当前元素所有最终使用的CSS属性值;
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#myDiv {
background-color: #FFF;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div id="myDiv" style="background-color: red; border: 1px solid #333;"></div>
</body>
<script>
var myDiv = document.getElementById('myDiv');
var computedStyle = document.defaultView.getComputedStyle(myDiv, null);
console.log(computedStyle.backgroundColor);//rgb(255, 0, 0)
console.log(computedStyle.width);//200px
console.log(computedStyle.height);//200px
console.log(parseFloat(computedStyle.border));//
console.log(computedStyle.border);//1px solid rgb(51, 51, 51)(chrome下)
//NaN(Firefox下)
//1px solid rgb(51, 51, 51)(Opera下)
</script>
</html>
IE不支持getComputedStyle()方法,取而代之的是IE浏览器的一个属性currentStyle;
var myDiv = document.getElementById('myDiv');
var computedStyle = myDiv.currentStyle;
console.log(computedStyle.backgroundColor);
console.log(computedStyle.width);
console.log(computedStyle.height);
console.log(computedStyle.border);//undefined
造成border差异原因:
border: 1px solid #333;是一个复合属性,浏览器在解析时把border解释为:borderleftwidth: 200px; borderleftcolor: #333; borderleftstyle: solid; ...一个div有四条边,每条边的样式(如宽度,颜色等)都可能不同,所以在不同的浏览器下输出结果会不一致;
延伸:与style.css区别
getComputedStyle()获取完整样式,而style.css仅能获得内联样式中的部分属性,无法获得层叠或继承来的外部属性;
使用注意点:
1、仅能设置内联样式中的css属性, css属性名(class-name)都要去横线变驼峰命名(className);
2、无论是获取/设置的css属性值必须都是字符串;获取时,要去单位,再计算;修改时,要补单位,再赋值;
声明:本博客的文章除特殊说明外均为原创,转载请注明出处;
getcomputedstyle()获取border像素差异问题的更多相关文章
- Android开发系列(一)Activity与Fragment获取屏幕获取屏幕像素的不同方式
Activity中常用的获取屏幕像素代码: //获取屏幕像素相关信息 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getD ...
- currentStyle、getComputedStyle 获取样式
style.height 获取的是行间的样式 currentStyle.height.getComputedStyle(elem,null).height 获取的是 div 的 content 的宽高 ...
- getComputedStyle获取css属性与IE下的currentStyle获取到的值不同
<!doctype html><html lang="en"> <head> <meta charset="UTF-8&quo ...
- swift获取图片像素颜色值
extension UIImage{ /** 获取图片中的像素颜色值 - parameter pos: 图片中的位置 - returns: 颜色值 */ func getPixelColor(pos: ...
- js用currentStyle和getComputedStyle获取css样式(非行间) 兼容ie与火狐
用js的style属性可以获得html标签的样式,但是不能获取非行间样式.那么怎么用js获取css的非行间样式呢?在IE下可以用currentStyle,而在火狐下面我们需要用到getComputed ...
- 火狐下不能使用非行间样式currentStyle用getComputedStyle获取
用js的style属性可以获得html标签的样式,但是不能获取非行间样式.那么怎么用js获取css的非行间样式呢?在IE下可以用currentStyle,而在火狐下面我们需要用到getComputed ...
- style.attr,currentStyle,getComputedStyle获取元素css
老朋友object.style.attr 平常我们都是使用object.style.attr的方式来获取元素的属性, 但是这种方法具有很大的局限性——只能获取内联样式, 而下面的两种方法可以获取到元素 ...
- currentStyle&getComputedStyle获取属性
方法如下: function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; // IE 中的方法 } ...
- C# 根据论文 像素差异算法【个人实验还是比较好使的】
论文地址:http://www.docin.com/p-1081596986.html 具体代码: 我转YUV,再通过上面的论文的方式比较近. YVU 介绍:https://blog.csdn.net ...
随机推荐
- C++ #if #endif #define #ifdef #ifndef #if defined #if !defined详解 (转)
(源)http://blog.csdn.net/sky1203850702/article/details/42024673 首先,让我们先从头文件开始,在很多头文件里,我们会看到这样的语句 #ifn ...
- 深入探索C++对象模型(六)
执行期语意学(Runtime Semantics) 对象的构造和析构(Object Constructor and Destructor) 一般而言,constructor和destructor的安插 ...
- iptables实用教程(一):基本概念和原理
概述 iptables是linux自带的防火墙软件,用于配置IPv4数据包过滤或NAT(IPv6用ip6tables). 在linux上,防火墙其实是系统内核的一部分,基于Netfilter构架,基本 ...
- shell网络客户端
需要把线上的access日志发送到另一个程序接收 开始想着用python实现,虽然python也有实现类似tail -F的方式,但太麻烦,而且效率也有折扣 偶然发现了shell可以实现网络client ...
- PHP面向对象笔记解析
PHP的面向对象是很重要的内容,也是很常用的内容.所以现在就把PHP面向对象进行整理了一下. 顺带,我会在后面把我整理的一整套CSS3,PHP,MYSQL的开发的笔记打包放到百度云,有需要可以直接去百 ...
- Java模拟http请求调用远程接口工具类
package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...
- c# networkcomms 3.0实现模拟登陆总结
最近项目需要做一个客户查询状态系统,当前上位机缺少服务功能,于是找到了networkcomms 开源框架,作为项目使用. 最新版networkcomms 下载地址:https://github.com ...
- swiper.js 碰到的坑
1. swiper隐藏之后,再显示,滑动不流畅,且无限滑动会失败: 解决方法: 添加一下两个属性 observer: true,//修改swiper自己或子元素时,自动初始化swiper observ ...
- 错误Fatal error: Call to undefined function mb_strlen()的解决办法
其实这个就是没有开启php_mbstring模块.Windows下只需要修改安装目录下的php.ini文件把extension=php_mbstring.dll前面的“#”号注释符去掉保存后重启Apa ...
- Linux下配置tomcat+apr+native应对高并发
摘要:在慢速网络上Tomcat线程数开到300以上的水平,不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待.但是配上APR之后,Tomcat将以JNI的形式调用Apache HTTP服 ...