getBoundingRect() returns the PREVIOUS page data, and NOT the current page...

This is my code:

$w.onReady(function () {
	wixWindow.getBoundingRect().then((sizeInfo) => {
		let scrollY = sizeInfo.scroll.y;
		$w('#textDebug').text = "Current Y Scroll = " + scrollY
	})
});

So you can see that I am using the then() method to wait for the info, and also this is contained with in the onReady() of the page…so I can’t see any timing issues.

I am expecting the above to always show Current Y Scroll = 0.

The code works fine when loading the page directly (e.g. by a Ctrl-F5 refresh after publish), but if you come to it from another page and are already scrolled down on that page (e.g. you click a link to this page from the bottom of another page) then textDebug shows the scroll position of the PREVIOUS page, even though you land on this page at the very top, at y = 0…

What am I doing wrong please?

You can add a timeout of 100ms:

$w.onReady(function () {
setTimeout(() => {
	wixWindow.getBoundingRect().then((sizeInfo) => {
		let scrollY = sizeInfo.scroll.y;
		$w('#textDebug').text = "Current Y Scroll = " + scrollY
	})
},100)
});
1 Like

Thank you JD, I’ve tried that and it now works. I appreciate it.

I’m curious as to why this is needed, as I can’t quite understand it… surely the onReady function is for when the page has finished loaded and is ready? And how does the current page get passed the scroll location history of the previous page? Also, could your workaround be maybe browser or PC specific (i.e. might an older browser or very slow PC need more than 100ms for example?)

Thanks again.

My guess is that when a new page loads, it starts from the current position (i.e. from the position of the previous page scroll) and once it’s ready the position changes to 0, 0 but it all happens so fast that you can’t notice it. However the computer is fast enough, and it registers the initial location unless you add some timeout.

1 Like

And I guess 100ms it’s a long delay, it’ll probably be more than enough for every modern computer.

Ace, thank you. That all makes sense.

1 Like

@jonatandor35 I think technically it’s because $w.onReady is technically firing when the $w selector is ready, not just the page. There’s a chance that $w will be ready before the document itself, or rather some aspects of the document.

@skmedia you’re probably right.

Hi. Please help me solve a similar problem…

Site address is currently https://jakebarrow.wixsite.com/jbaarchitecture

When I click a link on a page when in a scrolled down position (say halfway down that page) the next page loads in the same position as the previous page (before scrolling awkwardly to the top).

I need each new page to load directly at the top (i.e. y=0) regardless of the scroll position of the previous page.

I have tried the below code in the ’ site ’ section. This does not work for me, the (‘#textDebug’) section is not recognised? Not sure what I can do differently?

$w . onReady ( function () {
setTimeout (() => {
wixWindow . getBoundingRect (). then (( sizeInfo ) => {
let scrollY = sizeInfo . scroll .y;
$w ( ‘#textDebug’ ). text = "Current Y Scroll = " + scrollY
})
}, 100 )
});