Javascript Archives - Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane | https://10webtest.10web.me/category/javascript/ Wordpress developer servicing Melbourne, Brisbane, Sydney, Perth, Adelaide, Canberra Thu, 02 Mar 2023 01:46:38 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://www.jane-james.com.au/wp-content/uploads/2021/07/cropped-1901_logo-01-1-32x32.jpg Javascript Archives - Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane | https://10webtest.10web.me/category/javascript/ 32 32 WordPress developer problems: How to Use an Intersection Observer to Create a Sticky Navigation Bar https://www.jane-james.com.au/wordpress-developer-problems-create-a-sticky-nav-after-viewport/ Thu, 02 Mar 2023 01:34:32 +0000 https://www.jane-james.com.au/?p=15415 As a WordPress Developer, a sticky navigation bar is an often requested feature. A sticky navigation bar is a navigation bar that remains at the top of the screen even when the user scrolls down the page. It allows users to navigate to different sections of the website without having to scroll back up to […]

The post WordPress developer problems: How to Use an Intersection Observer to Create a Sticky Navigation Bar appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>
Wordpress Development

As a WordPress Developer, a sticky navigation bar is an often requested feature. A sticky navigation bar is a navigation bar that remains at the top of the screen even when the user scrolls down the page. It allows users to navigate to different sections of the website without having to scroll back up to the top. In this tutorial, we will learn how to create a sticky navigation bar using an Intersection Observer. The reason were using an intersection observer and not just making the site-header position:fixed, is because the header is going to kick in only after the defined viewport.

Intersection Observer is a JavaScript API that allows you to observe changes in the intersection of an element with another element or the viewport. By using the Intersection Observer API, we can detect when an element comes into view and when it goes out of view. In our case, we want to detect when our header element intersects with the viewport and toggle a class on our navigation bar element to make it sticky. This is a useful feature to have on projects and a common request in WordPress development.

Let’s take a look at the code:

The first three lines of code define the stickyNav, header, and navHeight variables. stickyNav and header are used to select the elements we want to observe, and navHeight is used to set the rootMargin option for the Intersection Observer. The rootMargin option defines a margin around the viewport that is used to trigger the intersection observer when the observed element is within this margin.

Next, we define the obsCallback function. This function is called every time the observed element intersects with another element or the viewport. The entries parameter contains an array of intersection observer entries, which we can use to check if the observed element is intersecting or not. In our case, we only have one observed element, so we can use the first element in the entries array to check if it’s intersecting or not. If the observed element is not intersecting, we add the .sticky-nav class to our stickyNav element, which makes it sticky. If the observed element is intersecting, we remove the .sticky-nav class from our stickyNav element, which makes it non-sticky.

After defining the obsCallback function, we define the obsOption object. This object is used to set the options for the Intersection Observer. The root option defines the element that is used as the viewport. In our case, we want to use the entire viewport, so we set it to null. The threshold option defines the percentage of the observed element that needs to be visible before the intersection observer is triggered. In our case, we want to trigger the intersection observer as soon as the observed element enters the viewport, so we set it to 0. Finally, the rootMargin option defines the margin around the viewport that we want to use. We set it to -navHeight, which makes the intersection observer trigger when the observed element is navHeight pixels away from the top of the Navbar.

Jane James is a WordPress web developer based in Melbourne, Australia but also services clients from Sydney, Brisbane, Newcastle, Perth, Adelaide, Darwin and Hobart. Have a project in mind? Contact me here.

The post WordPress developer problems: How to Use an Intersection Observer to Create a Sticky Navigation Bar appeared first on Freelance Wordpress developer Melbourne | Sydney | Gold Coast | Brisbane |.

]]>