Hello, friends!
Just in case this topic is still relevant (because it indeed IS for me). Iāve found a workaround that was sufficient enough.
1) Youāll need WixCode so switch it on.
2) For some weird reason adding a hashtag to the end of your URL (i.e.: https://mySite.com#myAnchor) will mess with Wix router (I canāt be bothered figuring out this ridiculous behaviour) so just donāt use hashtags on your Wix site I guess. Also, adding a hashtag to my website link makes my main menu disappear⦠By the way, you can STILL access the hashtag, even though it gets āeatenā by the router I assume:
wixLocation.hash; //returns URL hash string (with "#" character)
- Add some reasonable āqueryā parameters to your URL (i.e.: https://mySite.com?a=services), the value of āaā parameter will indicate an anchor on your page (you may replace āaā with any variable name that is URI-valid). Now you can access query parameters via:
wixLocation.query; //returns array of query parameters
- Here comes another āpleasant surpriseā from Wix. For some incomprehensible reason the following code:
$w("#myAnchorId").scrollTo().then(() => {
console.log("Done with scroll");
});
doesnāt scroll me anywhere. No matter if I use my Anchor ID or any ID of any visible element on the page - the promise (returned by āscrollToā) gets resolved, but no scroll happens whatsoever. So this approach failed for me also. Throw it into a rubbish bin.
- WixCode is āso liberatingā that I canāt even get the positioning of a page element! So in the end I became āa bit upsetā and brute-forced it via:
let isMobile = (wixWindow.formFactor === "Mobile"); //this helps us determine if we're in mobile version
wixWindow.scrollTo(0, (isMobile ? 400 : 600)); //the second parameter being the Y coordinate (in pixels) of the page, also since the page height is different we need two values; in this case it's 400 for mobile and 600 for web
which is just plain stupid and quite an inflexible approach, but I honestly couldnāt do better. I triedā¦
- So, at the end of the day, the more I use Wix, the more my first āwowā-impression fades away. Iāve got at least 3 MAJOR complaints which really force me to consider solutions other than Wix. Maybe thatās the business model: āGet subscriptions money for a year, a chunk of users will submit and conform anyways, then weāll get a bunch of new ganders that will swallow the shiny bait. Repeat the cycle. Profitā. Hereās my result code anyways:
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';
$w.onReady(function(){ //this function runs after the page is loaded and is SLOW. Any reasonable Wix Page is SLOW AS HELL because there is A TON of unnecessary JavaScript code! Sorry... went on a rant again
let query = wixLocation.query;
let isMobile = (wixWindow.formFactor === "Mobile");
if (query.hasOwnProperty("a")) { //this line checks the presence of "a" query parameter, you may need to use some other name
switch (query.a) { //this accesses the query parameter "a", change the name according to your needs
case "services": //you go here if your URL is "https://mySite.com?a=services"
wixWindow.scrollTo(0, (isMobile ? 400 : 600)); //you can get the Y value by left-clicking on an anchor you have on your page and look at the "Position Y" parameter on the "editor panel" on the right
break;
case "about": //other cases are similar, this is for "https://mySite.com?a=about"
wixWindow.scrollTo(0, (isMobile ? 800 : 1200));
break;
default:
//do nothing
}
}
});
Hope this helps any of you, friends! Best regards