//获取元素的值
function getStyle(obj,name){
if(window.getComputedStyle){
return getComputedStyle(obj,null)[name]
}
else{
return obj.currentStyle[name]
}
}
调用可直接获取
案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
margin:0;
padding:0;
}
.box{
width:200px;
height:200px;
border-radius:50%;
background:red;
position:absolute;
left:0px;
top:0px;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
<script>
let box = document.getElementsByClassName('box')[0];
box.onclick = function(){
//获取元素的值
function getStyle(obj,name){
if(window.getComputedStyle){
return getComputedStyle(obj,null)[name]
}
else{
return obj.currentStyle[name]
}
}
setInterval(function(){
var oldLeft = parseInt(getStyle(box,"left"))
let newLeft = oldLeft + 100;
box.style.left = newLeft+'px';
},1000)
}
</script>
</html>
网友评论