css脚本化


dom.style.prop
1:可读写行间样式,没有兼容性问题,碰到float的保留字属性,其前面应加css  (不加也可以)
2:复合属性必须拆解,  组合单词变成小驼峰式
border:"1px solid black";
属性拆解
borderWidth:"1px";
borderStyle:"solid";
borderColor:"block";
小驼峰
backgroungColor:"red";

    3:写入的值必须是字符串格式;


查询计算样式

window.getComputedStyle()
两个值  1:某一个元素   2:null
1.只读
2.返回的计算样式的值都是绝对值,没有相对单位
width:'10em '  返回width:'160px'    
backgroungColor:"red";    返回backgroungColor:"rgb(255,0,0)"; 
ie8以下不兼容使用
ele.currentStyle
只读
返回的计算样式的值不是进过转换的绝对值

ie独有


*************

  利用window.getComputedStyle获取当前元素的伪元素
  window.getComputedStyle(div,'after');

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	div:after{
		content: '';
		display: inline-block;
		width: 50px;
		height: 50px;
		background: gray;
	}
</style>
</head>
<body>
	<div style="width: 100px;height: 100px;background: red;"></div>
</body>

<script>
var div = document.getElementsByTagName('div')[0];
</script>
</html>

查询到div的after的width为50px

// 封装getStyle
function  getStyle(elem,prop){
	if(window.getComputedStyle){
		return window.getComputedStyle(elem,null)[prop];
	}else {
		return elem.currentStyle[prop];
	}
}

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>利用getStyle实现一个块做加速运动</title>
	<style></style>
</head>
<body>
	<div style="width: 100px;height: 100px;background: red;position:absolute;left: 0"></div>
</body>

<script>

// 封装getStyle
function  getStyle(elem,prop){
	if(window.getComputedStyle){
		return window.getComputedStyle(elem,null)[prop];
	}else {
		return elem.currentStyle[prop];
	}
}
var div = document.getElementsByTagName('div')[0]
var w = window.getComputedStyle(div,'after').width;
var speed = 2
var timer =setInterval(function(){
	speed +=speed/7;
	div.style.left=parseInt(getStyle(div,'left'))+speed+"px";
	if(parseInt(div.style.left)>500){
		clearInterval(timer)
	}
},10)

</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值