Refresh is required for resizing

My goal is to have 3 div elements scroll over each other, with their positions fixed when they reach the top. Everything works fine until the window is resized, requiring a page refresh. Is there a way to achieve this without refreshing the page?

Using the resize function didn't work for me, and I'm unsure if that's the correct approach.

Here is my code on JSFiddle:


var $cache = $('#two');
var $cache2=$('#three');

var vTop = $cache.offset().top - parseFloat($cache.css('margin-top').replace(/auto/, 0));
var vTop2 = $cache2.offset().top - parseFloat($cache2.css('margin-top').replace(/auto/, 0));

$(window).scroll(function (event) {
    var vTop = $cache.offset().top - parseFloat($cache.css('margin-top').replace(/auto/, 0));
        var vTop2 = $cache2.offset().top - parseFloat($cache2.css('margin-top').replace(/auto/, 0));
});

$(window).scroll(function (event) {
    var y = $(this).scrollTop();
    if (y >= vTop) {     
      $cache.addClass('stuck');
      $('#one').addClass('stuck');
      $('#two h2').addClass('stuck');     
    } else if(y>=vTop2)
    {
         $('#two h2').removeClass('stuck');
    }
      else {
      $cache.removeClass('stuck');
      $('#one').removeClass('stuck');
       $('#two h2').removeClass('stuck');       
    }
});

Answer №1

Implement window.onresize for managing elements:

window.onresize = function(event) {
//code for handling goes here
}

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

Stop iframes on iOS from automatically adjusting their height based on the content within them

Update: I recently discovered that Apple quietly prohibits fixed-size iframes in iOS. How frustrating! What can be done to make an IFrame responsive in iOS Safari? I am facing a seemingly straightforward task: embedding a fixed-size <iframe> within ...

Display or conceal div elements using JavaScript

Is there a way to hide certain divs when clicking on a specific div box? Clicking on the box will trigger the hiding of some application divs. See the first image below: When the boxes are hidden, the other divs will readjust in place of the current divs ...

Submit your information discreetly

Is there a way to submit a form to an external website without it causing the page to reload? I am working on automatically logging users into their Google account without interrupting their browsing experience. The following code snippet allows me to pr ...

What is the process for implementing a Content Security Policy to enable the loading of external JS files from a CDN in ExpressJS?

When working with ExpressJS, the HTML file is loaded in the following manner: app.use(express.static(__dirname + '/src/templates/')); Within the HTML file, here is an example of a meta tag containing Content Security Policy: <meta http-equiv= ...

Incorporating AngularJS to show sections of a webpage in real-time

, I am working on an angular js application in which I'm using rest services to retrieve http response data. My goal is to display different parts of the webpage conditionally based on the response data. For example, if the data in angular indicates " ...

Web Browser cross-origin AJAX requests

Recently, I developed an Internet Explorer extension that injects a script every time a page is accessed. The purpose of this script is to send an AJAX request to my localhost. However, I have encountered a roadblock where the request only works if it&apos ...

JavaScript's getElementsByName() method allows for easy access and

Excuse my lack of experience... I am attempting to run a javascript using the getElementByName method, with the goal of entering 0 in the quantity field on a particular site after 15 seconds of arriving there. Upon inspecting the quantity field, here is w ...

Gorgeous stew, precise match when using the "findAll()" function

Currently, I am utilizing Python (3.5), Selenium (3.6), and Beautiful Soup (4.6) to scrape a website. The code snippet I have implemented to locate a specific HTML tag is as follows: descContainer=descContainers[0].findAll("div", {"class":"userHtml"}) Re ...

Removing a background by linking a CSS file

Issue Resolved: I have found the solution. The problem was that the css document still contained html tags. Thank you to everyone who provided assistance. While designing a website using css, I encountered a situation where the site worked perfectly when ...

Angular HttpClient request fails to initiate

Overview: A button click on a form triggers the methodForm within the component. methodForm then calls methodService in the service layer. methodService is supposed to make an HTTP POST request. Problem: The HTTP POST request is not being made. However, me ...

Unlocking the power of async/await in jQuery sortable for receiving and updating items

Utilizing sortable's 'receive' feature allows me to receive elements from a draggable list. Upon receiving, the element's data is sent to the server via an Ajax call, creating an entry in the database. Subsequently, an id is returned an ...

What is the best way to keep the position of divs fixed when resizing the page?

I am currently designing a layout for a Tic Tac Toe game and I want the square blocks to remain in the same position even when the screen size is adjusted. The tools I am using are Bootstrap 4 and SCSS. 1) How can I ensure that the square blocks stay fix ...

The functionality of jQuery mouseenter and mouseleave events seems to be malfunctioning when applied to newly injected DOM elements

I encountered an issue with my code. It functions properly, but when I introduce new elements into the dom, it fails to show or hide them. jQuery("div.cat-icon").on({ mouseenter: function () { jQuery(this).find('.catselect').show( ...

Styling specific header cells in jQuery DataTable with C#CSS, focusing solely on title headers rather than cell data in the columns

Below are the details of my development environment: - Microsoft Visual Studio Professional 2013 - .NET Framework 4.0 - jQuery-2.1.4.min.js - jQuery DataTables 1.10.7 - Newtonsoft.Json.7.0.1 - jQuery UI 1.11.2 <table class="table mb-none" id="Inf ...

Change the background color of a Bootstrap dropdown when toggled

I recently created this code snippet: <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle buttonForProfile" type="button" id="dropdownMenuButton" data-bs-toggle='dropdo ...

Perform a replacement with jQuery.replaceWith() on an input field and refocus in Microsoft Internet Explorer

There exists a script http://gist.github.com/457324 which establishes default text (extracted from the element's title attribute) for empty input[type=text] as well as input[type=password] elements. However, setting default text for password elemen ...

Using the v-for loop to generate radio inputs results in selecting multiple radio buttons with identical values

Having a bit of trouble explaining, I've developed a basic rating system that utilizes input type ="radio" for an array of products. By using v-for="product in products", I iterate through the array and display all the products on the webpage. ...

Restrict a class to contain only functions that have a defined signature

Within my application, I have various classes dedicated to generating XML strings. Each of these classes contains specific methods that take input arguments and produce a string output. In order to enforce this structure and prevent the addition of methods ...

Eliminate any blank spaces and unproductive searches by utilizing AJAX and CodeIgniter

I am having an issue where I receive results from the database using a jQuery Ajax function when I enter only spaces. However, if I enter a space and then remove it, all results are returned from the database. Can someone please help me fix this problem? H ...

exploring for a keyword on the primary Amazon platform (analyzing)

Hey everyone, I noticed that Amazon's main search bar code looks like this: <input type="submit" class="nav-input" value="Go" tabindex="7"> I'm considering creating a function that will locate this tag on amazon.co.uk and perform a search ...