Stop jQuery popups from wrapping text when resizing the browser window

Whenever a link is clicked, a jQuery popup appears. The popup functions properly in terms of opening and closing, but I would like to ensure that its position remains fixed when resizing the browser window to avoid any wrapping issues.

Typically, I use a small snippet of jQuery code to prevent browser window resizing problems; however, it seems to be ineffective in this specific scenario.

The following is the jQuery code used to prevent wrapping:

<script src="http://wcetdesigns.com/assets/javascript/jquery.js"></script>

<script>

$(document).ready(function() {

$("[class~='floatNoWrap']").each(function () {
$(this).css("width", $(this).outerWidth());
});

});

</script>

This section contains the jQuery code for the popups:

$(document).ready(function () { 

// Opening Popup
$("#pop").click(function(){
$("#overlay_form").fadeIn(1000);
positionPopup();
});

// Closing Popup
$("#close").click(function(){
$("#overlay_form").fadeOut(500);
});

// Opening Popup 2
$("#pop2").click(function(){
$("#overlay_form2").fadeIn(1000);
positionPopup();
});

// Closing Popup 2
$("#close2").click(function(){
  $("#overlay_form2").fadeOut(500);
});


// Positioning the Popup at the Center of the Page
function positionPopup(){
if(!$("#overlay_form, #overlay_form2").is(':visible')){
return;
}
$("#overlay_form, #overlay_form2").css({
left: ($(window).width() - $('#overlay_form, #overlay_form2').width()) / 2,
top: ($(window).width() - $('#overlay_form, #overlay_form2').width()) / 3,
position:'absolute'
});
}

// Maintaining the Popup at the Center of the Page When Browser Resized
$(window).bind('resize',positionPopup);

});

This segment provides the CSS styles for the popups:

#overlay_form, #overlay_form2{
font-weight:bold;
border: 2px solid;
margin: 10px 70px;
padding:35px 35px 228px 70px;
background-repeat: no-repeat;
background-position: 10px center;
width:600px;
color: #4F8A10;
background-color:#EDFCED;
position:absolute;
}

#pop, #pop2{
display: block;
border: 1px solid gray;
width: 65px;
text-align: center;
padding: 6px;
border-radius: 5px;
text-decoration: none;
margin: 0 auto;
} 

Answer №1

To fix the floating popups, use the following code snippet:

jQuery:

// Center the popup on the page
function centerPopup(){
if(!$("#overlay_form, #overlay_form2").is(':visible')){
return;
}
$("#overlay_form, #overlay_form2").css(("width", $(this).outerWidth())
);
}

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

What is the best way to save inputted names as properties of an object and assign the corresponding input values as the values of those properties?

I am searching for a way to dynamically build an object where each property corresponds to the name of an input field and the value of that property is the input's value. Here is the HTML structure: <form> <fieldset> ...

verify that the div has finished loading with ajax

I am working on a project where I have a div with the id #pageToLoad. This div loads a form from formPage.php. I want to ensure that the page is fully loaded in the div so that if any errors occur, I can reset the form using: $("#form").reset(); Can some ...

Unable to locate node module when using Npm.require

I've been attempting to utilize the Npm.require method in order to access the ldapjs module for client authentication. Unfortunately, I'm encountering the following error message: var ldap = Npm.require('ldapjs'); Error: Cannot find ...

Transforming nested tables into JSON format

I have a table that is dynamically generated and I am looking to convert it into JSON format. Despite trying several methods, I haven't been successful. Can someone help me by providing guidance on how to achieve this using jQuery? For your reference ...

"Enhance your website with jQuery for a seamless

Here is some code I am working with: <div id="gallery"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <img src="image3.jpg" alt="Image 3"> </div> I am looking to display a standard animat ...

Leverage Bootstrap 4 for achieving flexible layouts and centering content with two elements using flex and justify-content

By using the d-flex, justify-content-center, and flex-grow-1 classes, I was able to achieve my desired layout with just 3 divs. <div class="row bg-primary d-flex justify-content-center"> <div class="col-auto"> LEFT </div> < ...

Enhance iframe with hover effect

I'm currently working on a unique WordPress blog design where images and iframes (YouTube) transition from monotone to color upon hover. So far, I've successfully implemented the grayscale effect for images: img { -webkit-filter: grayscale(100% ...

Setting the width of an SVG element to match the width of the <text> element inside it

Within this JSFiddle project - https://jsfiddle.net/xtxd6r8t/ - there is an <svg> element containing text inside a <text> tag. Upon inspection of the <svg> and delving into the <text> element, it's noticeable that the width of ...

Arranging several lists in a row without any specific order

I am trying to arrange multiple <ul> elements on the same line to create a shop-like display for products. Currently, I have set up several unordered list tags: ul { display: inline; } li { list-style: none; } <ul> <li>& ...

Extending the Angular Date filter with translation capabilities

During the development of an application that allows users to select a date, I encountered an issue with the translations of the date filter. To resolve this, I implemented the following solution: Extended the date filter functionality Substituted the ...

What is the best way to paste plain text into a Textarea without any formatting?

A challenge I am facing in my project is related to copying and pasting text into a Textarea. When a user copies text, the style of the text is also inserted along with it. However, when the user manually types the text, it gets inserted correctly without ...

Chrome Extension: Passing a variable between Content Script and background.html

Struggling to understand how to transfer a variable or an array of variables from a content script to a background page. The goal is to identify specific DOM elements using the content script and then send them to the background page for a cross-domain XML ...

Capture a section of the body background to incorporate into the canvas space

As a newcomer to the world of canvas, I have been learning from various sources about how it works. However, my goal is more dynamic and unique than what I've seen so far. I am looking to create a body background for my webpage that is responsive, cen ...

Is there a way for me to choose all the classNames that conclude with a specific word within the MUI sx property?

I am currently working with MUI and I have a need to modify certain properties that are prefixed with random IDs. How can I target, for instance, the first one using: '& endsWith(MuiAccordionDetails-root)' I wish to achieve this because the ...

Removing a portion of an item with the power of RxJS

I possess the subsequent entity: const myObject = { items:[ { name: 'John', age: 35, children: [ { child: 'Eric', age: 10, sex: 'M' }, { ...

Tips for creating an HTML table that fills the entire width of its parent element:

One of the rows in my table contains a nested table. I want both tables to fill up 100% of the width of the parent element. How can I achieve this? Check out the demo here Here's the HTML code: <div class="container"> <table> <tr ...

Verifying that the mapDispatchToProps functions have been triggered using ReactTestingLibrary

I am currently utilizing mapDispatchToProps to invoke a dispatch function that calls an API within the useEffect hook of my functional component. I am facing some challenges when attempting to write unit tests for this scenario using React Testing Library ...

Determine the value of a field by utilizing the values of two other fields through an onChange event

Setting the Stage: Imagine a scenario with 3 key fields: quantity, singlePrice, and totalPrice. I want these fields to be part of my form, with totalPrice being dynamically recalculated whenever quantity or singlePrice changes. My Approach: I created ...

WARNING: Alert raised due to the discovery of two offspring sharing identical keys, `${item}-${index}`

I have been facing the issue of receiving an error message 'ERROR Warning: Encountered two children with the same key, ${item}-${index}' and I am not sure what is causing it. Can someone please guide me on how to resolve this problem? Any help w ...

Only allow scrolling if the number of child elements exceeds a certain limit

I am looking to implement a scroll feature on the <ul> element when the number of <li>s exceeds a certain threshold. For example, if we have 12 children, I want to display only 7 of them and then scroll through the rest. This is my current app ...