js获取当前滚动元素

1,601 阅读1分钟

获取当前滚动元素

function findscroller(element) {
  element.onscroll = function () {
    // 返回当前滚动元素\
    console.log(element);
  };
  Array.from(element.children).forEach(findscroller);
}
findscroller(document.body)

获取要滚动元素的距离

const getOffsetSize = function (Node: any, offset?: any): any {
  if (!offset) {
    offset = {
      x0,
      y0,
    };
  }
  if (Node === document.bodyreturn offset;
  offset.x += Node.offsetLeft;
  offset.y += Node.offsetTop;
  return getOffsetSize(Node.offsetParent, offset);
};