2011-06-09 40 views
1

我的ASP.Net應用程序從代碼隱藏中生成<asp:Table>。我需要的是當用戶滾動過去時,該表格的標題行向下滑動頁面。jQuery滑動表格標題

我嘗試使用JavaScript以下方法:

window.onscroll = function() { 

    //grab the current scroll position 
    var scrollY = document.body.scrollTop; 
    if (scrollY == 0) { 
     if (window.pageYOffset) 
      scrollY = window.pageYOffset; 
     else 
      scrollY = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0; 
    } 

    //grab the position of the header row I want to slide 
    var head = $("tr[name='headerrow']").offset(); 
    var headtop = head.top; 
    var headleft = head.left; 

    //if the user has scrolled past the top of the header row 
    if (scrollY > headtop) { 

     //code correctly reaches this point as alerts show 
     //alert('got here'); 

     //position the header row to the same as the scroll position 
     $("tr[name='headerrow']").offset({ top: scrollY, left: headleft }); 
    } 
} 

我不能讓該行移動。在各種瀏覽器開發工具中沒有錯誤消息。

任何幫助,將不勝感激。

編輯:我想呼籲該行的孩子offset()功能(即所有<th>元素)是這樣的:

$("tr[name='headerrow']").children().offset({ top: scrollY, left: headleft }); 

這就是現在的工作,但當然,他們都推因爲我使用的是標題行本身的left值......我會隨着我的進度保持更新,但與此同時,任何幫助都會一如既往地受到讚賞。

回答

0

解決方案:

使用表格行children()方法來讓你改變每個<th>元素的偏移。所述left值可以從offset()方法可以省略,即

$("tr[name='headerrow']").children().offset({ top: scrollY }); 
-1

您不能設置任何元素的偏移量。您應該使用css方法並設置頂部和左側參數。

+0

不正確時,偏移可以應用於大多數元素 - 見上述我的編輯。此外,我不能使用CSS來應用頂部和左側,因爲我沒有使用「絕對」或「固定」定位。 – Arj 2011-06-09 14:08:19