Sticky position problem in the navigation bar

 window.onscroll= function() {myFunction()};

  var navbar = document.getElementById("navbar");
  var sticky = navbar.offsetTop;
  
  function myFunction() {
    if (window.pageYOffset >= sticky) {
      navbar.classList.add("sticky")
    } else {
      navbar.classList.remove("sticky");
    }
  }
<header id="home">
  <nav id="navbar">
    <div class="row flex">
      <a href="#"><img src="img/logo.png" class="logo" alt="logo"></a>
      <ul>
        <li><a href="#home" class="active">home</a></li>
        <li><a href="#service">service</a></li>
        <li><a href="#team">team</a></li>
        <li><a href="#skill">skill</a></li>
        <li><a href="#portfolio">portfolio</a></li>
        <li><a href="#review">review</a></li>
        <li><a href="#contact">contact</a></li>
      </ul>
    </div>
  </nav>

  <div class="row">
    <div class="hero-text-box">
      <h1>Hi there! We are the new kids on the block and we build awesome websites and mobile apps.</h1>
      <a href="#" class="btn btn-hero">work with us!</a>
    </div>
  </div>
</header>
As a newcomer to web development, I am currently practicing projects from tutorials on YouTube after learning CSS and HTML. Unfortunately, I encountered an issue with a jQuery plugin that is not functioning correctly. Despite searching for solutions on Google and watching videos, I have not found any helpful resources. Since I am a beginner in JavaScript, I am limited in what I can do with the JS part of the code. Presently, I have implemented JavaScript code from W3Schools, but it only partially meets my requirements. Specifically, I am facing difficulties with the navigation bar. My goal is to have the navbar blend seamlessly with the homepage and become sticky when scrolled to a certain point, similar to other sections on the website.

Answer №1

If you want to make a navbar sticky at a specific position, you need to use the CSS property "position: sticky". This works with certain positions where you want the navbar to stick. For example:

position: sticky;
top: 3rem;

To update your CSS code from:

.sticky { 
  position: fixed; 
  top: 0; 
 }

to

.sticky { 
  position: sticky; 
  top: 0; 
 }

For more information, you can refer to this link: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_sticky_element

Make sure to specify the exact position where you want the bar to stick while scrolling. You can try the following code snippet:

<!DOCTYPE HTML>
<html>

<style>
.sticky{
position: sticky;
top: 0;
}
</style>
<header id="home1">
  <nav id="navbar" class="sticky">
    <div class="row flex">
      <a href="#"><img src="img/logo.png" class="logo" alt="logo"></a>
      <ul>
        <li><a href="#home" class="active">home</a></li>
        <li><a href="#service">service</a></li>
        <li><a href="#team">team</a></li>
        <li><a href="#skill">skill</a></li>
        <li><a href="#portfolio">portfolio</a></li>
        <li><a href="#review">review</a></li>
        <li><a href="#contact">contact</a></li>
      </ul>
    </div>
  </nav>

  <div class="row">
    <div class="hero-text-box">
      <h1>Hi there! We are the new kids on the block and we build awesome websites and mobile apps.</h1>
      <a href="#" class="btn btn-hero">work with us!</a>
    </div>
  </div>
</header>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

Initiating a quick route refresh - Redirecting the traffic

Is it possible to change the URL address while processing a request in Express? I am looking for a simple redirect implementation that allows me to modify the current URL and restart the route matching process. Here's an example of what I'd like ...

Tips for preventing horizontal list items from stacking on top of each other in React

After successfully creating a horizontal list using HTML and CSS, I encountered an issue when trying to replicate the same code in my React application. In React, the list elements were stacking on top of each other due to a styling conflict with the style ...

Guide to exporting everything within a div as a PDF using Angular 2

When attempting to convert a div with the id "div1" into a PDF using JSPdf in Angular 2, everything seems to be working fine until I try to export it to PDF. Here is my code snippet: <div class="container" id="div1"> <table> <tr> & ...

What is the best way to handle a 3-element mode using an onClick function in Next.js?

