Experiencing difficulty in enhancing the flexibility of the top navigation bar while scrolling on the page

I am currently developing an angular-based website and I am looking to implement a top navigation bar similar to the one on

My goal is to have the navigation bar's width reduce when scrolling down the page. I have tried using ng-bootstrap and material design, but so far I have not been successful in achieving this effect.

Does anyone know how I can make the top navigation bar more flexible, like the one found on , where it adjusts as you scroll through the page?

Answer №1

To achieve this effect, I utilize jQuery to dynamically add a class when the window has been scrolled a certain distance.

Here is the JavaScript code that adds the 'scrolled' class after scrolling 100px and removes it if less than 100:

$(window).scroll(function() {
 if ($(window).scrollTop() > 100) {
  $('.menu').addClass('scrolled')
 } else {
  $('.menu').removeClass('scrolled')
 }
});

You can then style the behavior using CSS:

.menu {
 position: fixed;
 top: 0;
 left: 0;
 width: 100%;
 height: 100px;
 background-color: red;
 transition: height 500ms;  
}

.menu.scrolled {
 height: 50px;
}

If you want to see it in action, check out this Fiddle.

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

The sticky navigation bar's background fails to change at the appropriate time following an image slider

I am currently facing an issue with my sticky navigation bar. I want the background color to change to white after the user scrolls past the image slider. The problem is that the white background appears above the image slider instead of in the orange logo ...

Is there a CSS equivalent of column-gap for rows?

After following this guide, I attempted to recreate the photo grid with some spacing between the images. Changing the column-gap property to 3px did add horizontal space, but is there a way to include vertical gaps as well? Any suggestions or solutions wou ...

Center the text in links at the bottom of the screen using Flexbox

It feels too early on a Monday morning for this. I have successfully used flexbox to align the overall page, but I am facing trouble aligning the individual link items. Specifically, I want to align the text of the links to the bottom. I tried using verti ...

Image failing to align properly within div container

When attempting to align an image in the middle vertically inside a DIV on my website, it does not work properly. This issue does not occur when I do it on jsFiddle. It appears there may be a CSS conflict that I am unable to identify. I have applied the s ...

Guide to applying a linear-gradient and background image to a particular div using React's styling techniques

How can I set both a linear-gradient and a background image on a specific div using styles in Reactjs? I am struggling to achieve this, as I can only get either the image or the linear-gradient but not both simultaneously. The image ends up overlapping w ...

Proper application of this - encountering issues with property emit of undefined in Angular

I am working with an angular component and encountering the following code: @Output() fixPercentChanged = new EventEmitter<number>(); In addition, I have this event: fixChanged(e) { setTimeout(() => { let fix = e.component.option(&apo ...

Angular 2: Issue with disabled functionality not functioning correctly

In my TypeScript code, I have created a boolean variable called readOnlyMode. When this variable is set to true, all elements should be disabled. To achieve this, I am using [disabled]="readOnlyMode" in the HTML for elements that need to be disabled. Howev ...

Connect Angular Material by chaining together md-select elements from arrays and array of form inputs

I am encountering a challenge with combining chains in Angular Material. I aim to transition from this linked solution on jsfiddle to using md-select and md-option in Material. How should it function? It's quite simple. Here's the scenario: Se ...

Error message: The ofType method from Angular Redux was not found

Recently, I came across an old tutorial on Redux-Firebase-Angular Authentication. In the tutorial, there is a confusing function that caught my attention: The code snippet in question involves importing Actions from @ngrx/effects and other dependencies to ...

The alignment of the image on a mobile device appears to be off

I've been trying to center an image on my website, and it looks perfect when viewed on a desktop computer. However, when I load the page on a mobile phone browser, it appears a bit off. This is the CSS code I'm using to center the image: #logo ...

Is there a way to apply a setTimeout to a <div> element upon the first click, but not on subsequent clicks?

Check out the fiddle I've created: http://jsfiddle.net/dkarasinski/dj2nqy9c/1/ This is the basic code. I need assistance with a functionality where clicking on a black box opens a black background and then after 500ms a red box appears. I want the ...

Solved the issue of inconsistent element height and jumping on mobile devices during scrolling

Problem persists with the fixed element at the bottom of my mobile device when I scroll. It seems that the height is recalculated each time due to an increase in document height on mobile devices. The issue appears to be related to the address bar fading ...

The images on iPad2's Safari browser are appearing blurry due to low-quality

I'm encountering some challenges trying to display a large jpg file properly on an iPad 2. The image is 9577x600 pixels and is being used as a background image on a div. I have specified -webkit-background-size on that div and set it to the same dimen ...

Difficulty in making Materialize.css column match the height of the entire row

Utilizing React.js, Materialize.css, and certain MaterialUI components in collaboration to build a website. Developed a header (React component, basic div with title and logout button) for the site using a Materialize.css "grid". Issue: The react compone ...

angular - create a new variable once filtering of an array has been finished

When working with an Angular component, I am attempting to display a progress spinner while data is loading. When the user clicks on an HTML button, a function below is executed. The time it takes for the array filter to complete will vary based on the siz ...

What causes the checkboxes to shift positions when selected in Safari?

I have encountered an issue with a set of values and checkboxes that are pre-checked on this webpage. Everything seems to be working smoothly in all browsers except for Safari. Whenever I try to click on any of the checkboxes, they seem to 'jump' ...

Using Javascript to Conceal Button for Unauthenticated Users

Our website is currently running on an outdated e-commerce CMS platform, which limits my options due to my beginner level skills in JavaScript and jQuery. One specific issue we are facing is the need to hide Prices and Add to Cart buttons for users who ar ...

Maintaining Image Aspect Ratio with CSS Max-width Property

I'm currently facing an issue where I am using max-width to resize images, but they are not scaling proportionally. Is there a way to achieve this with JavaScript/jQuery? Additionally, is it possible to do this without initially knowing the image&apos ...

I am having trouble getting the JavaScript confirm function to function properly

Encountering a peculiar issue with the javascript confirm method - every time I call it, an error is thrown stating "Uncaught TypeError: Property 'confirm' of object [object Object] is not a function" var confirmVal = window.confirm("Please con ...

Mastering the art of error handling is essential in Angular2, much like using try-catch blocks in C

Can someone help me with posting all types of runtime errors to the server? I've been searching for a solution, but haven't found what I need. Below is my code: ngOnInit() { try{ this.CallGetBreakingNews( ...