Transitioning the height of a navigation bar with CSS

Is there a way to animate the navigation bar using CSS Transitions/Animations instead of JavaScript?

http://jsfiddle.net/v8fhn4qc/

$(function() {
  $('#header_nav').data('size', 'big');
});

$(window).scroll(function() {
  if ($(document).scrollTop() > 0) {
    if ($('#header_nav').data('size') == 'big') {
      $('#header_nav').data('size', 'small');
      $('#header_nav').stop().animate({
        height: '78px'
      }, 600);
      $("ul#menu-primary-menu").css("bottom", "35%");
    }
  } else {
    if ($('#header_nav').data('size') == 'small') {
      $('#header_nav').data('size', 'big');
      $('#header_nav').stop().animate({
        height: '100px'
      }, 600);
      $("ul#menu-primary-menu").css("bottom", "0");
    }
  }
});
#header_nav {
  background: #1588cb;
  width: 100%;
  height: 100px;
  position: fixed;
  z-index: 2;
  top: 0;
  left: 0;
}
body {
  height: 1000px
}
nav {
  height: 100px
}
nav ul {
  position: absolute;
  bottom: 0;
  margin: 0px;
  right: 0px;
  transition: 0.3s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="header_nav">

  <a id="cos_logo" href="#" title="">
    <img src="http://placehold.it/171/x30" alt="" width="171" height="30" class="no-scale" />
  </a>

  <nav class="primary menu">

    <div class="menu-primary-menu-container">

      <ul id="menu-primary-menu" class="menu">
        <li id="menu-item-44" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-44"><a href="/wordpress/">Home</a>
        </li>
      </ul>

    </div>

  </nav>

</div>

Answer №1

You have the option to incorporate CSS transitions and then dynamically add or remove a class.

CSS

#header_nav{
    height:100px;
    transition: height .600s ease;
}

#header_nav.scrolled{
    height:78px;
}

#header_nav.scrolled ul#menu-primary-menu{
    bottom:35%;
}

Javascript

$(window).scroll(function(){
   $('#header_nav').toggleClass('scrolled', $(document).scrollTop() > 0);
});

Answer №2

Affirmative.

nav{
   transition:height 0.5s;
   -webkit-transition:height 0.5s;
   -o-transition:height 0.5s;
   -ms-transition:height 0.5s;
} // The height of the navbar is currently being animated
.nav-small{
   height:60px; // The navbar is set to be small
}
.nav-normal{
   height:100px; // The navbar is set to be normal/big
}

Now use jQuery to change the class of the navbar element:

$('nav').addClass('nav-small'); // Make the navbar small
$('nav').removeClass('nav-normal');

This will cause the CSS to animate automatically.

Answer №3

To avoid setting the height of the #header_nav using its ID in CSS, follow these steps:

Include CSS transition properties for the navbar's element height:

transition:height 0.4s;
-webkit-transition:height 0.4s;

In the HTML body, set the navbar height to "100px" using the style attribute.

Use the following script:

function applyTransition(element, properties){
    $(element).css('transition', properties);
    $(element).css('-webkit-transition', properties);
    $(element).css('-o-transition', properties);
    $(element).css('-ms-transition', properties);
}

applyTransition('#header_nav', 'height 0.3s');

$(window).scroll(function(element){
    if($(window).scrollTop()<10){ // detect initial scroll position
         $('#header_nav').css('height','100px'); // expand
    }else{ // scroll position is larger...
         $('#header_nav').css('height','60px'); // collapse
    }
});

*It is recommended to set the navbar overflow to hidden*

See it in action here:

http://jsfiddle.net/r4xprh65/13/

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

Some elements are not responding to margin-top properly

I am facing a problem where 2 elements are not responding to the specified margins of margin: 260px 0 0 0; or margin-top: 260px;. Despite inspecting the elements in IE's dev tools and confirming that the margin is set, the elements appear at the top o ...

The JQuery hover feature seems to be malfunctioning in Internet Explorer specifically when called in a subpage through an ajax request

I have a JSP where I load a simple drop-down menu and retrieve the sub-menu using AJAX jQuery. In my master JSP, when the user selects a button, it invokes the drop-down JSP and displays it in the master JSP. In Firefox, after the AJAX call is completed, ...

Ensure that the floating div is positioned along the right side and adjusts to accommodate the

I've been searching for a solution to this issue, but it seems like I might not be articulating it correctly because I haven't been able to find an answer yet. On my page, there is a 'quick links' button that is supposed to stay fixed ...

What is the best way to convert minutes into both hours and seconds using javascript?

