gpt4 book ai didi

javascript - 如何计算
  • 相对于其父容器的 offset()
  • 转载 作者:太空宇宙 更新时间:2023-11-04 14:08:21 24 4
    gpt4 key购买 nike

    我有一个 <li>它嵌套在包含 <ul> 的内部,我想要的是计算偏移量。但是有左上角的<ul>0, 0 .

    所以我的 HTML 是:

    <div style="margin: 0 auto; width: 760px; height: 760px; overflow: hidden;">
    <ul style="width: 2000px; height: 2000px; min-height: 2000px; position: relative;">
    <li style="position: relative; width: 40px; height: 40px;"></li>
    </ul>
    </div>

    我当前的 jQuery 是:

    thePos = $('ul li').offset();
    alert('X = '+thePos.left+', Y = 'thePos.top);

    但是,这将返回不是 0, 0 的偏移量.我猜它是在计算相对于文档边缘而不是父容器的偏移量。

    我该如何解决这个问题?

    谢谢。

    注意:position()也没有检索到正确的数字:(

    最佳答案

    尝试使用 position() 而不是 offset()

    来自 jQuery 文档:

    The .offset() method allows us to retrieve the current position of an element relative to the document. Contrast this with .position(), which retrieves the current position relative to the offset parent.

    关于javascript - 如何计算 <li> 相对于其父容器的 offset(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3422580/

    24 4 0
    文章推荐: jquery - 显示一个
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com