如:.testCSS{position: absolute;left:0%;top:%10; width:20%; height:20%;}
<img id='idImg' alt='1.jpg' class=testCSS/>js中怎么获取left,top,width,height的真是位置,而非百分数 var image = document.getElementById('imgId');
window.getComputedStyle(image).left;  //这个获取的是百分比

解决方案 »

  1.   

    offsetLeft,offsetTop....
      

  2.   

    position: absolute;left:0%;top:%10; width:20%; height:20%;position: absolute;left:0%;top:10%; width:20%; height:20%;left本来就是0
      

  3.   

    window.getComputedStyle(image).left 数字px
    image.offsetLeft 数字
      

  4.   

    可以用getBoundingClientRect()方法
    https://blog.csdn.net/gao_xu_520/article/details/80365799注意这个是获取元素到窗口可视范围的距离。如果获取元素到页面的距离需要加上页面滚动条的位置
    rectObject.left + (document.body.scrollLeft || document.documentElement.scrollLeft)
    rectObject.top + (document.body.scrollTop || document.documentElement.scrollTop)
      

  5.   

    为什么BoundingClientRect返回的left/top/right/bottom都是0啊。我实在window.onload里面做的。
      

  6.   

    为什么BoundingClientRect返回的left/top/right/bottom都是0啊。我实在window.onload里面做的。
    可能是因为你设置的百分比并没有生效。元素要设置百分比生效,需要先设置 html, body {height: 100%;}