Tips on displaying only the main menu when it's a sticky menu

I've been researching how to create a sticky menu and have gone through numerous tutorials. I've also looked at related questions here but haven't found a solution that works for me without using a plugin. Is it possible to achieve this without a plugin?

I tried implementing the code from one of the tutorials, but the menu still doesn't stick.

Here are the code snippets I added to my original files:

HTML file:

<!--sticky menu-->
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<!--ends here-->

CSS:

.fixed{
    position:fixed;
    top:0;
    left: 0;
    width: 100%;
    background-color: transparent;

}

nav{
    z-index: 500;
    background-color: #ffffff;
}

.nav-placeholder{
    margin: 0 0 40px 0;
}

.fixed .nav-inner{
    padding: 0 20px;
}

.fixed .nav-inner-most{
    max-width:700px;
    margin: 0 auto;
    background-color: #ffffff;
}

The JS script:

jQuery(document).ready(function() {
    
    // function to run on page load and window resize
    function stickyUtility() {
        
        // only update navOffset if it is not currently using fixed position
        
    } // end stickyUtility function

 });
 

Summary: http://jsfiddle.net/misaki03/9gtk9x7r/1/

If anyone can provide guidance, it would be greatly appreciated.

I made some edits because I came across this code:

<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>

In the tutorial I was following, I noticed this line of code that seemed confusing. (Link provided due to length)

Although I'm not sure what it does, it did make the menu sticky. However, the sub-menu appears as I scroll down. Is there a way to fix this?

Answer №1

If you're searching for a solution similar to this, check out the following Codepen link: http://codepen.io/noobskie/pen/avqVVd?editors=010

It appears that there were some issues with the z-index and script errors affecting the functionality.

Here is an optimized version of stickymenu:

function stickIt() {

  var orgElementPos = $('.original').offset();
  orgElementTop = orgElementPos.top;               

  if ($(window).scrollTop() >= (orgElementTop)) {
    // Once scrolled past the original position, display the cloned sticky element.

    orgElement = $('.original');
    coordsOrgElement = orgElement.offset();
    leftOrgElement = coordsOrgElement.left;  
    widthOrgElement = orgElement.css('width');
    $('.cloned').css('left',leftOrgElement+'px').css('top',0).css('width',widthOrgElement).show();
    $('.original').css('visibility','hidden');
  } else {
    // If not scrolled past the menu, show only the original menu.
    $('.clloned').hide();
    $('.original').css('visibility','visible');
  }
}

Answer №2

Successfully resolving the issue with Amit's assistance after exploring alternate sources. Upon discovering a related question, I decided to experiment with the provided code and made some adjustments.

For reference, see the link below:

sticky navigation with submenu and smooth scroll

By implementing the codes suggested by Amit in the shared jfiddle, I managed to resolve the problem. Appreciation to @Amit and the original poster of the question for their contributions.

Answer №3

There's a new concept in CSS called "sticky", which acts like position: static within its parent until it reaches a specified offset threshold, at which point it behaves like position: fixed.

.menu {
    margin: 0;
    padding: 0;
    width: 100%;
    background-color: #bffff3;
    position: sticky;
 }

If you want to learn more about this feature, check out the following link: http://www.sitepoint.com/css-position-sticky-introduction-polyfills/. It might provide some helpful information. Best of luck!

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

I am currently facing an issue in choosing the appropriate year using Python, Selenium, and VSCode

Having trouble selecting the year in my code. Please run the code to see what happens at the end. I've attempted various methods. Unable to find a solution. # -*- coding: utf-8 -*- from time import time import time from selenium import webdr ...

Generating an HTML table that adjusts its size based on the paper dimensions chosen by the user

I am facing a challenge with my html table and print option on a webpage. Currently, users can easily print the table on an A4 page by default using the JavaScript print function. The table fits perfectly on the full size of the A4 page. However, I now h ...

Adjust the size of the child div to fit the remaining space within the parent div

I'm dealing with a situation where there are two child divs inside a parent div. The first child div takes up 32% of the width, while the second child div takes up 68% of the width. If I were to set the display of the first child div to "none", how ca ...

Can someone help me locate the file using the inspect element feature?

Today, I encountered a small issue on my website that I wanted to fix. Although I was able to make the necessary changes using Inspect Element, I couldn't locate the file where it needed to be changed. The website in question is gesher-jds.org/giving. ...

