When the max-width changes, the CSS transition on the left side snaps into place

There is a simple '.full-width' class that is being added and removed from a <nav> element when the user scrolls past a banner. The intention is to gradually expand or shrink the <nav> based on the user's scrolling position in relation to the banner. The CSS provided includes a transition property for the nav element.

However, there seems to be an issue where the <nav> snaps back on the left side when it expands. The expansion and shrinking transitions work correctly on the right side but encounter snapping on the left side.

Interestingly, this functionality works fine with Bootstrap 3, but there appears to be a problem with Bootstrap 4 causing the navbar to snap unexpectedly.

If anyone can spot something I might be overlooking or have any insights into why this behavior is occurring, your input would be greatly appreciated.

var NAVBAR_HEIGHT_IN_PX = 50;

$(document).ready(function() {
  var bottomOfBannerContainer =
      $('.banner-container').offset().top +
      $('.banner-container').outerHeight(true) -
      NAVBAR_HEIGHT_IN_PX;

  $(window).scroll(function() {
    if ($(document).scrollTop() >= bottomOfBannerContainer) {
      $('#navbar').addClass('container');
      $('#navbar').removeClass('full-width');
    } else {
      $('#navbar').addClass('full-width');
      $('#navbar').removeClass('container');
    }
  });
});
#navbar {
  transition: all 0.8s ease;
}

.banner-container {
  width: 100%;
  min-height: 200px !important;
  background: blue;
}

