2016-11-21 34 views
0

我的目标是让一个div内的GridView拥有固定邮件头以及回发后保持滚动位置。我有两个单独工作的函数,但由于我对JavaScript语法知之甚少,因此我无法合并这些函数。有人可以帮我吗?谢谢!合并Javascript函数以维护固定邮件回传

相关的代码:

<script language="javascript" type="text/javascript"> //FUNCTION 1 Static Header 
        function MakeStaticHeader(gridId, height, width, headerHeight, isFooter) { 
         var tbl = document.getElementById(gridId); 
         if (tbl) { 
          var DivHR = document.getElementById('DivHeaderRow'); 
          var DivMC = document.getElementById('DivMainContent'); 
          var DivFR = document.getElementById('DivFooterRow'); 

          //*** Set divheaderRow Properties **** 
          DivHR.style.height = headerHeight + 'px'; 
          DivHR.style.width = (parseInt(width) - 0) + 'px'; 
          DivHR.style.position = 'relative'; 
          DivHR.style.top = '0px'; 
          DivHR.style.zIndex = '10'; 
          DivHR.style.verticalAlign = 'top'; 
          DivHR.style.alignContent = 'center'; 

          //*** Set divMainContent Properties **** 
          DivMC.style.width = width + 'px'; 
          DivMC.style.height = height + 'px'; 
          DivMC.style.position = 'relative'; 
          DivMC.style.top = -headerHeight + 'px'; 
          DivMC.style.zIndex = '1'; 
          //****Copy Header in divHeaderRow**** 
          DivHR.appendChild(tbl.cloneNode(true)); 
         } 
        } 



        function OnScrollDiv(Scrollablediv) { 
         document.getElementById('DivHeaderRow').scrollLeft = Scrollablediv.scrollLeft; 
        } 


       </script> 
       <script type="text/javascript"> // FUNCTION 2 Maintain Scroll 
        window.onload = function() { 
         var h = document.getElementById("<%=hfScrollPosition.ClientID%>"); 
         document.getElementById("<%=DivMainContent.ClientID%>").scrollTop = h.value; 
        } 
        function SetDivPosition() { 
         var intY = document.getElementById("<%=DivMainContent.ClientID%>").scrollTop; 
         var h = document.getElementById("<%=hfScrollPosition.ClientID%>"); 
         h.value = intY; 
        } 

       function afterpostback() { 
       var h = document.getElementById("<%=hfScrollPosition.ClientID%>"); 
       document.getElementById("<%=DivMainContent.ClientID%>").scrollTop = h.value; 
       } 
       </script> 

       <asp:HiddenField ID="hfScrollPosition" runat="server" Value="0" /> 

        <div style="overflow: hidden;" id="DivHeaderRow"></div> 
        <div style="overflow: scroll;" onscroll="SetDivPosition()" id="DivMainContent" runat="server"> 
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" AutoGenerateColumns="True" ....> </asp:GridView> 
        </div> 

a a

回答

0

一个巨大的简单的解决。刚添加

<div style="overflow: scroll;" onscroll="SetDivPosition(); OnScrollDiv(this)" id="DivMainContent" runat="server"> 

专注于OnScrollDiv(this)专门添加在函数中。奇迹般有效。