简体   繁体   English

如何隔离或捕获对 ScrollIntoView 的调用

[英]How to Isolate or Catch a call to ScrollIntoView

Our team is working on a small bug in an online application on a large corporate website.我们的团队正在处理大型企业网站上在线应用程序中的一个小错误。

The Customer clicks Next to go to the next section, but there is no next URL - all is handled on the same web address.客户点击 Next to go 进入下一个部分,但没有下一个 URL - 所有都在同一个 web 地址上处理。

The bug is: On one of the "pages", the Customer clicks Next, and the focus on the following page goes to one of the bottom controls.错误是:在其中一个“页面”上,客户单击“下一步”,下一页上的焦点转到底部控件之一。 I don't know which control - but it is bypassing required fields by causing the page to scroll focus to the bottom of the form.我不知道哪个控件 - 但它通过使页面将焦点滚动到表单底部来绕过必填字段。 I am the only software developer on our team, and my background is in Windows Forms.我是我们团队中唯一的软件开发人员,我的背景是 Windows Forms。

In the code, VS Code found several instances of ScrollIntoView attached to base controls.在代码中,VS Code 发现了几个附加到基本控件的ScrollIntoView实例。 Breakpoints are useless because the code has to be deployed to the test server to see results.断点是无用的,因为必须将代码部署到测试服务器才能看到结果。

By using browser breakpoints in the javascript on mouse click-event, I was able to step to a segment of code that reads return Promise.resolve(config);通过在 javascript 中使用鼠标单击事件中的浏览器断点,我能够进入一段代码,该代码段显示为return Promise.resolve(config); - this is that file: - 这是那个文件:

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

import { getAccessToken } from 'bank-authentication';
import BankLogger from 'bank-logger';

var AuthenticationRequestInterceptor = /*#__PURE__*/function () {
  function AuthenticationRequestInterceptor() {
    _classCallCheck(this, AuthenticationRequestInterceptor);

    this._logger = new BankLogger();
  }

  _createClass(AuthenticationRequestInterceptor, [{
    key: "fulfilled",
    value: function fulfilled(config) {
      return getAccessToken().then(function (accessToken) {
        if (accessToken) {
          config.headers = config.headers || {};
          config.headers.Authorization = "Bearer ".concat(accessToken);
        }

        return Promise.resolve(config);
      }, function (error) {
        //make the call anyway to get the header answer
        return Promise.resolve(config);
      });
    }
  }, {
    key: "rejected",
    value: function rejected(error) {
      return error;
    }
  }]);

  return AuthenticationRequestInterceptor;
}();

export default AuthenticationRequestInterceptor;
//# sourceMappingURL=AuthenticationRequestInterceptor.js.map

In VS Code, I have searched the entire project for an instance of the text AuthenticationRequest , but it isn't in the code.在 VS Code 中,我在整个项目中搜索了文本AuthenticationRequest的实例,但它不在代码中。

What's a technique I can use to find out what is causing one of these "pages" to scroll to the bottom whenever it loads?我可以使用什么技术来找出导致这些“页面”之一在加载时滚动到底部的原因?

The code snippet that you have provided does not look like it is directly related to the scroll event that takes place.您提供的代码片段看起来与发生的滚动事件没有直接关系。 Perhaps it is caused by what happens after the promise has resolved.可能是 promise resolve 之后发生的事情造成的。 Nevertheless, to answer your question directly you can listen to ScrollIntoView by adding the onscroll event to either your body element or the div element where the scrolling takes place:不过,要直接回答您的问题,您可以通过将onscroll事件添加到您的body元素或发生滚动的div元素来收听ScrollIntoView

<div onscroll="preventScroll()">

This event will then trigger the function preventScroll which will execute code such as the prevention of scrolling:然后,此事件将触发 function preventScroll ,它将执行诸如防止滚动之类的代码:

function preventScroll() {
  const x = window.scrollX;
  const y = window.scrollY;
  window.onscroll = () => window.scrollTo(x, y);
}

If you want to stop scrolling temporarily, you can facilitate this by having an outside boolean variable such as shouldPreventScrolling and set it to true before the scroll event that you want to prevent is going to take place.如果你想暂时停止滚动,你可以通过外部 boolean 变量来实现这一点,比如shouldPreventScrolling并在你想要阻止的滚动事件发生之前将它设置为true You can then unset it to false outside of the function manually or do it in the timeout:然后,您可以在 function 之外手动将其取消设置为false或在超时时执行此操作:

let shouldPreventScrolling = true;
let isCurrentlyPreventingScrolling = false;

function temporarilyPreventScroll() {
  if (shouldPreventScrolling && !isCurrentlyPreventingScrolling) {
    isCurrentlyPreventingScrolling = true;
    const x = window.scrollX;
    const y = window.scrollY;
    window.onscroll = () => window.scrollTo(x, y);
    setTimeout(() => {
      shouldPreventScrolling = false;
      window.onscroll = temporarilyPreventScroll;
      isCurrentlyPreventingScrolling = false;
    }, 1000);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM