Animating the change in height

In my case, the code snippet below modifies the size of my navbar as I scroll down and back up: The original height is 15vh while the smaller navbar height is set to 8vh

$(document).ready(function($){
    var nav = $('nav');

    $(window).scroll(function () {
        if ($(this).scrollTop() > 13.35) {
            nav.addClass("smallnav");
        } else if($(this).scrollTop() > 0) {
            nav.removeClass("smallnav");
        }
    });
});

Currently, the transition from small to big or vice versa happens instantly. How can I introduce an animation effect so that it smoothly shrinks or expands?

Answer №1

To enhance the smooth transition of height change, consider incorporating the following code:

  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;

Demo

$(document).ready(function($) {
  var nav = $('nav');

  $(window).scroll(function() {
    if ($(this).scrollTop() > 13.35) {
      nav.addClass("smallnav");
    } else if ($(this).scrollTop() >= 0) {
      nav.removeClass("smallnav");
    }
  });
});
nav {
  height: 15vh;
  border: 1px solid black;
  position: fixed;
  top: 0;
  width: 100%;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

.smallnav {
  height: 8vh;
}

body {
  height: 200vh
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>your nav </nav>

Answer №2

Embed this snippet into your navigation stylesheet

  -webkit-transition: height 1s; 
  -moz-transition: height 1s; 
  -ms-transition: height 1s; 
  -o-transition: height 1s; 
  transition: height 1s; 

This code will create a smooth height transition lasting for 1 second.

To learn more, visit this link.

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 icon for the weather on openweathermap is currently not displaying

Take a look at what my webpage looks like: http://prntscr.com/dg6dmm and also check out my codepen link: http://codepen.io/johnthorlby/pen/dOmaEr I am trying to extract the weather icon from the api call and display that icon (e.g. "02n") on the page base ...

What is the best way to apply custom styles in reactJs to toggle the visibility of Google Maps?

There are three radio buttons that correspond to school, restaurant, and store. Clicking on each button should display nearby locations of the selected type. Displaying Google Map and nearby places individually works fine without any issues. class Propert ...

Preserve the original color of links in CSS by using the `a

Is it feasible to specify in CSS that a link should not change its color and instead use the default one? For example: If I have text displayed in red, and that text is also a hyperlink. Typically, when clicked on, the text would change to blue because i ...

Transmitting a sequence of JSON information from php to JavaScript,

I am struggling to fetch a series of JSON data from PHP to my JavaScript file. Initially, I have multiple JSON data stored in an array in PHP, and I am echoing each one by looping through the array in my JavaScript file. <?php $result = array('{ ...

What is the proper method for utilizing the .done or .complete functions in conjunction with .toggle in jQuery?

I am struggling to understand the proper usage of .complete or .done after .toggle in jQuery. My goal is to have the button's text change after the toggle animation finishes, but I'm not sure if I'm chaining them correctly. The jQuery docume ...

Tips for invoking a custom function in jQuery with a delay when hovering

I've been struggling with my syntax, trying to achieve something simple. I have a function called 'displayUserinfo' that pops up a box with user information. It is triggered when the user clicks on an element with the class 'avatar&apo ...

How to grab selected HTML content with jQuery

I have been scouring the internet for a solution to this issue. My goal is to convert text selection start and end points into their corresponding HTML selection values. For example: HTML: test text **some <span style="color:red">te**st t</span& ...

JS/JQuery: Retrieve value from dropdown list or input field

I'm looking for a way for my user to choose a value from a drop-down menu, but if they can't find the right option there, I want them to be able to input a custom value. I've figured out a workaround by deactivating the drop-down and activa ...

Unpredictable Instances of JQuery AJAX Failing to Execute

Utilizing JQuery's AJAX feature enables me to send values to my PHP server for interpretation and processing. While everything is running smoothly, occasionally (at random intervals), the AJAX call seems to not execute properly. The AJAX code I am u ...

Limit jQuery to selecting only one list item (li) element

I am facing an issue where I can only add one value of a li-tag to an input at a time. How can I make it so that multiple values of the li-tags are added, separated by commas? Additionally, it would be ideal if each li value is only added once. I appreciat ...

What is the most effective way to eliminate error messages from the email format checker code?

Recently, I've been working on a code to validate forms using javascript/jquery. One particular issue I encountered was related to checking email format. The problem arose when entering an invalid email - the error message would display correctly. How ...

JavaScript - Delayed Text Appearance with Smooth Start

I want to create a landing page where the text appears with a slight delay - first, the first line should be displayed followed by the second line. Both lines should have an easing effect as they appear. Below is a screenshot of the section: https://i.sst ...

Requesting a resource using the HTTP GET method returned no data

Looking to process the response from an http-request using JavaScript? Check out this straightforward example below. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x ...

I seem to have misplaced the plot here, but can someone explain why this tiny snippet of JavaScript isn't functioning properly?

I am looking to create a universal error handler for all my forms var errorHandler = function() { var $errorBox = $('#form-error'); return { add: function(errors) { if ($errorBox.length === 0) { $erro ...

Unsure how to proceed with resolving lint errors that are causing changes in the code

Updated. I made changes to the code but I am still encountering the following error: Error Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper object. It is recom ...

Using Javascript to parse a json file that contains additional content at the beginning

I am attempting to access a JSON URL, but the JSON is displaying some extra characters before it starts. The structure of the JSON script is as follows: throw 'allowIllegalResourceCall is false.'; { "name":"mkyong", "age":30, "addres ...

What's the best way to customize the color of the text "labels" in the Form components of a basic React JS module?

I have a React component named "Login.js" that utilizes forms and renders the following:- return ( <div className="form-container"> <Form onSubmit={onSubmit} noValidate className={loading ? 'loading' : ''}&g ...

Implementing jQuery Validation Plugin for Form Fields including Textarea

I'm struggling a bit with understanding how to incorporate the jQuery Validation Plugin into my form submission process using Ajax: $(document).delegate("'#submit-quote'", "submit", function(){ var quoteVal = $(this).find('[nam ...

Obtaining the textarea value from LineControl Editor in an MVC Controller: A Step-by-Step Guide

I recently integrated a jQuery plugin into my MVC 5 project to create a wysiwyg text editor. In one of the views, I included a textarea where the text editor is meant to be displayed. However, I encountered an issue with binding or retrieving the value o ...

Is there a way to change this from webkit to moz?

Is there a way to change this code from webkit to moz? background-color: #fff; background-image: -moz-linear-gradient(0deg, transparent 79px, #ABCED4 79px, #ABCED4 81px, transparent 81px), -moz-linear-gradient(#EEE .05em, transparent .05em); ...