const scrollPercent = getScrollPercent();
const mainDivHeight = (mainDivRef.current as any).offsetHeight;
const imgHeight = (imgRef.current as any).offsetHeight;
const offset = ((imgHeight - mainDivHeight) * scrollPercent) / 100;
setBgrImgStyle(x =>
Object.assign({}, x, { transform: `translateY(-${offset}px)` })
);
As seen, we've got a lot of const
declared that are used only once, meaning they could be simply inlined.
I do it frequently to improve readability, sometimes perhaps a too much.
What's the performance cost? The code above is JS, nevertheless I'd be happy to receive a language-agnostic answer.