Scaled-down navigation when scrolling

Is there a way to make the navigation bar shrink when scrolling, similar to how it functions on this website? The transition on this site is so seamless.

I'm guessing it's achieved with JQuery.

Answer №1

Check out this coding example for applying a class on scroll to a specific height and shrinking it in CSS once the class is added:

$(document).scroll(function() {
  if ($(document).scrollTop() > 100) {
    $('.header').addClass('shrinkIt');
  } else {
    $('.header').removeClass('shrinkIt');
  }
})
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.header {
  width: 100%;
  background: rgba(255, 255, 255, 0.6);
  position: fixed;
  top: 0px;
  left: 0px;
  padding: 50px 100px;
  transition: all .3s ease;
  font-size: 22px;
}
.header.shrinkIt {
  padding: 20px 100px;
  font-size: 16px;
}
.content {
  background: #2b2b99;
  height: 1400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="header">
    I will shrink.
  </div>

  <div class="content">



  </div>
</div>

Answer №2

To create a smooth scrolling effect on your website, you can implement a scroll event handler on the window object that adds or removes a class from the body of your document. This class can then be styled in CSS to achieve the desired design. While jQuery's animate function provides cross-browser compatibility for animations, using CSS transitions is often preferred for smoother effects.

For example, if you observe the behavior on the site you mentioned, you will notice that as you scroll, the header element's class changes to 'short'. Their code snippet from custom.js demonstrates how this is achieved:

// Scroll to top
jQuery(window).scroll(function ()
{
    if (jQuery(this).scrollTop() > 100)
    {
        jQuery('header.navbar').toggleClass('short', true);
        jQuery('.scrollup').fadeIn(300);
        jQuery('.scrollup-jobs').fadeIn(300);
    }
    else
    {
        jQuery('header.navbar').toggleClass('short', false);
        jQuery('.scrollup').fadeOut(1000);
        jQuery('.scrollup-jobs').fadeOut(1000);
    }
});

Answer №3

Create two CSS classes, one of which includes a new class named .smaller.

CSS

.navigation{
  /* CSS for larger navigation */
}

.navigation.smaller{
  /* CSS for smaller navigation */
}

You must write the CSS for both states, and then add some jQuery functionality.

JQUERY

$(document).ready(function(){
    $(window).scroll(function(){
        var windowScroll = $('html, body').scrollTop();
        if(windowScroll >= 100){ // Scroll value can be adjusted
            $('.navigation').addClass('smaller');
        } else {
            $('.navigation').removeClass('smaller');
        };
    });
});

The jQuery above will detect when you scroll the window to 100px, it will add the 'smaller' class to the navigation which already has corresponding CSS rules in place.

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

Transform JavaScript variables into CSS variables

Similar Post: Avoiding repeated constants in CSS I am currently working on a javascript file that aims to adjust my site's resolution based on the width and height of the viewport. While I have successfully retrieved these values using JavaScript ...

An effective way to prevent right-clicking on iframes across all websites

I am facing an issue with disabling right click for the iframe. I've successfully disabled it for the default URL of the IFrame, but when displaying any other webpage, the right click remains usable. Below are the sample codes I have used: document.o ...

Designing draggable tags similar to those on LinkedIn, incorporating a parent div element containing an SVG image and text

Lately, I've been exploring the world of CSS and Angular. Can someone give me a jumpstart on using CSS to design an element like the one shown in this https://i.stack.imgur.com/vcR3Z.png image? Essentially, this outer tag consists of a div element th ...

verify that the div has finished loading with ajax

I am working on a project where I have a div with the id #pageToLoad. This div loads a form from formPage.php. I want to ensure that the page is fully loaded in the div so that if any errors occur, I can reset the form using: $("#form").reset(); Can some ...

Obtaining Values from Identical jQuery Buttons with the Same Class名称

I am facing an issue with my PHP script that generates buttons for each content schedule. The script outputs multiple HTML buttons with the same name, class, and ID, but their values are dynamically set based on a variable. In my jQuery code, I have selec ...

The functionality of the button is currently not compatible with Internet Explorer

I have encountered an issue in my application involving a combination of <h:outputLink> and <p:commandButton> as shown below: <h:outputLink target="_blank" value="theURL"> <p:commandButton value="Click" type="button" /> </h: ...

How to customize TextField error color in Material-UI using conditional logic in React

Currently, I am incorporating the React Material-UI library into my project and faced with a challenge of conditionally changing the error color of a TextField. My goal is to alter the color of the helper text, border, text, and required marker to yellow ...

Unsure of the response from the $.post request

CHANGE: I keep receiving the object {"readyState":1}. Why is this happening? How can I successfully retrieve the result from the $.post (specifically, the object {"result":"ERROR"})? This pertains to using jEditable. (Please note: This seems more like a ...

Animating colors with jQuery and shifting SVG shapes to create dynamic and

I am currently working on an svg animation that involves changing the color of the svg to a different color, creating a running light effect. Rather than fading the fill color of the entire svg as commonly seen in examples, I aim to achieve a dynamic trans ...

Struggling with identifying jQuery ajax errors?

I am attempting to handle all errors on my own. When an error occurs in jQuery.ajax(), I can detect it using the error options. However, this error is not catchable by my window.onerror event. window.onerror = function(e) { console.log("I was here! E ...

JavaScript drop-down menu malfunctioning

I am currently in the process of learning web development languages, and I'm attempting to create a dropdown list using JavaScript (I previously tried in CSS without success). I would greatly appreciate it if you could review my code and let me know ...

What could be causing this highchart plot to fail to display in both IE and Chrome?

Check out the plot in this jsfiddle: http://jsfiddle.net/essennsee/y5HCm/ The plot looks good in Firefox, but only shows the legend in IE and Chrome. If I remove the following code snippet it works fine. Is there a different way to format the labels?: ...

Exploring the use of Shadow DOM in ContentEditable for securing text segments

I have recently been working on creating a basic text editor using ContentEditable. The main requirement for the application is to allow users to insert blocks of text that are protected from typical editing actions. These protected text blocks should not ...

Implementing image rendering functionality in Vue.js

So here's what's going on: I developed a horror movie bucket list app for my bootcamp final project. The minimum viable product received positive feedback, and I obtained my certification. However, now that I've graduated, I want to enhance ...

What is the best way to distribute components with varying cell heights?

I am currently working on creating a user interface layout with components that need to be arranged in a specific order, as depicted in the accompanying image. My task involves developing a Material UI Dialog within a React application based on the design ...

Export specific rows from a datatables table using tabletools

I'm currently working with datatables and tabletools on a sizeable table that is populated through an ajax request. Once the user selects multiple rows, they are displayed as selected within the table. Is there a way to specify the csv/xls export to ...

Unsuccessful AJAX form submission failing to include file input type

"I'm having trouble getting the $_FILES variable in my PHP script when posting a simple form using AJAX." <form id="submitForm" method="post" enctype="multipart/form-data" > <input type="file" name="file" /> <input type="tex ...

Issue encountered when making several calls with the jQuery Ajax load() function

Hey there! I'm currently in the process of incorporating the jQuery load() method to dynamically load content into a web page based on the input value entered. However, I seem to be encountering an issue where multiple AJAX calls are being made simult ...

A PHP tag containing a div element

As someone who is new to web development, I have a question that may seem silly to some. I am familiar with adding div tags inside php tags, but I am unsure if it's possible to add onclick functions or any other type of function to these div tags. He ...

Webgrid columns lose their width after a postback event

I'm working on a simple webgrid that has 3 columns: View columns: grid.Columns( grid.Column("Applicant Name", format: (item) => @Html.ActionLink((string)item.Name, "EditApplicant", "Applicant", new { id = item.ApplicantId }, null), style: "AppN ...