Join the Webstudio community

Updated last month

Get scroll depth and assign to a variable

At a glance
The community member is looking to track a user's scroll depth on their webpage using JavaScript and store that value in a variable. They are having trouble determining the best place to insert their custom code and how to integrate it with their existing data variable. The discussion in the comments suggests using position: fixed and adding an HTML embed script with a scroll listener to set the opacity based on the current scroll position. Another suggestion is to use position: sticky for the navigation bar, placing it after the hero section and setting top: 0 to make it stick. The community members agree that the position: sticky approach is simpler and more effective.
Useful resources
I’m looking to track a user’s scroll depth on my webpage using JavaScript and store that value in a variable. However, I’m having trouble determining the best place to insert my custom code and how to integrate it with my existing data variable. Could you provide some guidance on how to achieve this?
Marked as solution
you can create this menu with position: fixed and add html embed script with scroll listener which would set opacity based on current scroll.
View full solution
B
t
O
19 comments
What exactly do you want this for?
Hi, Bogdan. I want to make my nav bar visible/hidden based on scroll depth
Are you sure this is the right approch? Doesn't this still leave the nav bar visible all the time?
What I want to achieve is that on the hero section, the nav bar is hidden
At what point on scroll do you want to make it hidden?
Do you have any examples?
Here's one example: https://www.mealplan.top/
You can see the nav bar appears when the user has scroll out of the hero section
you can create this menu with position: fixed and add html embed script with scroll listener which would set opacity based on current scroll.
In the future there will be "stuck" breakpoint and you will be able to drop this script in favor of it.
Though position: sticky if you put the menu after the hero section would be much easier.
Using sticky is a good idea, but I can't seem to get it to work. Placing it under the hero section with position = sticky, it doesn't seem to stick: https://www.getcodeless.tech/
Give it top: 0
And now it works 🙂
This is not bad, simpler than my appraoch. Thank you Bogdan
we have examples for this using animotion
its a template in the marketplace
Can you tell me which template you refer to?
Yes, I discovered this too, should have read your message sooner 😄 But wonderful add on 🙂
Add a reply
Sign up and join the conversation on Discord