Sleek scrolling animations

I've implemented a navbar that changes its size on scroll by adding a class called "shrink". While it's functioning correctly, I find the resizing happens too quickly and doesn't look smooth. I want to make the shrinking animation smoother but haven't been successful so far. Here is my current code:

$(window).scroll(function() {
  if ($(document).scrollTop() > 50) {
    $('nav').addClass('shrink');
  } else {
    $('nav').removeClass('shrink');
  }
});
body {
  padding-top: 50px;
  min-height:800px
}

nav a {
  padding-top: 20px !important;
  padding-bottom: 20px !important;
  font-size: 18px;
}

nav .navbar-toggle {
  margin: 13px 15px 13px 0;
}

.navbar-brand {
  font-size: 30px;
}

nav.navbar.shrink {
  min-height: 35px;
}

nav.shrink a {
  padding-top: 10px !important;
  padding-bottom: 10px !important;
  font-size: 15px;
}

nav.shrink .navbar-brand {
  font-size: 25px;
}

nav.shrink .navbar-toggle {
  padding: 4px 5px;
  margin: 8px 15px 8px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav pull-right">
        <li class="active"><a href="#">Home</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

<div class="container">
  <div class="text-center">
    <h1>Bootstrap starter template</h1>
    <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
  </div>
  <div class="text-center">
    <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
  </div>
  
</div>

Answer №1

please review this code snippet where I have included the transition property.

$(window).scroll(function() {
  if ($(document).scrollTop() > 50) {
    $('nav').addClass('shrink');
  } else {
    $('nav').removeClass('shrink');
  }
});
body {
  padding-top: 50px;
  min-height:800px
}

nav a {
  padding-top: 20px !important;
  padding-bottom: 20px !important;
  font-size: 18px;
}

nav .navbar-toggle {
  margin: 13px 15px 13px 0;
  -moz-transition: all 0.3s;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

.navbar-brand {
  font-size: 30px;
      -moz-transition: all 0.3s;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

nav.navbar.shrink {
  min-height: 35px;
    -moz-transition: all 0.3s;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}

nav.shrink a {
  padding-top: 10px !important;
  padding-bottom: 10px !important;
  font-size: 15px;
}

nav.shrink .navbar-brand {
  font-size: 25px;
}

nav.shrink .navbar-toggle {
  padding: 4px 5px;
  margin: 8px 15px 8px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav pull-right">
        <li class="active"><a href="#">Home</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

<div class="container">
  <div class="text-center">
    <h1>Bootstrap starter template</h1>
    <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
  </div>
  <div class="text-center">
    <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
  </div>
  
</div>

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

Managed inputs versus FormData API

Recently, I came across a fascinating article that has the potential to revolutionize how web form submissions are handled in ReactJS moving forward. Check it out: https://medium.com/@everdimension/how-to-handle-forms-with-just-react-ac066c48bd4f What ar ...

"Double Up Your Design with CSS Double Border Styling

While working on my website's design, I came up with an idea but wasn't sure how to execute it. After searching extensively, I couldn't find a helpful solution. Can anyone here lend me some assistance? My idea involves using border: 10px do ...

What is the equivalent of angular.isString() in Angular 2?

I am currently working on a project using Angular 2 and I'm interested in finding out if there is a way to incorporate AngularJS functionalities into my Angular 2 application. For example, in AngularJS, I used to perform the following operations: a ...

Vue.js - Problem with loading image on screen

Although I have experience with React, I am currently facing the challenge of using Vue for a code assessment for the first time. My struggle lies in creating a reusable image component with WAI-ARIA accessibility. Despite my efforts, I cannot get the imag ...

Is there Polyfill Compatibility for Custom Elements in Angular 9?

When it comes to polyfilling support for custom elements created with Angular, there are various recommendations available. This demo demonstrates that adding the following polyfill in polyfills.ts works: import '@webcomponents/webcomponentsjs/custo ...

Executing a function when an item is dragged in jQuery UI sortable

I have a "draggable" block and a "sortable" block. What I am trying to achieve is to trigger an action through jQuery when I drag an item from the "sortable" block. Below is the JavaScript code I am currently using: $(".sortableList").sortable({ }); ...

Internet Explorer has a known issue where the jQuery Form function ajaxForm() does not properly process radio buttons that are set using JavaScript

Incorporating data retrieved from Google Maps, I am setting a radio button using the following code snippet: countryCode = results[0].address_components[i].short_name; $('input[type=radio][name=country]').prop('checked', false); $(&apo ...

Storing a blob on the server?

I have a program that can extract metadata from sound files. If the sound file contains an album image, I am able to convert it into a blob object URL and use it locally. However, I am struggling with saving this blob image to the server. Currently, I ha ...

Struggling to find a solution for changing the color of a select box when an option is chosen

Here's an example of the HTML I'm working with: <select onclick="colorchanger()"> <option name="white" value="0">--Select--</option> <option name="red" value="1">Work</option> <option name="green" value="2" ...

Filling out a form with existing data in React.JS

As I venture into the world of React, I encountered a challenge that has left me puzzled. My goal is to pre-fill a form with data fetched from a database using a parent component. The child component should display this fetched data as default and then adj ...

Firefox triggers drag events while passing over a div scrollbar

I am looking to create a file drag and drop feature using a styled div: When dragging files over the div, I want the border color to change When dragging files out of the div, I want the border color to revert back to the original In Firefox 35 (Ubuntu) ...

What is the best way to add or delete data when specific radio buttons are chosen?

Hey there, I'm facing an issue where the data is being appended regardless of which radio button is selected. Can someone help me with a solution on how to properly add and remove data based on the selected radio button? $( document ).ready(functio ...

Leveraging Vue Data for Storing CSS Properties

I am currently utilizing the Quasar framework in conjunction with Vue for my application development. Below is a snippet of my code: <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"> Save </q-tooltip> <q-tooltip c ...

"Leaflet automatically refreshes tile cache when zooming in or out

Currently, I'm facing an issue regarding the tile cache in leaflet. If I move from point A to point B, and examine the tiles in between, they are cached without any problems. However, if I go from A to B, zoom in and out, and then return to point A, ...

serving index.html using express and react

I am currently working on an application with Express, which includes a create-react-app. My goal is to display the index.html file located in the public folder of the create-react-app when a user visits the root URL. Below is the code snippet from my inde ...

How can you identify and detect the id of an li element that has been loaded from a JSON

I've got a ul element filled with li elements that were fetched from a json file. My goal is to pass certain data from that json file to another page (a jquery mobile page) based on the id of the clicked li element. However, I'm facing issues in ...

The price filter slider is experiencing issues with the onresize function not functioning properly

I am facing an issue with a price filter I developed for my project. Despite having coded it, the filter is not functioning properly. <div class="price_range_caption"> <span class="currency_from">Rs.</span><span id="price_range_f ...

What is the best way to test a component that displays its children elements?

I am encountering an issue with testing the functionality of a component I have created. The component is a Button that accepts props such as className, children, and otherProps. export default function Button({ className, children, ...otherProps }) { r ...

Encountering an error of "Insufficient permissions" while trying to logout from a Firebase React application

In my React/Firebase project, I am encountering an "Uncaught Error in onSnapshot: FirebaseError: Missing or insufficient permissions" issue when logging out. This is likely due to the read/write rules that have been implemented. I suspect that the problem ...

Struggling with adjusting the color scheme on a specific page of my WordPress website

Currently, I'm attempting to customize the colors of my user profile page located at www.wastelandgamers.com/user-profile. My goal is to transform the blue fields into white and change the text within those fields from white to black. While I can ach ...