Adjust the height of the menu as you scroll

It would be great if my website's menu could automatically minimize its height when I scroll down. For instance, my header is fixed at the top with a height of 150px. However, when I scroll down, I would like it to resize to a height of 50px.

I am currently using bootstrap 3, which means that the class for my header is .navbar .navbar-fixed-top .

Does anyone know how I can achieve this effect?

Answer №1

Here is an easy jquery solution for this problem.

$(window).scroll(function (event) {
        var y = $(this).scrollTop(); //determine position from top to change style in pixels
        if (y >= 300) {
            $('#header').addClass('resized');
        } else {
            $('#header').removeClass('resized');
        }
    });

CSS:

.resized {
    height:50px !important;
}

JSFiddle

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

Freshly updated, discover how to create an underlined navbar menu item

I'm trying to underline the currently selected menu item in a navbar, but I'm having trouble getting it to work. Here's my code: .navbar-nav .nav-item:focus .nav-link { text-decoration: underline; background-color: yellow; } It&apo ...

Tips for creating a mobile-friendly website with Twitter Bootstrap

Having an issue with my site on mobile devices while using Twitter Bootstrap. The header is not displaying correctly when I resize the window, it appears like this: It should look consistent across all devices as shown below: Below is my HTML code: ...

Updating AngularJS scope does not occur when a value is selected using jQuery

When writing a test using cucumber and selenium, I encountered an issue with a dropdown selection. After selecting a value using jQuery, pressing next resulted in an error. The problem seems to be that Angular has not updated its scope properly. <sele ...

Decreasing the gap between form controls such as textboxes and labels with Bootstrap

I am currently working on a .NET (C#) MVC web application and I have implemented a Bootstrap dropdown menu in my project. My main objectives are as follows: 1) I aim to minimize the space between a label and its corresponding text-box. Initially, I at ...

Tips for cycling through an associative array using integer indices?

No matter where I look, it seems like finding an answer to this question is impossible I've got an array array=["id1" : "somedata", "id2" : "somedata2",....] So I can access it using a special database ID, but what if I want to iterate through this ...

Is full support for CSS 3D transforms (preserve-3d) provided by IE11?

Creating a web app utilizing css 3d transforms, my goal is to have IE11 support if feasible. I am aware that IE10 does not endorse the preserve-3d value for the transform-style css attribute, however, there seems to be conflicting information regarding I ...

Develop a versatile JavaScript or jQuery script that automatically sets the focus on the first input field within a div or tag when the user clicks on it

I am currently working on creating a data entry form using a table layout. The form has two columns - the first column for input titles and the second column mostly for input tags. I styled the inputs in the second column to appear transparent with no bord ...

Tips for vertically aligning <p> and <div> elements without using the table-cell property

I am trying to align my text vertically in the middle using a ch-grid <div> (the circle color element). Is it possible to achieve this without specifying a display attribute for the <p> element? I plan to hide it by setting display:none initia ...

Utilize AJAX to dynamically load content within dynamic Bootstrap tabs for enhanced website functionality

I've been struggling to implement this functionality without success. My goal is to retrieve the name/id of the current bootstrap 4 tab using jQuery, pass it into a PHP variable, and then utilize it in a MySQL WHERE clause to determine which data shou ...

Is there a never-ending loop caused by a jquery/jqtouch getJSON call?

I'm currently working on my first JQTouch application. I've encountered an issue where, upon transitioning from #login to #home, a JSON ajax call is triggered successfully but it seems that the pageAnimationEnded event gets stuck in an infinite l ...

Obtain the value of an AngularJS $scope variable using basic JavaScript code

This isn't a duplicate of the question regarding accessing scope from outside a JavaScript function in AngularJS, as I'm specifically looking to avoid accessing the variable on any click event. Within my Maincontroller.js file, I have AngularJS ...

Finding the nearest time in an array using Javascript or jQuery

I've been attempting to retrieve the closest time from an array or list, but so far I haven't had any success with the code I found. I made some edits to it, but it didn't work as expected. I'm open to using jQuery if it would simplify ...

adjusting the size and positioning of an image within a parent div using React

Hey everyone, I'm a newcomer to the world of React. Currently, I'm working on a website and everything seems to be falling into place nicely. However, I've encountered a little hiccup. My goal is to write code within my SCSS folder that will ...

Having trouble parsing JSON data from $_POST while utilizing AngularJS

While working in Angular, I encountered the following scenario: var data = {}; data['name'] = "test"; data['class'] = "testClass"; $http.post('http://testURL',data,function(data){ console.log(data); }); Upon inspecting the ...

"Enhance the Visualization: Adding Spacing Between Bars in AngularJS ChartJS

I have implemented Angularjs Chartjs by referring to the documentation available at , Here is the JavaScript code snippet: $scope.options = { cornerRadius: 20, scales: { yAxes: [{ gridLines: { display: f ...

Retrieve complete browsing history using asp.net

Similar Question: How can browser history be accessed? I am interested in obtaining a list of websites visited by the client through their browser history in order to gather information from these sites and display it on my website. How can I access t ...

Exploring Various Alignments within Tailwind CSS

I am facing a layout challenge with three divs: a navigation bar, a webgl player, and a panel. I want the nav bar to be positioned at the top of the screen, the panel aligned to the right-hand side, and the webgl player centered both vertically and horizon ...

arranging <li> elements in alphabetical order

I'm looking for a way to alphabetically sort an unordered list while still keeping the outer html intact. My current method sorts the list alphabetically, but it only reorganizes the inner html of the list elements and not the entire element itself. T ...

What are the possible complications that could arise from implementing this system for designing web pages?

Feeling frustrated with the limitations and compatibility issues of CSS, I decided to create a new approach for structuring webpages. Instead of relying on CSS, I developed a javascript library that reads layout instructions from XML files and uses absolut ...

Adjusting the CSS for a specific element while hovering over another

I'm currently facing the challenge of creating a fixed position navbar with white links that should change to black when hovering over another element with a white background. Here is the CSS for the navbar links: nav ul li a {color:#ffffff;} CSS f ...