Changing the height using JQuery UI resizable() can also affect the width during resizing

Having an issue with resizing here. Using the JQuery .resizable() on a div with specific css:

div#resizable {
   float: left;
   border: solid 1px white;
   box-shadow: 0px 0px 0px 1px #F2F2F2;
   box-sizing: border-box;
   padding: 15px;
}

This div contains various other elements. Attaching the resizable like this:

$( "#resizable" ).resizable({ minWidth: 200, 
                              maxWidth: 597, 
                              minHeight: 240,
                              resize: widgetResizeListener 
                            });

The widgetResizeListener() currently just logs to console.

When trying to change the height by dragging the ui-resizable-s side, it automatically adjusts the width by approximately 30px. This occurs every time I begin dragging, causing the desired width to be lost. The same issue happens when adjusting the width and affecting the height.

Wondering if this is due to the padding of the div? Previously, without padding, these "jumps" did not occur. However, removing the padding is not an option as it serves a purpose. Is there a way to account for padding within JQuery resizable()?

Any assistance would be greatly appreciated. Thank you...

Answer №1

In my opinion, the issue arises from using both box-sizing: border-box; and adding padding. This combination seems to interfere with the resizable function.

You could experiment by reverting back to default box-sizing (box-sizing: content-box), or consider placing an inner container for the padding instead.

Answer №2

For situations where removing the box-sizing property is not feasible, I discovered an alternative solution that may be helpful. While it may not be the most elegant, it gets the job done. Although I have not tested the exact code, the concept involves adding padding widths while resizing and then reverting back when resizing stops. It's important to note that if the element does not have padding, you will need to add an if statement to determine if a padding value is set.

var padLeft, padRight, padTotal; // declare variables 

$('#resize').resizable({

start: function(event, ui){
   padLeft = this.css('padding-left').replace('px', '');
   padRight = this.css('padding-right').replace('px', '');
   padTotal = parseFloat(padLeft + padRight);
   ui.size.width = ui.size.width + padTotal;
},
resize: function(event, ui){
   ui.size.width = ui.size.width + padTotal;
},
stop: function(event, ui){
   this.css('width', ui.size.width - padTotal); 
}

});

Answer №3

Although this issue may be considered outdated, I recently encountered the same problem due to utilizing the Foundation library, which automatically applies box-sizing border-box to all elements. To resolve it, a simple adjustment in the jQuery UI resizable widget code is required. The solution lies within

_mouseCapture

I have implemented the following code snippet:

var padLeft = el.css('padding-left').replace('px','');
var padRight = el.css('padding-right').replace('px','');
var padTotal = parseFloat(Number(padLeft) + Number(padRight));

this.originalSize = this._helper ? { width: el.outerWidth() + padTotal, height: el.outerHeight() + padTotal } : { width: el.width() + padTotal, height: el.height() + padTotal};

Adjustments may need to be made to the height values, depending on whether the resizable was applied directly to the foundation container for each grid element or a nested div inside. This could involve including or excluding the height adjustment.

If you are interested in modifying the jQuery widget without altering the core library, you can refer to this resource for guidance, although I have not yet successfully achieved this:

How to extend a jquery ui widget ? (1.7)

I am encountering errors in the subsequent functions of the resizable object. Any suggestions on resolving this issue would be greatly appreciated.

Answer №4

When dealing with situations where box-sizing:border-box is necessary, we have found a unique solution to address this issue.

Within the resizeable callback implementation: 1. We switch to box-sizing:content-box during the start phase 2. Reverting back to box-sizing:border-box during the stop phase

This approach involves temporarily changing the box-sizing property to content-box while resizing in order to prevent any disruptive jumping behavior that may occur when border-box is utilized.

Answer №5

If you encounter this issue, utilize the 'handles' parameter to resolve it. Here is an example:

$( "#resizable" ).resizable({handles: 'e,w'});

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

Tips for successfully passing the event in a jQuery function

