zoukankan      html  css  js  c++  java
  • [原]js获取dom元素的实际位置及相对坐标

    关键API:

    Element.getBoundingClientRect()

    mdn:https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

    用法:

    var rect = element.getBoundingClientRect();
    console.log(rect.top, rect.right, rect.bottom, rect.left);

    获取相对位置:

    var bodyRect = document.body.getBoundingClientRect(),
        elemRect = element.getBoundingClientRect(),
        offset   = elemRect.top - bodyRect.top;
    
    alert('Element is ' + offset + ' vertical pixels from <body>');

    参考网址:http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element

    我只是个搬运工,一起学习了~

  • 相关阅读:
    jdk.jre.jvm 三者之间的关系?
    SQL7
    SQL6
    sql题
    sql题
    sql
    简说SQL题
    递归算法
    JAVA的简单描述
    第一篇技术博客
  • 原文地址:https://www.cnblogs.com/DarkMaster/p/6767662.html
Copyright © 2011-2022 走看看