2011-12-27 123 views
0

我生成img標籤動態再提供阻力,之後,我將其附加到容器中,然後設置其位置在容器中心的大小的功能。這一切工作正常,但追加後,當我撤回頂部和左上方添加img標籤的位置,我越來越頂部0和0。當我調整一次後,我就獲得了正確的位置。爲什麼會發生這種情況,我總是很無情。請幫助。無法獲得頂部和左側值

下面是代碼

var img_clip = document.createElement('img'); 
    $(img_clip).attr("id", "dyndiv" + count); 
    $(img_clip).attr("src", current_clip) ;  
    $(img_clip).attr("width", 30); 
    $(img_clip).attr("height",50);                 
     var $ctrl = $(img_clip).resizable({ containment: '#containment-wrapper'}).parent().addClass("resizable").draggable({ containment: '#containment-wrapper', cursor: 'move',delay: 200,distance: 30, opacity: 0.35}); 
     objid = "dyndiv" + count ; 
     $(img_clip).css("z-index" , $(img_clip).css("z-index") + count); 
     $('#sel_obj_text').val("Image"); 
     count++; 
     $("#containment-wrapper").append($ctrl); 
      $('#' + objid).parent().position({ 
        of: $("#containment-wrapper"), 
        my: "center" + " " + "center", 
        at: "center" + " " + "center" 
               }); 

後來在其他部分的代碼,我想從以下行

$(this).parent().css("left") 
    $(this).parent().css("top") 
+2

一個的jsfiddle例子是,在這種情況下非常有幫助! '$(this).parent()'是一個對象嗎?如果是的話,試試'$(this).parent()。offset()。left'(和.top)或者如果你想要相對位置$(this).parent()。position()。left'。 – Armin 2011-12-27 15:02:33

+0

yes $(this).parent()是通過調整大小功能創建的div包裝器。 – 2011-12-27 15:07:16

+0

那麼'.offset()'和/或'.position()'方法應該工作:) – Armin 2011-12-27 15:09:42

回答

0

你好我能夠解決問題。

當我從相對於絕對的div位置改變它的作品。

$(this).parent().css("position","absolute"); 

問候 馬諾

0

獲得位置試試這個:

$(this).parent().offset().left 
$(this).parent().offset().top 
+0

兩者都給予0甚至是位置,一旦我調整圖像大小,我開始得到正確的值。:( – 2011-12-27 15:19:36

+0

我想你需要檢查一下你的'this'指向的是什麼,如果碰巧指向文檔對象,它總會給你'0,0',我建議在位置之前放置一個斷點代碼並檢查「this」實際包含的內容 - 確保它確實指向您需要的元素。 – techfoobar 2011-12-27 15:25:40

相關問題