JQuery - Copy dimensions and absolute position of element

点点圈 提交于 2020-01-03 05:37:09

问题


I'm trying to copy a element's dimensions and the position(relative to the document) onto another element.

Ex:

  var SelectedElement = $("div#MyTargetElement");

  // The CopiedButEmpty element is a div with absolute position that is meant to hover/float above the selected element.
  $("div#CopiedButEmpty").css("width", SelectedElement.width()).css("height", SelectedElement.height())
    .css("left", SelectedElement.offset().left).css("top", SelectedElement.offset().top)
    .css("marginTop", SelectedElement.css("marginTop")).css("marginLeft", SelectedElement.css("marginLeft"))
    .css("marginRight", SelectedElement.css("marginRight")).css("marginBottom", SelectedElement.css("marginBottom"))
    .css("paddingLeft", SelectedElement.css("paddingLeft")).css("paddingTop", SelectedElement.css("paddingTop"))
    .css("paddingRight", SelectedElement.css("paddingRight")).css("paddingBottom", SelectedElement.css("paddingBottom"));

But in some cases, it still does not give me the correct position. Am I missing something? Is there any way to easily copy an elements position(relative to the document), so my element "CopiedButEmpty" can hover the element.


回答1:


I wonder why you didn't chose to use jquery clone().



来源:https://stackoverflow.com/questions/1113758/jquery-copy-dimensions-and-absolute-position-of-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!