import { Vector2 } from 'touchcontroller'; export function getScroll(element: Element): Vector2 { //todo better const elementChild = element.children[0]; if (!elementChild) { throw new Error( `There should be at least 1. child element if you want to measure scroll.`, ); } const boundingRect = elementChild.getBoundingClientRect(); return new Vector2(boundingRect.left, boundingRect.top).scaleInPlace(-1); } /* //todo get working with fixed let documentScroll = TC.Vector2.Zero(); document.addEventListener('scroll',(event)=>{ const event_:any = event; documentScroll = new TC.Vector2(event_.pageX,event_.pageY); }); export function getScroll(element:Element):TC.Vector2{ const scroll = TC.Vector2.Zero(); let currentElement:Element|null = element; while(currentElement){ if(currentElement instanceof HTMLElement){ if(currentElement.style.position==='fixed'){ break; } } if(currentElement === window.document.body){ break; } const currentScroll = new TC.Vector2(element.scrollLeft,element.scrollTop) //console.log(currentElement,currentScroll); scroll.addInPlace(currentScroll); currentElement = currentElement.parentElement; } if(currentElement === window.document.body){ scroll.addInPlace(documentScroll); } return scroll; } /* setImmediate(()=>{ window.addEventListener('scroll',(event)=>{ console.log('window',event); }); document.addEventListener('scroll',(event)=>{ console.log('document',event); }); document.body.addEventListener('scroll',(event)=>{ console.log('document.body',event); }); }); */