Adjust the minimum height and width when resizing the window

Apologies in advance if this has already been addressed, but I have tried solutions from other sources without success.

My Objective:

I aim to set the minimum height and width of a div based on the current dimensions of the document. This should trigger whenever the document window is resized.

What I've Attempted:

$(document).ready(function() {

function reset_dimensions() {
    doc_height = $(document).height();
    doc_width = $(document).width();
    $(".flicker-div").css("min-width", doc_width + "px");
    $(".flicker-div").css("min-height", doc_height + "px");
    alert("From - Function" + doc_width + "x" + doc_height);

}

    reset_dimensions(); 
    $(window).resize(function() {
        reset_dimensions();

    });

});

The Issue at Hand:

Upon initial load, correct dimensions are displayed. However, manual window resizing reveals two noticed problems:

1. The function is called twice, leading to multiple alert boxes. 2. The captured document height remains unchanged. 3. The captured document width increases rapidly, with values not being discarded as they appear static in subsequent runs.

Test Run Output:

On Load:
1349x626
On Resize:
1369x3130
1389x15650

I am convinced that I have overlooked something simple here. Any assistance in resolving this issue would be greatly appreciated.

Answer №1

It seems like the error lies in your method of capturing the height of the WEBPAGE rather than the browser window.

You are adjusting the size of the window but anticipating a change in the document size.

Revise your code to utilize $(window).height() and $(window).width() for accurate measurements.

Answer №2

First thing to note is that you do not need to add 'px' when specifying the unit of measurement.

If you reload your browser after refreshing, the code should start working correctly. Right?

A few days back, I faced a similar issue and realized it was due to using document.width/document.height.

Do you think it's achievable by using window.width/window.height instead?

The function gets called every time you resize the window, but you can set a timeout to ensure it only executes once. You can use the timeout plugin mentioned here:

Answer №3

Consider restructuring your code to enhance its efficiency and avoid potential issues. It is not necessary to nest function definitions within the onready function, which may impact the scope of the function and garbage collection process. Define your functions separately before the onready event to ensure proper execution. Furthermore, removing alerts from certain locations can prevent browser crashes caused by rapid event firing. Instead, consider utilizing $(window).width() to monitor changes in document size, especially when dealing with padding or margins.

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

Text box input that displays the latter portion before the initial part

Looking for assistance with showing the end of the entered text rather than the beginning in an input field. For example, the input could be "Starting portion, Ending Portion". If the input field is limited to 14 characters, instead of displaying the firs ...

How to create a stunning Bootstrap Jumbotron with a blurred background that doesn't affect the

I need help making my Jumbotron image blurry without affecting the text inside it! Below is the code from my index.html and style.css files. Any assistance would be greatly appreciated. body { background-image: url(bg1.png); background-repeat: repea ...

Anchor link leading to an incorrect location

Sorry for the potentially silly question, but I'm struggling to figure out what's happening here. I'm currently working on a website and trying to create an anchor link at . I've placed the anchor just before in this code: <a name ...

Error in Webpack: JSX elements that are adjacent must be enclosed within a wrapper tag

After adding a new component and integrating it into my Main component, I encountered an error when running webpack. The error message displayed was: "Adjacent JSX elements must be wrapped in an enclosing tag" Below is the snippet of code where the iss ...

When a tooltip inside a button is clicked, the hover effect is passing through to the surrounding parent element

I am facing an issue with a nested tooltip within a button. The problem arises when I try to click on the 'read more' link inside the tooltip popup, intending to go to an article. However, instead of redirecting me to the article, clicking on the ...

Guide on extracting unique identifiers from an array of objects and sorting them by the earliest date in JavaScript

I've got an array of objects and I'm looking to retrieve the items with unique IDs while also selecting the earliest date. For example: [{id:1, date: Jan 12}, {id:2, date: Feb 8}, {id:3, date: Feb 8}] var array = [{id: 1, date: Jan 12 2021 08:00 ...

including a code snippet within a dropdown menu or embedding it in a clickable button

Hey there, my name is Wouter Sanders and I am currently learning to code! I recently finished creating a RAL color picker for a project I'm working on. The only issue I've run into is trying to embed the code in a menu or button so that it doesn ...

Discovering the specific marker that was clicked from a group of Google map markers

I have a collection of marker objects named markers. I am currently utilizing a for loop to assign event listeners to each one. However, I am encountering difficulty in determining which specific marker was clicked. This is the current code snippet I have ...

Activating gzip compression using fetch.js

I am utilizing fetch.js (https://github.com/github/fetch) to transmit a rather substantial JSON object to the backend. The size of the JSON is significant due to it containing an SVG image string. My question is whether fetch.js applies gzip compression a ...

What are the steps for retrieving a JSON object within an array?

var jsonData = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue" :{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text" :"Size32","price":"22","isSelec ...

Necessary form group in html [Angular]

Is there a way to make certain form fieldsets required while allowing the user to choose which inputs to fill? My idea: Users can select what they want to fill out without having to complete all of the fieldset inputs. I have these two options: 1: < ...

Eliminating empty space within my container

I am facing an issue with a container that contains an image and a button on top of it. There seems to be some extra space created within the container on the right side which is taking up more space than necessary. The layout consists of three horizontal ...

What is the best way to navigate back to the top of the page once a link has been clicked?

One issue I'm facing is that whenever I click on a link in NextJS, it directs me to the middle of the page: <Link href={`/products/${id}`} key={id}> <a> {/* other components */} </a> </Link> I believe the problem l ...

SQL table not being updated by AJAX request data submission

I'm encountering an issue when trying to update a SQL table with form data sent via a JQuery AJAX POST. Even after using mysql_error(), no errors are being returned. As a troubleshooting step, I appended "-Test Test" to my textarea input. This addi ...

Reset the text input field when the dropdown menu is set to 'no/other'

CURRENT: Choosing from a dropdown menu with options 'Yes' or 'No'. If 'Yes' is chosen: Display additional dropdowns/inputs for entry If 'No' is chosen: Conceal additional dropdowns/inputs WANT: I am looking to imp ...

In client.js, learn how to retrieve the value of an input tag (Name) from a login form

Let me explain the situation at hand: I'm currently in the process of developing a chat application using socket.io and NodeJS (Express). Initially, I had a simple prompt that asked for the user's name, easily accessible through const name = pro ...

ReferenceError: _jquery2.default.ajax is not a function" encountered in a React Native application

I have been attempting to use jQuery to retrieve xml data from an internal website. My expo project setup is quite basic and resembles the following: import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; imp ...

Is it a good idea to show a loading image for slow php/mysql results?

I need some help with my website that is powered by php/MySQL for querying and displaying results. The database has grown to over 40,000 records, causing the query to slow down. I am looking for an efficient way to show a loading image while the page loads ...

Nuxt Page Featuring One Exclusive Product

I am just getting started with Nuxt and I'm in the process of creating a Single Product. I have some questions: How can I generate multiple pages using SSR and create a unique HTML for each page? Should CSR be developed first before implementing SSR, ...

The simple passport.js sign-up feature is not successful as it mistakenly believes that the username is already in

Currently, I am working on setting up a basic signup feature for my Node + Express + Sequelize application using passport.js. The database is empty at the moment, and I am utilizing the passport-local strategy to extract the user's email and password ...