I currently have four buttons on my HTML page. Create Airport Create City Create City Shortname Create Airport Shortname All of these buttons will trigger an AJAX call to the same page. Here is a snippet of the code for one of the buttons: $('#im ...

Responsive breakpoints in TailwindCSS seem to have an issue with applying padding and margin styles properly

Currently, I am tackling a project that involves creating a responsive design with Tailwind CSS on nuxtjs. Has anyone encountered the issue of py-8 applying to both breakpoints? If so, please share your insights! Here is how I have structured the compone ...

Issues with loading content in jQuery's ajax() function

Having an issue with the ajax() function in jQuery. It seems like a simple problem, but I'm struggling to figure it out. My goal is to load content from another HTML file using ajax and here's the code I have so far: $(function(){ $('.submi ...

Symfony2: Making AJAX request that unexpectedly returns complete html page

I am facing an issue with setting up a basic AJAX request in Symfony2. It appears that the controller is not receiving the request as expected. Instead of displaying '123', the AJAX response shows the HTML content of the current page (index.html. ...

Using JQuery to create an animated slideToggle effect for a multicolumn list

I have a large list where each li element has a width of 33%, resulting in 3 columns: computers monitors hi-fi sex-toys pancakes scissors Each column contains a hidden UL, which is revealed through slideToggle on click. JQuery $('.subCate ...

Highchart displays text centrally on legend hover

I am struggling with the code provided here. My goal is to make text appear in the center of the donut chart when hovering over the legend. Similar to how it works when hovering over a single piece of the donut chart. var chart = new Highcharts.Chart ...

Steps for adding an onclick function to a collection of div elements

My current project involves utilizing the Flickr API and I am interested in creating a popup div that opens when clicking on each image. The goal is to display the image in a larger form, similar to how it's done using Fancybox. I'm looking for ...

Arrange a pair of div containers side by side on the webpage without the need to alter the existing

Is there a way to align these two div side by side? <div class="main_one"> <div class="number_one">Title A</div> </div> <div class="main_two"> <div class="number_two">Title B</div> </div> <div class=" ...

What is causing the spinner with CSS rotation transform to bounce rhythmically?

In my design, I have created a simple Spinner icon in Figma. It's meant to be perfectly aligned at the center of an enclosing frame that has dimensions of 64x64. To achieve the rotating effect, I applied CSS rotation as shown below: let rotation = ...

Is there a way to deactivate specific options within the "Select" component on Material-UI, similar to how it is done in the "Autocomplete" feature?

Is it possible to restrict certain options in the Select component similar to how it is done in the Autocomplete component? PS: The options are present in an array. <FormControl variant="outlined"> <InputLabel>States</Inp ...

Utilizing Struts 2 to Manage HTTP Array Parameters through Ajax Requests

Struggling with sending array parameters to a Struts 2 action class using struts 2.1.8.1. Check out this snippet of code: public class MyAction extends ActionSupport { private String[] types; public String execute() { return SUCCESS; ...

Issues with the Content Editable Functionality

While working on my website, I encountered a strange issue. For some reason, I can't seem to get the contenteditable="true" attribute to work on the heading with the ID "hl". It would be awesome if someone could help me figure out how to mak ...

Is there a way to create a PHP function that can process an AJAX request and return a boolean value?

After some testing, I discovered that when the code snippet below is executed within my PHP function to manage an AJAX call: session_start(); if ( $_POST['animal'] != $_SESSION['animal'] ) die(json_encode(false)); Upon returning to th ...

I am experiencing the issue of unexpected borders showing up on the HTML/CSS page that I designed

Greetings, amazing community at StackOverflow! I recently completed an HTML/CSS Project as part of a Codecademy Intensive course. If you're curious to see the project in action, feel free to check it out on GitHub by visiting this link. One thing I ...

Guide to positioning elements in CSS

I'm struggling to get all the elements to align in a single row. I've tried using display: inline-block, but it's not working as expected. I'm working with DataTables and I want the button images to be aligned with the page number box. ...

Unable to generate a vertical navigation bar

When attempting to create a vertical menu, the final result doesn't align as expected. https://i.stack.imgur.com/2ok47.jpg This is the current code being used: $("#example-one").append("<li id='magic-line'></li>") ...

Determine if a div contains an svg element with JavaScript

My question involves a div containing a question mark and some SVG elements. Here is the initial setup: <div id="mydiv">?</div> When validating a form submission in my code, I check if the div text contains a question mark like this: const f ...

Colorful radial spinner bar

I am interested in replicating the effect seen in this video: My goal is to create a spinner with text in the center that changes color, and when the color bar reaches 100%, trigger a specific event. I believe using a plugin would make this task simpler, ...

Switch the secondary menu to be the main menu on all pages excluding the homepage in WordPress

I am looking to customize my menu display by showing the primary menu only on my home page (front-page.php) and displaying the secondary menu on all other pages except for the home page. In my functions.php file, I have registered both the primary and sec ...

Unpredictable jQuery modal form activation occurring twice

I have integrated Dropzone.js into my website for uploading images, and everything works smoothly during the upload process. However, I encounter a strange issue when attempting to delete the images. Occasionally, when clicking on the "trash" icon to delet ...