首页 > Windows开发 > 详细

js计算机样式window.getComputedStyle(ele,null)2

时间:2018-03-15 10:54:50      阅读:335      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>window.getComputedStyle(div,null)</title>
    <style type="text/css">
        *{margin:0;padding:0}
    </style>
</head>
<body>
<div style="width: 100px; height: 100px; background: red; position: absolute;left: 0; top:0"></div>
<script type="text/javascript">
    //封装
    function getStyle(elem,prop){
        if(window.getComputedStyle){        //如果它存在的话(兼容谷歌)
            return window.getComputedStyle(elem,null)[prop];//prop作为参数字符串传进来,所有得中括号
        }else{
            return elem.currentStyle[prop];   //兼容IE
        }
    }
    var timer;
    var div = document.getElementsByTagName(div)[0]
    timer=setInterval(function(){
        div.style.left=parseInt(getStyle(div,left))+1+"px";//注意:getStyle(div,‘left‘)打印出来是0px,所以用parseInt取整数
    },10)
    div.onclick = function() {
        clearInterval(timer)
    }
</script>
</body>
</html>

效果图:

技术分享图片

 

js计算机样式window.getComputedStyle(ele,null)2

原文:https://www.cnblogs.com/huanghuali/p/8571474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!