2014-10-21 43 views
0

我有兩個相鄰的表格,右邊的那個div在使用show()時顯示頁面的問題。如何讓jQuery show()保留在頁面

我該如何預防?

$("tr.invoice_head td:last-child a").hover(
     function(){$(".custNotes-span",this).position({ 
      of: '.custNotes-span', 
      my: 'right top', 
      at: 'right top', 
      offset: '.custNotes-span' 
     }).css({ 
      'z-index':'100', 
      'width': '200px', 
      'position':'absolute'}).show();}, 
     function(){$(".custNotes-span").hide();} 
    ); 

更新:的jsfiddle鏈接 - http://jsfiddle.net/r60ywb0L/2/

我的道歉,這裏是的jsfiddle包括使用JS和CSS的副本。有兩個表相互浮動,我試圖在tr.invoice_head的最後一個td內懸停在錨定鏈接上時,獲取div.custNotes-span以顯示頁面的其餘部分。

+0

您可以創建你的HTML提琴包括在內? – APAD1 2014-10-21 23:59:50

+0

很難說沒有HTML。但我認爲它與'show()'設置'display:block'這一事實有關,而這正在搞亂佈局。你可以嘗試用'show()'調用'css('display','inline-block')'。 – 2014-10-22 00:04:08

+0

檢查它是哪個表,如果它是第二個表格,也許使用'left top'。 'if($(this).closest('#table2ID')。length)...' – charlietfl 2014-10-22 00:04:10

回答

1

在演奏小提琴的時候,我發現:

  • CSS需要被定位之前被應用。
  • 定位時(但不一定「可見」),元素需要在DOM中呈現。
  • position()地圖的of屬性是更好地設置爲最接近的錶行
  • collision:'fit'似乎工作比默認collision:'flip'更好,雖然你可能沒有注意到,除了在次小提琴的差異。
  • 元素可以定位一次。每次放映都沒有必要重新定位它們。

以下解決方法利用CSS visibility來允許在避免FOUC時進行正確定位。

  • 申請的.css(),包括visibility:hidden
  • 「秀」 與.show()(但仍隱藏)
  • 申請.POSITION()
  • 隱藏與.hide()
  • 使 「看得見的」 與visibility:visible(但仍然隱藏)

然後元素可以顯示/隱藏與.show()和在懸停處理程序中使用d .hide()

// As the positioning map and the css map get used in a loop, it's worth assigning them outside the loop. 
var maps = { 
    css: { 
     'z-index': '100', 
     'width': '200px', 
     'position': 'absolute', 
     'visibility': 'hidden' 
    }, 
    pos: { 
     'of': null, //added dynamically below 
     'my': 'right top', 
     'at': 'right top', 
     'collision': 'fit' 
    } 
}; 

//Apply CSS and pre-position the notes 
$(".custNotes-span").each(function() { 
    $(this) 
     .css(maps.css) 
     .show() 
     .position($.extend({}, maps.pos, {'of': $(this).closest("tr")})) 
     .hide() 
     .css('visibility','visible'); 
}); 

//The maps have done their work and can be deleted from the closure. 
delete maps.css; 
delete maps.pos; 

//Hover actions 
$(".custNotes a").hover(function() { 
    $(".custNotes-span", this).show(); 
}, function() { 
    $(".custNotes-span", this).hide(); 
}); 

選擇器被修改爲更友好一些。

DEMO

+1

這是迄今爲止我在StackOverflow上收到的最有幫助和最好的建議。就像我之前說過的,我會爲jQuery嶄露頭角,甚至沒有意識到這些屬性的順序是重要的。我肯定會花一點時間來消化一切,並進行勤奮的研究,以進一步瞭解爲什麼背後有極有幫助的解釋;但是你做了一個很棒的工作,談論我(和其他任何讀這篇文章的人),正是爲什麼你所做的改變很重要。非常真誠的感謝並向你致敬! – Eric 2014-10-22 14:18:01

+0

還有你推薦用於jQuery的任何資源還是隻是練習,練習和練習? – Eric 2014-10-22 14:23:48

+0

非常感謝您的好評。關於jQuery的唯一一本書是[David Flanagan的jQuery Pocket Reference]](http://www.amazon.co.uk/s/?ie=UTF8&keywords=jquery+pocket+reference&tag=googhydr-21&index=aps&hvadid=34729088148&hvpos= 1T1&hvexid =&hvnetw = G&hvrand = 5891366538417268591&hvpone =&hvptwo =&hvqmt = b&hvdev = C REF = pd_sl_21143qacxm_b)。雖然自本書發佈以來,jQuery已經發生了很多變化,但基本功能並沒有改變,它很好地覆蓋了它們。只需幾美元,這是一個真正的便宜貨。在線上,你無法做得比官方的[jQuery API文檔](http://api.jquery.com)更好。 – 2014-10-22 14:46:48