Creating an accordion menu in Next.js comes with the challenge of implementing 3 different modes for an element: The first mode is default, where no click event has occurred: .mainLi_default In the second mode, the first click event triggers the opening o ...

How come my div isn't updating its color in Internet Explorer?

Having trouble with the following scenario in IE. Styles: .tabmenu { background-color: #990000; display: inline-block; width: 150px; position: relative; left: 59%; border-top-left-radius: 10em; margin-bottom: -8px; min-wi ...

A Bootstrap carousel displaying images in a confused manner with another carousel on the same webpage

I recently encountered an issue with adding two Bootstrap 5 carousels on the same page. Even after assigning unique IDs to each carousel, the second carousel seems to mix photos with the first one, duplicating two photos from the previous slide. I tried ...

Attempting to locate an element using Selenium IDE proves to be challenging unless each command is executed individually

Currently, I am utilizing selenium ide for automating my tests. Once I click on a link, a popup window appears with a div containing text. Strangely, I am unable to retrieve the text within the div tag without either double-clicking on it or executing the ...

Preserve the scroll location while adding new content to a list in AngularJS

I have been attempting to add new items to a list within a scrollable container using ng-repeat, with the most recent item appearing at the top. It is important that I am able to preserve the scroll position if the scrollbar is not at the very top when add ...

What's the deal with inline blocks and text in the middle?

Check out this code I created for a navigation bar. HTML : <div class="header-nav"> <div class="header"> <img src="../Pictures/LifeFrame/2.jpg" width="100%" height="100px" /> </div> <div class="nav" align="c ...

The dropdown UL element loses its opacity in Internet Explorer

After creating a basic CSS dropdown menu, I encountered a strange issue when viewing it in IE. In the screenshot provided, only the shadow is visible and the content inside the element appears to be transparent. I have not utilized transparency anywhere o ...

Guidelines for aligning a form in the middle of the screen (positioned between the navigation bar and footer

Before asking this question, I made sure it's not a duplicate by researching previous similar issues with no success. I'm currently utilizing Bootstrap 4. You can view my app at (I'm unable to post the CSS and HTML of my React app due to S ...

What is the best method to extract values from an array?

In my array, I am storing multiple values together such as: var locations = [ 'Bhopal','Mobile',new google.maps.LatLng(18.40,78.81),'images/mobile.png', 'Bangalore','Television',new google.maps.Lat ...

What is the top choice instead of using the jQuery toggle() method?

After jQuery deprecated the toggle() method, I began searching for alternative ways to toggle classes on Stack Overflow. I came across various other methods to accomplish the same task (Alternative to jQuery's .toggle() method that supports eventData? ...

The issue of React hover style malfunctioning in conjunction with Radium and Material-UI

I am currently utilizing the Radium library for inline styling in my React project. I have encountered an issue where the styling does not apply properly to Material-UI components, specifically when hovering over a Paper element. The color should change ...

Is it possible to utilize JSX when developing an App using React CDN or the CRA (create-react-app) boilerplate template?

The HTML Code: <div id="app"></div> <script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@latest/umd/react-dom.develo ...

Update the position of a div when an element becomes visible

Currently, I have 3 titles each with a button underneath to reveal more information about the subject. To toggle the visibility of the content, I have implemented a script that shows or hides the corresponding div when the button is clicked. On smaller de ...

By default, populate the text area in an HTML form with content fetched from the database

Can anyone provide suggestions on the best method to extract text from a database field and place it as the default text in a text area? I am thinking of assigning the field to a variable and then utilizing javascript to set it as the default text, but I ...

Utilizing the $_SESSION variable for passing a PHP variable between pages?

My goal is to create an app that can receive and tally votes. I am currently facing an issue where the voter's selection does not carry over to a confirmation page, resulting in an "undefined variable" error. In my attempt to solve this, I utilized $ ...

Exploring Angular2: Understanding how to retrieve an object with an unknown key

I am trying to showcase a specific value from different objects within a template. The path of the desired value depends on the object being referenced. For example: let obj = { "a": "a", "b": { "1": "1", "2": "READ ME" } } let ...