.full-width {
  max-width: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav id="navbar" class="navbar navbar-fixed-top navbar-dark bg-inverse full-width">
  <a class="navbar-brand" href="#">Hello S/O!</a>
</nav>

<div id="content" class="container">
  <div class="text-center col-xs-12 banner-container">workplz</div>

<p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p><br><p>Content!</p...

</div>

Answer №1

If you want to enhance the navbar, consider wrapping it in the additional container class when scrolling, rather than directly adding it to the navbar.

var NAVBAR_HEIGHT_IN_PX = 50;

$(document).ready(function() {
  var bottomOfBannerContainer =
    $('.banner-container').offset().top +
    $('.banner-container').outerHeight(true) -
    NAVBAR_HEIGHT_IN_PX;

  $(window).scroll(function() {
    if ($(document).scrollTop() >= bottomOfBannerContainer) {
      $('#navbar').addClass('container');
    } else {
      $('#navbar').removeClass('container');
    }
  });
});
#navbar {
  -webkit-transition: all 0.8s ease;
  transition: all 0.8s ease;
}
.banner-container {
  min-height: 150px;
  background: url(http://backbonejs.org/docs/images/backbone.png) center center no-repeat;
  background-size: contain;
  margin: 70px 30px 20px 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css" rel="stylesheet" />
<div id="navbar" class="navbar-fixed-top">
  <nav class="navbar navbar-dark bg-inverse navbar-static-top">
    <a class="navbar-brand" href="#">Hello S/O!</a>
  </nav>
</div>
<div class="banner-container"></div>
<div class="container">Lorem Ipsum text here</div>

Answer №2

After some investigation, I found that the snapping effect was linked to the sudden inclusion or removal of the max-width attribute. By just adding max-width to the .container-fluid class, all transitions ran smoothly with this jquery code snippet:

let bottomOfBannerContainer =
  $('.banner-container').offset().top +
  $('.banner-container').outerHeight(true) -
  NAVBAR_HEIGHT_IN_PX;

$(window).scroll(function() {
  if ($(document).scrollTop() >= bottomOfBannerContainer) {
    $('#navbar').addClass('container');
    $('#navbar').removeClass('container-fluid');
  } else {
    $('#navbar').addClass('container-fluid');
    $('#navbar').removeClass('container');
  }
});

In addition, a simple modification in the CSS further improved the transition:

.container-fluid {
  max-width: 100%;
}

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 Full Screen Image with HTML and CSS

On my current web project, I am aiming for an immediate impact with a full-screen image that transitions to reveal text and information when the user starts scrolling. The challenge lies in ensuring this effect is consistent across various screen resolutio ...

Transferring selected text to a Cordova application from a different application

Can I extract highlighted text from a different application, such as a browser, and send it to my Intel XDK Cordova app in JSON format for utilization? (Potentially utilizing a context menu) ...

Create a smooth scrolling effect for a div with a fixed position

Having trouble scrolling a fixed position div on the page? Are you using this jquery script: $('html, body').animate({ scrollTop: $(".example").offset().top }, 750); I had to set the body overflow property to hidden for another issue. The ...

What could be the reason for the absence of margin on the top and bottom

.separator { border: 1px solid #000000; margin: 10px; } <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>Separator</title> </head> <body> &l ...

What is the correct way to markup the semantic form label assistance text?

When designing a form, there are certain form elements that may need explanatory text to clarify their function. One approach is to wrap this text in a paragraph element, as demonstrated below: label p { margin-top: -1px; font-size: smaller; } <div ...

AngularJS allows for emitting the value from a search button based on the current state of a checkbox

Within my application, there is a Search Button that interacts with an internal API using a GET Request. This API retrieves Data that is sometimes protected. To simulate the Back-End behavior, there is a checkbox that is initially set to true. If the chec ...

`In Firefox, the footer refuses to remain anchored at the bottom of the page`

After using the following code, my footer stays at the bottom of my page: In my vue project, #app serves as the container for my content and footer. #app { min-height: 100%; position: relative; } .footer { position: absolute; bottom: 0; width: ...

Incomplete width allocation for the floating div positioned to the left

I've encountered an issue with the placement of the side-text div in relation to the image. Currently, without specifying the width of the side-text div, it automatically moves to the bottom. To address this problem, I attempted using display:inline- ...

Effortless sliding panel that appears on hover and vanishes when mouse is moved away

I am in the process of creating a menu for my website that utilizes linkbuttons which trigger additional linkbuttons to slide down upon hover. The desired effect is a smooth sliding panel that appears when hovering over the linkbutton, and disappears when ...

What could be preventing the background color from changing when attempting to use style.backgroundColor?

My objective is to store the current tab background color in a variable and then use that variable to change the body color. However, for some reason it is not working as expected. Can you help me figure out why? const tabName = 'someId'; var ...

Introducing LightZAP v2.54 (silent update2), the revolutionary Lightbox custom plugin that seamlessly launches when the page loads using the designated tag name (#img1)

As I have the LightZAP plugin installed on my website as an alternative to Lightbox, I am looking to trigger it (to display an image gallery with a specific image) when the page loads using the following link: index.html#img1, if possible. I have come acr ...

limitation in bootstrap ensures that only one collapse element is open at any

Hey, can someone help me fix this code? I'm trying to make it so that only one element opens at a time, but I can't seem to figure out how. Right now, if I click on two elements, they both open and the other one doesn't close. This is what ...

Adding a 'dot' to the progress bar in Bootstrap enhances its visual appeal

I am currently working on a progress bar and I would like it to look similar to this: https://i.sstatic.net/TSDEy.png However, my current output looks like this: https://i.sstatic.net/rQhYc.png I'm puzzled as to why the tooltip is floating there, ...

Troubleshooting issue with RadioButton check change not updating in onPost event in ASP.Net project utilizing Bootstrap HTML and C

Having created two radio buttons, grouped them together, and applied bootstrap classes for styling; I am encountering an issue where the values of the radio buttons are not updating when clicked and submitted. Interestingly, the checkboxes on the same form ...

What is preventing FancyBox from functioning properly?

Seeking assistance in implementing FancyBox for an e-commerce website I am developing for a client. Despite trying various tutorials and troubleshooting steps over the course of three days, I have been unable to get it to function properly. Not encounterin ...

If an element with a "hidden" display property is loaded in the browser window, will it be visible?

Is an element in a hidden display still using memory when the page is loaded? It's convenient to have many elements on a page, but if 99 elements are hidden and only 1 is displayed, does that impact the loading of the page? I'm curious if the pa ...

Move a CSS div with animation to a specific fixed position

Is there a way to modify this code so that the ball moves to the right and remains in that position? http://codepen.io/chriscoyier/pen/pBCax You can experiment with the live version of the output by clicking on the link above. body { padding: 30px; } # ...

Creating a responsive layout in HTML/CSS where the div element expands to fill the entire vertical space of its parent

Looking for assistance in creating a webpage layout like the one shown in this fiddle... http://jsfiddle.net/jeljeljel/5BSva/ I need the vertical line on the left side navigation tab to extend all the way down to the footer at the bottom of the page. Th ...

Move your cursor over the image to activate the effect, then hover over it again to make the effect disappear

Looking to enhance my images with hover effects. Currently, all the images are in grayscale and I'd like to change that so that when you hover over an image, it reverts to full color and remains that way until hovered over again. I've noticed so ...

Creating a tailored React component for a dynamic table with varying column and row sizes

I'm attempting to convert this into a React component for integration on my website: https://i.sstatic.net/MUwE3.png Figuring out the process of creating this, and if there are any libraries required is currently unknown to me. ...