The page's scroll bar automatically moves downward as I adjust the size of the window

Exploring the world of creating a "sticky" navigation bar and menu using only the properties of "position" and "float", alongside JavaScript, has been on my agenda lately. This exercise serves as a great way to enhance my JavaScript skills.

Everything seems to be functioning smoothly, except for a minor bug when resizing the page causing positions to become non-static. To replicate this issue, one must first "run the code snippet" then click on "full page". Proceed to scroll down until both the "menu" and "sidebar" positions become "fixed". Now, upon resizing the page, you'll notice an automatic scroll downward.

(function(){

    function makeSticky(element){
        var rect = element.getBoundingClientRect();                                     
        var top = rect.top + scrollY;         

        var fakeElement = document.createElement('div'); 
        fakeElement.style.width = rect.width + "px";                      
        fakeElement.style.height = rect.height + "px"; 

        var offset = parseInt(element.getAttribute('data-offset') || 0, 10);   

        var hasDataConstraint = element.getAttribute('data-constraint');
        if(hasDataConstraint != null) {var constraint = document.querySelector(hasDataConstraint);}
        else {var constraint = document.body;}
        
        var constraintRect = constraint.getBoundingClientRect();
        var constraintBottom = constraintRect.top + scrollY + constraintRect.height - offset - rect.height;                 
        


        function onResize(){                                     
            element.style.width = "auto";      
            element.style.position = 'static';               
            fakeElement.style.display = "none";       
            rect = element.getBoundingClientRect();
            top = rect.top + scrollY; 

            fakeElement.style.width = rect.width + "px";
            fakeElement.style.height = rect.height + "px";
            fakeElement.style.display = "block";

            constraintRect = constraint.getBoundingClientRect();
            constraintBottom = constraintRect.top + scrollY + constraintRect.height - offset - rect.height; 

            onScroll();
        }
        

... (remaining code unchanged)
...

Answer №1

The solution to the issue is to add the line "fakeElement.style.display = 'block';" at the conclusion of the function following onScroll().

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

Incorporate an external object not native to the Angular framework within a factory

We're in the midst of a debate and I'm hoping you can help us reach an agreement. Imagine I have a basic factory set up like this: angular.module('myModule', []) .factory('Fact', function() { var Fact = function() { ...

Change the ng-model of the initial controller when a button is clicked in a separate controller

I am working on an AngularJS application that has two controllers. I am facing an issue where I need to update the ng-model of an input field in the first controller by clicking a button in the second controller. Accessing the $scope of the first controlle ...

Transform an array into a string with spaces in between each value by combining the elements

I have a complex select input on my website. By using jQuery, I can easily extract the selected values of all child option elements like this: $("#select").val(); This action returns an array as demonstrated below: ["Hello", "Test", "Multiple Words"] N ...

Leveraging the power of ReactJS alongside Google Tag Manager

Looking for a way to track my application using Google Tag Manager, I stumbled upon a popular package at https://www.npmjs.com/package/react-google-tag-manager. However, despite following the instructions, I am having trouble configuring it properly! Foll ...

Is there a way to implement a local gltf loader in three.js to display on a GitHub server within my repository?

Every time I run code on my VS app, it works perfectly fine. However, when I try running it on GitHub, it shows an error 404 and gets aborted. I came across some documentation regarding this issue, but I'm having trouble understanding it. Check out ...

Look through the contents of each child within a div to locate specific text, then conceal any that do not include it

I want to dynamically hide divs that do not contain the text I specify. Here is the code I have tried: var $searchBox = $('#search-weeazer'); $searchBox.on('input', function() { var scope = this; var $userDivs = $('.infor ...

Select a date using the jQuery calendar feature in Cypress

In my application, I am utilizing a jQuery calendar plugin with the following code snippet. Additionally, I have included Cypress code to select the date 1990-10-11. $(function() { $("#datepicker").datepicker({ changeMonth: true, changeYear: ...

What is the best way to incorporate navigation options alongside a centered logo?

Looking to create a unique top header layout with the logo positioned in the center and navigation options on both sides. Below is the initial code, but unsure how to proceed further. Any suggestions on achieving this design? <div class="header"> ...

Looking for guidance on restructuring a JSON object?

As I prepare to restructure a vast amount of JSON Object data for an upcoming summer class assignment, I am faced with the challenge of converting it into a more suitable format. Unfortunately, the current state of the data does not align with my requireme ...

View the Outcome of the Latest Form Submission on a Fresh Page

I have created a form that stores input values in a database table. Now, I want to display these results on another page once the form is submitted. However, I am struggling with how to retrieve and display all the values from the database record on the ne ...

Detect both single and double click events on a single Vue component with precision

I did some online research but couldn't find a clear answer. However, I came across this article -> Since I didn't quite understand the solution provided, I decided to come up with my own inspired by it. detectClick() { this.clickCount += ...

A guide to setting defaultValue dynamically in React Hook Form

Presently, I am facing an issue with editing the Product page. The props values are fetched through an API and sent from the parent component. However, I am struggling to set this value to my Datepicker input. This is because the defaultValue function from ...

Determine the upcoming Saturday's date by utilizing a stored procedure in Snowflake

Looking for assistance in retrieving the upcoming Saturday date based on a date field in a table using a Snowflake JavaScript stored procedure. Any suggestions would be greatly appreciated. Running the following query in the Snowflake console provides the ...

Searching in React can be done dynamically by referencing an array, similar to how the "LIKE" function works in SQL

Currently, I'm tackling an issue with a dropdown feature. My goal is to have the dropdown results sorted when the user enters something into the input field. The values in the dropdown are sourced from an array. For instance, codeList = [N1, N2, N3, ...

How to disable the click handler of a div using only classes

Although I'm not an expert in HTML, I am eager to create a basic bootstrap button in HTML that can be enabled and disabled. My question relates to the following scenario: <div class="button" onClick=doSomething >My Button</div ...

Encountering an unusual issue: Unable to access undefined properties (specifically 'get')

I'm struggling to get the order history screen to display the order history of a specific user. Every time I navigate to the path, I encounter the error mentioned in the title. I double-checked the path for accuracy and made sure there are no spelling ...

Insert a new row into the table using jQuery right above the button

I am working on a table where I need to dynamically add rows in the same rowId upon clicking a specific button. For instance, when the "Add Row 1" button is clicked, a new row should be added under "Some content 1", and so on for other rows. <table i ...

In this scenario, why is the `position: fixed` property anchored to the document rather than the viewport?

I have a gallery set up with a custom lightbox feature that I designed. The lightbox is styled with position:fixed and top:0, so theoretically it should stay in the same position regardless of scrolling. However, I am experiencing issues where this is not ...

Leverage WooCommerce API in conjunction with Express.js

Here's the code snippet I've been working on: const express = require('express'); const router = express.Router(); const WooCommerceAPI = require('woocommerce-api'); const WooCommerce = new WooCommerceAPI({ ...

Tips for eliminating the flash of color upon website loading

Every time I visit my site at , there's a brief moment of grey before the site fully loads. Despite being a simple landing page, it seems to be taking longer than expected to load. I suspect this issue is caused by the combination of linear gradients ...