仅当将新数据添加到div时,滚动到div的底部

时间:2013-11-22 07:22:30

标签: javascript jquery asp.net html scroll

这是每当页面刷新时我保持div“currentinfo”的滚动位置的方式。 但是当我将新数据添加到div时,我想滚动到div的底部。怎么可能?

<form id="form1" runat="server"><asp:ScriptManager ID="manager" ScriptMode="Release" runat="server" ></asp:ScriptManager>
    <script type="text/javascript">

      // It is important to place this JavaScript code after ScriptManager1
      var xPos, yPos;
      var prm = Sys.WebForms.PageRequestManager.getInstance();

      function BeginRequestHandler(sender, args) {
          if ($get('<%=currentinfo.ClientID%>') != null) {
          // Get X and Y positions of scrollbar before the partial postback
              xPos = $get('<%=currentinfo.ClientID%>').scrollLeft;
              yPos = $get('<%=currentinfo.ClientID%>').scrollTop;
        }
     }

     function EndRequestHandler(sender, args) {
         if ($get('<%=currentinfo.ClientID%>') != null) {
           // Set X and Y positions back to the scrollbar
           // after partial postback
             $get('<%=currentinfo.ClientID%>').scrollLeft = xPos;
             $get('<%=currentinfo.ClientID%>').scrollTop = yPos;
         }
     }

     prm.add_beginRequest(BeginRequestHandler);
     prm.add_endRequest(EndRequestHandler);
 </script>
....
......
</form>

1 个答案:

答案 0 :(得分:0)

当您向“currentinfo”div添加内容时,请执行以下操作:

var currentinfoDiv =  $get('<%=currentinfo.ClientID%>');

//this will make the div scroll to bottom
currentinfoDiv.scrollTop = currentinfoDiv.scrollHeight;