我的JS scrollTo函数不会滚动页面

时间:2017-10-04 22:33:10

标签: javascript html asp.net

我的目标是让一个可点击的按钮/链接滚动到页面中的某个位置。我已经通过 Michael Whinfrey 引用了这个answer,代码完全适用于小提琴而不是我的。

这可能有点乱,但我在表格中有多个GridView,而一些 td div 而有些不在。同样在后面的代码中,我在Page_load中有以下代码。

Page.MaintainScrollPositionOnPostBack = false;

下面简化了我的aspx页面。

<html>
<head>
  <script type="text/javascript">
    function scrollTo(name) {
            ScrollToResolver(document.getElementById(name));
        }
        function ScrollToResolver(elem) {
            var jump = parseInt(elem.getBoundingClientRect().top * .2);
            document.body.scrollTop += jump;
            document.documentElement.scrollTop += jump;
            if (!elem.lastjump || elem.lastjump > Math.abs(jump)) {
                elem.lastjump = Math.abs(jump);
                setTimeout(function () { ScrollToResolver(elem); }, "100");
            } else {
                elem.lastjump = null;
            }
        }
    </script>
</head>
<body>
  .......some input/textbox......
  <button onclick="scrollTo('divDesignation')">TEST</button>
  <div>
    <table>
      <tr>
        <td>
          <asp:GridView id="gv1" .....>
      ......the whole table has 6~7 rows of GridView data that is not displaying here.....
      <tr>
        <td>
          <div id="divDesignation" runat="server" style="max-height:600px; overflow-y:auto;">
            <asp:GridView id="gvDesignation" ......
          </div>
        </td>
      </tr>
</body>
</html>

我正在尝试在单击按钮时滚动到divDesignation,但它根本不会。

感谢任何帮助。

0 个答案:

没有答案
相关问题