Js on-stack and off-stack

clipboard.png
modifying scrollTop is not valid because it is a shallow copy. It specifies a value that has nothing to do with the original object. How to write it?

Feb.28,2021

 function scroll() {
            return {
                top: window.pageYOffset || document.documentElement.scrollTop,
                left: window.pageXOffset || document.documentElement.scrollLeft
            }
        }

scroll (). Top


var scrollTop =. write in the timer


both have getter and setter. You take the value out of the value. Setter is to modify the actual content. Your changes to your variables have nothing to do with the original setter.


const realScroll = [
  [document.body, 'scrollTop'],
  [window, 'pageYOffset'],
  [document.documentElement, 'scrollTop']
];
const len = realScroll.length;
let time = setInterval(function() {
  let i = 0,
      getter = function() {
        return realScroll[i][0][realScroll[i][1]];
      },
      setter = function(v) {
        realScroll[i][0][realScroll[i][1]] = v;
      },
      top;
  for (; i < len; iPP) {
    top = getter();
    if (top) {
      setter(top - 200);
      if (top <= 0) {
        clearInterval(time);
      }
    }
  }
});
Menu