【问题标题】:the handle is giving its position according to the page, not its parent, how do I get its left position from its parent?句柄根据页面给出其位置,而不是其父级,我如何从其父级获取其左侧位置?
【发布时间】:2011-07-10 15:15:05
【问题描述】:

http://jsfiddle.net/nicktheandroid/TWKJb/5/

一旦你抓住处理程序并开始拖动它,左边的处理程序从 10px 开始,它应该从 0px 开始,如果我向容器填充添加 20px,它会将轨道从左边额外推 20px,现在当我第一次抓住手柄时,它从 30px 开始......

所以句柄从页面的最左侧获取其左侧位置。处理程序应该从其父级(轨道)给出其左侧位置,而不是从页面的最左侧..

谁能告诉我我做错了什么让句柄根据页面而不是其父级给出其左侧位置?我已经搞砸了好几个小时>:(

【问题讨论】:

  • "它是" == "它是"。 "its" 是所有格形式。
  • 这个标题太长了。请不要在每个问题的开头写“Jquery:”。你大写错了,我们已经有标签了。

标签: jquery position offset


【解决方案1】:

如果您像我刚才那样在 Chrome 中进行测试,那么这可能是 Chrome 似乎与.position() 存在的相当普遍的问题,阿贾霍。但一直是posted before on StackOverflow

jQuery.fn.aPosition = function() {
    thisLeft = this.offset().left;
    thisTop = this.offset().top;
    thisParent = this.parent();

    parentLeft = thisParent.offset().left;
    parentTop = thisParent.offset().top;

    return {
        left: thisLeft-parentLeft,
        top: thisTop-parentTop
    };
};

在演示中,我还在句柄上设置了position: relative;,因为它需要相对于它的父级。

更新的演示: http://jsfiddle.net/TWKJb/6/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多