Reducing the size of a static top bar while scrolling, along with a secondary header

While searching for methods to resize the header of my website upon scrolling, I came across an example that worked well. You can view the working example on JsFiddle

index

<!DOCTYPE html>
<html lang="en">
<head>
...
</body>
</html>

css

.header {
width:100%;
height:150px;
overflow:hidden;
position:fixed;
top:0;
left:0;
z-index:999;
background-color:#0683c9;
...

.sub-header {
background-color:#000;
height:40px;
color:#999;
width:100%;
}

.clearfix:after {
visibility:hidden;
display:block;
content:"";
clear:both;
height:0;
}

and the js

( function( window ) {

'use strict';

// class helper functions from bonzo https://github.com/ded/bonzo
...

})( window );

The Challenge.

I have implemented a fixed header with two parts (main header and sub-header) as shown in the image here: [link to image]. However, whenever I scroll down, the main header shrinks but the sub-header (black in color) remains visible. I would like the sub-header to disappear when scrolling happens along with the shrinking of the main header. Despite several attempts, I haven't been able to achieve this. Can anyone provide suggestions on how this can be accomplished?

Answer №1

If you're looking to modify your CSS for a smaller class definition, you could try something like this:

header.smaller {
    height: 5px; /* or even less */
}

Here is a sample of how it can work: https://jsfiddle.net/qc01zwyg/


Update:

To remove the black sub element, you can target its CSS within the .smaller selector.

header.smaller {
  height: 75px;
}

header.smaller .menu {
  display: none;
}

Check out the updated example here: https://jsfiddle.net/qc01zwyg/2/

Answer №2

Consider implementing a style adjustment for the sub-header.

.menu {
  background-color: #000;
  height: 40px;
  color: #999;
  width: 100%;
  transition: height .3s;
}

.smaller .menu {
  height: 0;
  overflow: hidden;
}

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

Having trouble extracting HTTP Post data in Node Express?

I am encountering an issue while attempting to send an HTTP POST request from my HTML frontend to a Node.js Express backend. Despite using XMLHttpRequest on the frontend, the server seems to be receiving only an empty object in response. I would greatly ap ...

Node.js bug: unexpected behavior when storing a function's return value as a variable

Not much to say right now... I've been utilizing node.js as a WebSocket server, and I've encountered this dilemma?: see image description here (the expected output is: 'works here') These are the lines of code causing this issue: see ...

Navigation dropdown menu with the latest Bootstrap 4 features

Attempting to recreate the design from Codepen using Bootstrap 4. The current progress can be viewed in this Codepen (drop down with 2 columns), which is assisting with online tutorials. Here's the HTML for the Navbar: <nav class="navbar navbar- ...

Tips for passing an object by value to an Angular2+ component

Imagine having a scenario where I create a component that takes a Foo instance and generates a form for editing. export class ChildComponent implements OnInit { @Input() foo : Foo; @Output() onChange : EventEmitter<Foo> = new EvenEmitter<Foo& ...

The React JS textarea's onchange event is not functioning properly when trying to set the value

This is a class component I created for an auto-suggest text area similar to Twitter's feature. import React, { Component, createRef } from 'react'; import { TextArea, List, Triangle } from './Styled'; import ge ...

Creating personalized text in the react-native calendar界 can be as simple as following these

I am currently using the react-native-calendars library in my react-native project image description goes here I want to display text or emojis for each day if they exist import React from 'react'; import { Text, View, StyleSheet, TextInput } fr ...

Troubleshooting Navigation Bar Toggle Button Issue in Bootstrap 5

Currently, I am in the process of working on a web project that requires the implementation of a responsive sidebar. This sidebar should be toggleable using a button located in the navigation bar. My choice for the layout is Bootstrap, and I have come acr ...

Sending checkbox data to controller using jquery/ajax and handling null values returned to the controller

I am trying to send data from my .NET MVC application to my controller using jquery ajax. Although everything appears to be working fine in the Firebug console (with values showing up in an array in my post request), when I check the controller, I am recei ...

What is the best way to collect and store data from various sources in an HTML interface to a Google Spreadsheet?

Currently, I have a spreadsheet with a button that is supposed to link to a function in my Google Apps Script called openInputDialog. The goal is for this button to open an HTML UI where users can input text into five fields. This input should then be adde ...

No cookie found in the system

Attempting to create an effect using bloom and shaders in post-processing. However, encountering an error in the console with a blank white screen. I have tried clearing cookies, caches, and even running this in incognito mode, but it's still not work ...

Tips for preventing a component from updating state prior to data retrieval?

I have a specific scenario where I am working with a page that consists of two components. In this setup, I am retrieving data from the root component and passing it to the child component using the react-redux library. However, I encountered an issue wher ...

React/JS property divided by a colon

Can you explain the significance of the colons used in between { placeholder }: { placeholder: string } within the props of the Search component? This pattern is also seen later in the code for the main Page component.... Does it signify that the expected ...

Name or Title of a Polygon/Polyhedron Using Three.js

My page contains a sample code that successfully retrieves the name of an object when a user clicks on it. However, the code works well with cubes and spheres but fails with polygons. To see how the clicks respond, you can check the console logs. What shou ...

For every iteration, verify the presence of the image

I am currently working on a foreach loop to iterate over the data returned by an ajax call. Within this loop, I am checking if each record has an associated image. Code for Checking Image Existence: function checkImageExists(url, callback) { var img ...

Utilizing useEffect in the header component of ReactJS/Next.js: A Comprehensive Guide

In my next.js/react project, I have a component called header.js that is included on every page. Within this component, I've implemented a typewriter effect with rotation by using the following code snippet inside useEffect: export default function H ...

What could be the reason for the appearance of Next.js compile indicator in my final production build?

Upon completing the development and deployment of a Next.js website, I observed that the black compile indicator continued to appear in the bottom-right corner of my browser, similar to its presence during local development. The indicator can be viewed he ...

What is the best method to apply a background image in HTML that functions properly in Internet Explorer?

Having trouble getting this CSS code to work in Internet Explorer. Need a solution that is compatible with IE for setting the background. body { background: url("images/Login-BG.png") no-repeat center center fixed; -moz-background-size: cover; -webkit ...

What is the best way to specify multiple types as a repeatable parameter?

/** * Calculate the total sum of all numbers provided to the function. * @example * sum([1,2,3]) * //results in 6 * sum(1,2,3) * //results in 6 * @param {number[]|...number} params - Represents an array of numbers or individual numbers passed as arg ...

Error encountered in react-test-renderer test due to TypeError: Attempting to access properties of an undefined variable, specifically trying to read the 'current

Having trouble with my unit tests using react-test-renderer. Even though the component I'm testing is working fine, the test won't pass. Can someone please help me figure out what I'm doing wrong? Below is the code for the component: // ...

Implement the jQuery colorbox plugin onto a dynamically generated element to display the initial image consistently

Utilizing DataTables.net, I have dynamically created a picture table using ajax. Presently, displaying the pictures through colorbox is functioning flawlessly. $('.colorbox').live('click', function(e) { e.preventDefault(); $(&a ...