0

我有一个小问题想问。

首先,我使用 JS 来加载页面。

function ReLoad() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function () {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
             document.getElementById("box").innerHTML = xhttp.responseText;
          }
       };
      xhttp.open("GET", "calc/index.html", true);
      xhttp.send();
 }

现在,我的问题是当我将此索引插入主页时,因为页面会立即自动滚动到开头,我想加载页面而不会产生这种烦人的效果。

请问,有人可以帮我吗?谢谢

4

2 回答 2

0

您可以存储滚动位置;

var currentPosition = window.pageYOffset || document.documentElement.scrollTop;

并重新应用它:

window.scrollTo(0, currentPosition);

所以你的代码会变成:

function ReLoad() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
     if (xhttp.readyState == 4 && xhttp.status == 200) {
         var currentPosition = window.pageYOffset || document.documentElement.scrollTop;
         document.getElementById("box").innerHTML = xhttp.responseText;
         window.scrollTo(0, currentPosition);
      }
   };
  xhttp.open("GET", "calc/index.html", true);
  xhttp.send();
}
于 2016-03-12T14:22:11.910 回答
0

我终于解决了它添加到代码的末尾:

e.preventDefault();
return false;
于 2016-03-14T20:47:55.680 回答