Enter the values of the array into the designated inputs based on their class, one by one

Is there a way to assign each element of an array of strings to individual HTML fields based on their index? const pid = [ "688", "500", "450" ]; <input type="text" class="field_pid"> <input typ ...

Error: Grunt Watch encountered a fatal error with the message "listen EACCES" and liver

PC Specs: Windows 7 Enterprise x64 Currently working on a project using grunt.js and encountered an error when trying to run 'grunt watch'. Yesterday, everything was running smoothly with Grunt, but today I started getting this message: Runni ...

Discovering all class names following the same naming convention and storing them in an array through Javascript

Hey everyone, I could use some assistance with a coding challenge. I'm aiming to extract all class names from the DOM that share a common naming convention and store them in an array. For instance: <div class="userName_342">John</div> & ...

In JavaScript, the act of shuffling an array will produce consistent results

I have created a random array generator by shuffling an initial array multiple times. SHUFFLE FUNCTION const shuffle =(array) => { let currentIndex = array.length, temporaryValue, randomIndex; while (0 !== currentIndex) { randomIndex = Ma ...

Employing a function to concatenate promises

In my coding process, I have come across a situation where I need to fetch content and then save it using two separate functions. Each function performs a different task based on the type of content provided. These functions act as helper functions in my o ...

PHP is returning an empty response during an AJAX request

I am facing an issue with my AJAX request where I am trying to return a simple echo, but for some reason, it's not working this time. Even after stripping down the code to its bare essentials, the response is still blank. Javascript function getUs ...

Steps for embedding a font in a .pptx file

While working on creating a .pptx file using ASPOSE.Slides, I encountered some issues with embedding fonts. As an alternative option, I am looking for suggestions on how to embed custom fonts in a .pptx file using Apache POI or other methods. If you have ...

Transforming the image data from Axios into a format compatible for Vue to render in an <img> element

Seeking assistance from the knowledgeable community at stack overflow. I'll provide a brief summary of the situation below: I'm sending a request from a Vue page to a Node.js server using the code snippet shown The Node.js server is communicatin ...

All outcomes being displayed from Youtube's json feed

Currently, I am retrieving a youtube playlist by using the following link: I'm curious if there is a method to display all 250 videos from my feed instead of just the 25 that are currently being shown. Any assistance on this matter would be highly a ...

Retrieve the value of a cell in Google Sheets

Seeking assistance on how to retrieve the content of a cell in Google Sheets and store it for comparison purposes at a later time. My search efforts have led me to various online resources such as the Visualization API and the Sheets Script, but I have n ...

Having trouble rendering SVG image using ReactJS

Trying my hand at learning React JS through a tutorial, I created a reusable component to display information. However, I encountered an issue where my SVG image is not displaying properly. Take a look: https://i.sstatic.net/dhK4D.png Notice how the imag ...

The command 'var' is not recognized as an internal or external command within npm

I need assistance with installing the histogramjs package. The command npm i histogramjs successfully installs it, but when I attempt to run the code using var hist = require('histogramjs'), I encounter an error in the command prompt: 'var& ...

Encountering a 'require is not defined' issue while trying to implement chartjs within a JavaScript script

I am new to using Node.js and JavaScript. Currently, I am working on integrating chartjs into my project. Below are the HTML and JavaScript files associated with my project: charts.html: <!DOCTYPE html> <html lang="en" dir="ltr"> <hea ...

Utilizing Multiple Select Options and CSS Float Styling in Mozilla Firefox

I have been attempting to customize the CSS of a multiple select element. While I am satisfied with how it looks on Chrome, I am facing issues getting it to work correctly on Mozilla Firefox. $('option').mousedown(function(e) { ...

Error message: JavaScript JSON variable undefined in AWS Lambda

My lambda function is being triggered by an IoT rule that sends a MQTT message in JSON format. I am facing an issue where the top level fields are logging correctly, but when it comes to nested objects in the JSON, they appear as "undefined". Even after tr ...

Get both div tags within a single find_all function call in Beautiful Soup

Is there a way to extract multiple HTML div tags using a single "soup.find_all" function in beautifulSoup? The divs are labeled as "event odd" and "event even" on the webpage, and I want to iterate through all of them. Here is an example of the webpage co ...