Subnav sticks when scrolling down but becomes unresponsive on resizing

My webpage has a main header that sticks to the top of the window and a subnav fixed at the bottom. The hero image adjusts its height based on the window minus the header and subnav heights. When scrolling past the subnav, it sticks just below the main navigation. Everything functions well except for one bug.

To see how it currently works, visit this link: https://jsfiddle.net/owgvjxdj.

The issue arises when resizing the window; the subnav's position doesn't adjust accordingly and can end up too high or low. To fix this, I attempted to refactor the subnav's measurements by adding a window resize event:

// Update subnav position on window resize
$( window ).resize(function() {

  var windowH = $(window).height();
  var stickToBot = windowH - $('#subnav').outerHeight(true);

  var scrollVal = $(this).scrollTop();
  if ( scrollVal > stickToBot - $('.navbar').outerHeight(true) ) {
      $('#subnav').css({'position':'fixed','top' :'80px'});
  } else {
      $('#subnav').css({'position':'absolute','top': stickToBot +'px'});
  }

});

Although this solution works initially, after scrolling and resizing the window, the positioning becomes inaccurate. View the issue here: https://jsfiddle.net/owgvjxdj/1/

I sense I'm overlooking something evident here, but what could it be?!

Answer №1

To ensure proper functionality, it is crucial to constantly update the windowH and eventually the sticktoBot. By making these variables global and adjusting their values during window resizing, you can achieve the desired outcome. For a demonstration, refer to this fiddle - http://jsfiddle.net/owgvjxdj/4/

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

Creating a dynamic scene with three.js within an HTML div element

I've been searching for solutions but can't seem to figure it out for this particular scenario. https://codepen.io/rkimo/pen/vdwxJj Essentially, I just want to include this blob in a div so that I can add content before and after it and be able ...

Tips for adding an additional div inside a navigation container, evenly spacing objects, and aligning the search bar to the right

I'm currently working on designing a simple navigation bar but I'm facing difficulties in aligning the elements correctly. See my progress here: https://jsfiddle.net/zigzag/bL1jxfax/ Here are my objectives: 1) Ensure that the navigation bar rea ...

Challenges of implementing an infinite scroll feature using PHP, jQuery, MySQL, and AJAX

Hey there! I'm in the process of developing this website: It's almost complete, but I'm facing a challenge with the "Coleccion" section. In this section, users select a specific type of bag, and only 20 bags are initially loaded from a MySQ ...

Tips for creating responsive content within an iframe

I have inserted a player from a website that streams a channel using an iframe. While I have managed to make the iframe responsive, the video player inside the iframe does not adapt to changes in viewport size. Despite trying various solutions found online ...

Using Ajax to create a loading animation

I want to display an ajax-loader.gif while my data is loading, and then hide it once the data has been completely loaded. Here is the code snippet for update: $.ajax({ type: "POST", dataType: 'json', url: "api/Employee/GetData", beforeSend: fu ...

Issue with JQuery's click and submit events not triggering, but change event is working fine

My webpage has a dynamic DOM that includes add and save buttons to interact with elements. Each row also has a remove option for deletion. When a user logs in, the controller redirects them to their homepage in the codeigniter PHP framework. This controlle ...

jQuery Ajax functionality is functional in Internet Explorer, but experiences major issues in Firefox

When running this block of Javascript code, it always returns null in Firefox and Chrome, but it works perfectly fine in Internet Explorer: $.ajax({ url: "http://example.com/webservice.asmx/myfunction", data: "{ 'q': 'hotels&apos ...

Exploring ways to showcase informational alerts when a link is hovered over by the mouse

I am currently working on a website that showcases links utilized by my team. One specific requirement is that when a user hovers over a link, note information should be displayed. Although the simplest solution would be to not list the link if it's n ...

Save a JSON object from a URL into a JavaScript variable using jQuery

I am trying to figure out how to use the .getJSON() function to store a JSON object as a JavaScript variable. Can someone guide me through this process? var js = $.getJSON(url, function(data) {...} ); Is it necessary to include the function(data) { ...

Numerous pop-up windows displaying a variety of distinct content in each one

While working on a website, I came across a helpful jQuery pop-up function that I found on Stack Overflow. The original post can be found here. I am trying to customize each pop-up box so they contain different content from the .pop1 and .pop2 divs (parag ...

CSS translation animation fails to execute successfully if the parent element is visible

Inquiries similar to this and this have been explored, but do not provide a solution for this particular scenario. The objective is to smoothly slide a menu onto the screen using CSS translation when its parent is displayed. However, the current issue is ...

Horizontal compression applied to background image

After following a suggestion ("css only technique number 1" found here http://css-tricks.com/perfect-full-page-background-image/), I experimented with using an inline img element and CSS to create a background image that would occupy the entire browser win ...

Arrange the child divs on top of the parent div

<div id="1"> <div id="2"> </div> </div> It is proving challenging to position div2 above div1 in the vertical dimension. Applying a negative top position does not have the desired effect, as it appears that the top border of the ...

The colors of a jVector Map change based on the display of two datasets

I have two sets of data, 'ctrl.gdpData' and 'ctrl.pdpData', which I am currently displaying on the jVector world map as shown below: $('#world-map-gdp').vectorMap({ map: 'world_mill', series: { ...

Firefox is unable to render the jQuery file from ajax.googleapis.com

I have Mozilla Firefox version 20.0.1 installed, and I am using the following code on my website: <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <!--<script s ...

Alter the value of a parameter within a script tag with JavaScript/jQuery

Looking to dynamically change a parameter value using JavaScript or jQuery. Here is an example URL: www.exampleurl.com/somepage?foo=test I have a function that can extract the value after the 'foo' parameter: function getParameterByName(name, ...

When using JQuery's .each method, only the first two elements from an array of automatically generated elements are retrieved

I have a loop that creates multiple divs with the class panel. @for(comment <- event.getCommentsSorted()) { However, when I try to manipulate each of these divs using jQuery's .each, only the first two divs are selected. $(window).on('load& ...

The styling of the Material UI autocomplete listbox does not affect its appearance

I am working on an autocomplete feature that groups items by titles. I am trying to adjust the height of the first group to be different from the rest, but I am facing some challenges since there is no unique identifier for the ul component. To work around ...

Is it possible to caution users before refreshing the page, while still allowing them to

I have a script that displays a warning message to the user when they attempt to refresh the page. It works perfectly for that purpose. However, my issue is that this same alert pops up when a user clicks on a button or a link within the website itself. I ...

Attempting to use tabs within a tab does not function correctly

Visit the CodePen link here This is the codepen example. <div class="row"> <div class="col s12"> <ul class="tabs2"> <li class="tab col s3"><a href="#test1">Test 1</a></li> <li class=" ...