gpt4 book ai didi

JavaScript。
  • 元素相对于浏览器边界的绝对位置?
  • 转载 作者:行者123 更新时间:2023-11-28 05:07:43 24 4
    gpt4 key购买 nike

    如何知道<li>的绝对位置相对于浏览器边界的元素?

    <html>
    <body style="overflow: hidden">
    <div ID="Ticker" style="position:absolute; white-space: nowrap;">
    <ol style="list-style-type:decimal; ">
    <li style="float: left; width: 100px; padding: 2px 0px;">Test</li>
    <li style="float: left; width: 100px; padding: 2px 0px;">Test</li>
    <li ID="TargetElem" style="float: left; width: 100px; padding: 2px 0px;">Test</li>
    <li style="float: left; width: 100px; padding: 2px 0px;">Test</li>
    <li style="float: left; width: 100px; padding: 2px 0px;">Test</li>
    <li style="float: left; width: 100px; padding: 2px 0px;">Test</li>
    <li style="float: left; width: 100px; padding: 2px 0px;">Test</li>
    </ol>
    </div>
    <script type="text/javascript" src="ticker.js" language="javascript"></script>
    </body>
    </html>

    我需要一种方法来知道元素 <li> 的左值与 ID="TargetElem" .

    最佳答案

    借自http://www.quirksmode.org/js/findpos.html ...

    // findPosition() by quirksmode.org
    // Finds the **absolute** position of an element on a page
    function findPosition(obj) {

    var curleft = curtop = 0;
    if (obj.offsetParent) {
    do {

    /*
    * The loop continues until the object currently being investigated
    * does not have an offsetParent any more. While the offsetParent is
    * still there, it still adds the offsetLeft of the object to curleft,
    * and the offsetTop to curtop.
    */
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;

    } while (obj = obj.offsetParent);
    }

    return [curleft,curtop];
    }

    此功能应该在 IE6、IE7、IE8、Safari、Firefox 和 Chrome 中运行良好,但我尚未对其进行彻底测试。此外,本文可能与您的需求相关...

    希望对您有所帮助。

    关于JavaScript。 <li> 元素相对于浏览器边界的绝对位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5042461/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com