Can someone help me figure out why every time the function runs, it scrolls me back to the top of the page. I would like it to stay were I am on the page, I don’t care if it scrolls when the resolution changes but while on the current page I’d like it to not scroll back up to the top every time it runs. I think it might be because the wixLocation !== “/home1” isn’t working so it keeps refreshing the page and scrolling me back up but still don’t know why it’s not working…
Here’s my code:
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';
$w.onReady(function() {
setInterval(function() {makeResponsive()}, 400);
} );
function makeResponsive() {
wixWindow.getBoundingRect()
.then((windowSizeInfo)=>{
let windowWidth = windowSizeInfo.window.width;
if (((windowWidth <= 1280) && (wixWindow.formFactor === "Desktop")) && (wixLocation !== "/home2")) {
//show & hide or change style properties for 720p or below
wixLocation.to("/newsite/home2");
}
else if (((windowWidth > 1280) && (wixWindow.formFactor === "Desktop")) && (wixLocation !== "/home1")) {
//show & hide or change style properties for 1080p
wixLocation.to("/newsite/home1");
}
}
)}
There isn’t really a purpose for those other than I added them when I was trying to figure out way around the issue using scroll.to in reality it didn’t really help anything kinda just made a jittery scroll up to the top I was thinking that maybe if I added code to pull you back down to the right spot that it would just stay there but in all reality it didn’t help just hadn’t taken those bits of code out yet.
@alecraymccutcheon If I understand your code, you’re trying to move to another page based on the window size (if need).
So first of all, make sure you don’t have page transition animation.
Second, you can do the following:
Re-write:
let scrollY = windowSizeInfo.scroll.y;
in the beginning of your code:
$w.onReady(function() {
let p = wixLocation.query.p;
if (p){wixWindow.scroll.to(0,Number(p), {scrollAnimation: false});};
//etc...
@alecraymccutcheon I don’t know. But now that I’m looking at your code I see another problem. This line (and the similar line for the other page) is wrong:
wixLocation !== “/home2” —This is wrong —
You need to use one of the followings (it depends your site path, so test each of them):
wixLocation.path[0] !== “/home2”
OR
wixLocation.path[1] !== “/home2”