Need help resolving an issue with an HTML header that's overlapping in JavaScript?

Hey there! I was working on implementing a dynamic header that resizes as you scroll on the page. I also decided to try out Headroom as an additional solution, and it seems to be functioning well in terms of hiding the header. You can check out my progress HERE. The first time you scroll, the resizing happens smoothly to reach a smaller header of 45px, after which Headroom kicks in to hide it. However, on subsequent scrolls, the resizing doesn't happen smoothly and there's a noticeable jump to the smaller header before Headroom hides it. I'm working on ensuring a seamless resizing experience every time you scroll (you'll see the resizing at shrinkOn = 50, and the header hide at a tolerance of 300 in the headroom code).

Here is the script for shrinking the header:

<script>
    function init() {
        window.addEventListener('scroll', function(e){
            var distanceY = window.pageYOffset || document.documentElement.scrollTop,
                shrinkOn = 50,
                header = document.querySelector("header");
            if (distanceY > shrinkOn) {
                classie.add(header,"smaller", "headroom");
            } else {
                if (classie.has(header,"smaller", "headroom")) {
                    classie.remove(header,"smaller", "headroom");
                }
            }
        });
    }
    window.onload = init();
</script>

And here is the script for activating Headroom:

var myElement = document.querySelector("header");
//construct an instance of Headroom, passing the element
var headroom = new Headroom(myElement);
//initialize
headroom.init();

These are the options:

"tolerance": 2,
"offset": 350,

Thank you in advance for your help and suggestions!

Answer №1

At the beginning, your classes in the header are: headroom headroom--top

During the transitions, you will add smaller, remove headroom--top, replace it with headroom--not-top, and add headroom--unpinned. So the second bar looks like this:

headroom smaller headroom--top headroom--pinned

And it ends with:

headroom smaller headroom--not-top headroom--unpinned

When going back through the transitions, switch headroom--not-top with headroom--top and headroom--unpinned with headroom--pinned, then remove smaller. However, never remove headroom--pinned, which is essential to return to the original state.

Therefore, it is important to include this line in the script:

...snip
    classie.remove(header,"smaller", "headroom");
    classie.remove(header,"headroom--pinned", "headroom");  //add this line here
}

For reference, visit: Your modified codepen

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

I need help with making multiple XMLHttpRequests within a single JavaScript file. Can someone provide guidance on

https://i.sstatic.net/M0EJk.png Hey lovely people! I need some help. I'm trying to send two HTTP GET requests and log their responses in the console. However, I'm only seeing the response from the first request and the second one seems to be stuc ...

When modifying v-model directly in a Vue instance, the Vuetify component on the screen may not update its displayed value

Within my Vue component example (utilizing Vue 2 and Vuetify 1), I have implemented an input field for user data entry. However, I am looking to programmatically alter the input field's data to a specific value. For instance, in this scenario, I aim ...

Leverage the power of Vue Nuxt Auth to activate authentication middleware on a route-by-route

Is there a way to implement auth middleware for individual routes using Class Components? How can I achieve the same functionality as this: <script> export default { middleware: 'auth' } </script> The following method does n ...

Building a dynamic URL in ReactJS from scratch

const selectedFilters = { category: "", mealtype: "lunch", cuisinetype: "Italian", dishType: "Pasta" } const apiUrl = `https://api.edamam.com/api/recipes/v2?type=public&q=${query}&app_id=${app_id}&app_key=${app_key}`; User ...

The various sections of my website are neatly tucked away under one tab, only revealing themselves to users as they click on each individual tab

My recently published website is experiencing a strange issue. When the site loads, all contents including paragraphs and videos from other pages are displayed under one tab (the HOME tab). However, once any other tab is clicked, the issue fixes itself. Yo ...

Retain the spaces within a string in Java and JavaScript programming languages

My Java code sends data to the browser via an HTTP request, and the output looks something like this: JAVA CODE OUTPUT | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | | | 3 ...

What is the best way to get rid of trailing numbers in material UI class names?

The standard class for the material ui input box is .MuiInputBase-input, but when I inspect it using developer tools, I see that the same class is displayed as .MuiInputBase-input-619. How can I remove the suffix from the class name? I am currently utili ...

Utilizing Jest to Mock a jQuery Method within a Promise

I have created a function that utilizes a jQuery function named dataFunc, which is expected to return an object. Instead of testing the dataFunc function itself, I want to focus on testing the promise it produces. To achieve this, I need to mock the respo ...

The link button appears unselected without a border displayed

I am facing an issue with a link button in my code. Here is the snippet: <div class="col-2"> <a type="button" routerLink="auto-generate-schedules/generate" class="btn btn-primary mb-2">Generate Sche ...

"Exploring the concepts of inheritance in Typescript

I'm seeking answers regarding inheritance in TypeScript/JavaScript. Below is my base class (express controller router): abstract class BaseCtrl { abstract model; // Get all getAll = (req, res) => { this.model.find({}, (err, docs) => ...

Tips for addressing responsiveness issues in Bootstrap 4 column layout

Currently, I am working on creating a responsive profile page with Bootstrap 4. The challenge I am facing is that when viewed on smaller screens, the content shifts to the left while the center and right sections remain empty. This issue particularly affec ...

Issue with jQuery hide() function in Internet Explorer 9

I need help with creating a hidden div that becomes visible when a user clicks a link. The code I have works in FF / IE7 / IE8 but not in IE9, where the div is always visible without any content. Any advice is appreciated! <script> $(document).r ...

The height of the Bootstrap Sidebar is not displaying correctly

Currently, I am working on developing a traditional sidebar layout for the left portion of my template using Bootstrap 4. Here is the code snippet I have implemented: <nav class="team-left-column col-12 col-md-4 col-lg-2" style="background-color: ...

"Trouble with Bootstrap 4 menu: only one dropdown opens at a time

I am facing an issue where the text remains a link and the dropdown toggle works, but it is causing Dropdown-2 to open when I click on Dropdown-1. Only Dropdown-1 seems to be working properly. I can't seem to figure out why only Dropdown-1 opens no m ...

What is the process for incorporating a class into a table within TinyMCE using JavaScript?

I am facing an issue with adding a class to a table. I'd like the following code: <table></table> to transform into this code by clicking a button in tinymce. <table class="try-class"></table> I have added a button, bu ...

When using Reactjs with leaflet-routing-machine, the waypoint is rendered twice

While attempting to use the leaflet-routing-machine library, I encountered a bug. The issue is that the "Waypoints" section is rendering twice. Why is this happening? Can anyone offer assistance? Thank you https://i.sstatic.net/MlkQ2.jpg My code is lis ...

Using jQuery, how can you make fixed elements fade as the page scrolls?

How can I make a fixed element, such as a corner ad or notice, fade when the page is scrolled down to a certain point? What would be the most effective method for determining this point: using pixels, percentage, or another way? And how can I implement th ...

Show the attribute of an element in a pop-up window

I have a modal from ngx-bootstrap that I want to display in there a property of my object let's say the name: public students = [ { id: 1, name: 'lorem'} In this button that is common for all entries in the table (each row has this butt ...

Guide to leveraging dependency chaining in Node.js

Scenario: I am working with a node.js library that utilizes sequelize for defining and exporting models. This library is being used in my backend application, which also requires access to the functionalities provided by sequelize. Query: How can I proper ...

Parsing JSON data using if/else conditions

Currently, I am engaged in long polling (ajax) and have a looping portion of code that is part of an internal messaging system. When a message arrives, a specific area of the page will start blinking to notify the user. If the user checks the message, the ...