In order to achieve this functionality, I am trying to implement a pop-up text box where the user can choose either h for hours or s for seconds. Once they make their selection, another pop-up will display the answer. However, I am facing issues with gett ...

Update: "Mui V5 - Eliminate collapse/expand icons in TreeView and reduce TreeItem indentation"

My current project involves Mui V5 and I am looking to customize the TreeView component. Specifically, I need to remove the collapse/expand icons as I want them to be integrated into the TreeItem label component on the left side instead of the right. Add ...

Display a progress bar that shows completion based on the maximum value supplied

I successfully created a progress bar using HTML, CSS, and Javascript. It functions perfectly up to a provided value of 100. However, if a value higher than 100 is given, the progress completes but the value continues to change until it reaches the maximum ...

Event callback method in jQuery

Currently working on a captcha system that incorporates jQuery to refresh the image in case it's not readable. $('a#reload').click(function(){ $(this).removeClass('reload').addClass('reloading'); var ts = Math. ...

Double-Image Display: Ennui Content Slider showcasing dual images simultaneously

Hey there! I am attempting to utilize the Ennui Content slider in order to showcase two images for each sequence of images. If you're interested, here is the link to the slider: I'm a bit unsure about what modifications or steps I need to take ...

Spacing in CSS between the menu and its submenu elements

My current challenge involves creating space between the menu and the submenu. However, I've noticed that when there is this space, the submenu doesn't function correctly. It only works when the design has no gap between the menu and the submenu. ...

What is the best way to adjust the z-index when hovering over an li element

In my <ul>, each <li> contains an image. I want these images to scale and become opaque upon hover. However, the issue is that when they scale, they get covered by the following image. Unsure if the z-index is correctly placed... Below is the C ...

Switch up background colors for different Div Labels

I have a unique structure resembling a table using div elements. My goal is to alternate the background color of only the divLabels. Each "table row" consists of a divLabel and a divData component. Here is my attempt so far: http://jsfiddle.net/o5dv4qk ...

Attempting to adjust the background colors of divs in a specific order

I am currently experimenting with the JQuery jquery.animate-colors-min.js plugin to create a sequential change in background color for multiple div boxes. $('#firstbox').animate({ backgroundColor: "#f6f6f6" }, 'slow') Animating the fi ...

How can I dynamically set the width of a span element in HTML?

Hello there! As a beginner in html and angularjs, I'm experimenting with dynamically assigning span width based on an angular js response. <div class="statarea" ng-repeat="x in names"> <div class="ratingarea"> <div class=" ...

I possess an array with a specific structure that I wish to substitute the keys as demonstrated below

The data array is currently structured in this way. I have attempted various methods but have not been able to find a suitable solution. I need to send the JSON below via an AJAX request, however, I do not want the keys 0 and 1 in both child arrays. [sch ...

What causes an "Internal Server Error" when attempting to use data for a database request with AJAX GET/POST in Laravel?

There's a unique issue that I'm struggling to resolve... Every time I drag and drop an event into the calendar, an Ajax Post Request is sent to my controller. The controller then inserts some data into the database with the event name received v ...

Ways to retrieve the URL prior to rendering it

I am trying to create a system that can track which URLs the user has visited and count how many times each URL has been opened. There are multiple URLs that lead to the same WaitView (TemplateView), so I need to track the count for each specific URL when ...

Adjust the start and end dates of the Bootstrap datepicker according to the chosen value

I am looking to dynamically set the startDate and endDate of a Bootstrap datepicker based on the value selected in an option form. For example, if I select "Boby" from the options, then the datepicker should be configured with startDate = 2016-10-04 and e ...

Ways to verify if the flash plugin has been disabled in the Chrome browser

Is there a way to use JavaScript or jQuery to detect if the flash plugin is blocked in Google Chrome? You can check for a disabled flash plugin using the following code: ((typeof navigator.plugins != "undefined" && typeof navigator.plugins["Shock ...

Modifying the button for the ajaxtoolkit:AsyncFileUpload widget

I am currently using the ajaxtoolkit:AsyncFileUpload and I'm curious if there is a way to customize the button that comes with it? <ajaxtoolkit:AsyncFileUpload Width="200" ID="filImageUpload" ThrobberID="imgUploadProgress" OnUploadedComplete= ...

What is the best way to arrange the elements of a dropdown list in reverse order?

I have the following code snippet used to retrieve data from a database and display it in a dropdown list: @{ int m = 1; foreach (var item in Model.MessagesList) { if (@item.receiverUserId == userId) { <li> <a class=&qu ...