jQuery's draggable and resizable functionality with containment provisions is designed to

I'm struggling to create a resizable and draggable div using jQuery, but it's proving to be quite buggy in its implementation.

Despite finding similar questions, I haven't come across a solution yet.

  • How can I fix the containment bug when using resizable() and draggable() together in jQuery?
  • Resizing a JQuery Draggable element's containment parent while dragging

I've tried different position settings and experimented with the refreshPositions option, but nothing seems to work.

The issues I'm encountering include:

  • Moving the child item quickly (especially in circular movements) often causes it to break out of containment.
  • Dragging the child to the bottom right corner and repeatedly trying to drag it out of the corner gradually breaks the containment further with each iteration.
  • Once the containment is broken, you can continue dragging into that area on subsequent drags.

I've provided a simplified example which reproduces the problem consistently across all browsers, although the demo may not work well in IE 10 and Opera.

$(".child")
    .draggable({
    containment: "parent",
    cursor: "move"
}).resizable({
    handles: "se",
    containment: "parent",
    minHeight: 50,
    minWidth: 50
});

For a complete yet straightforward demonstration, check out this link.

http://jsfiddle.net/jxY8M/

Any suggestions on how to resolve this containment issue would be greatly appreciated.

Answer №1

There seems to be a glitch with the UI draggable feature itself, but it has already been documented here

To address this issue (particularly in the latest version of Chrome), you can try the following solutions:

  1. Add a 5px padding to the parent element, see demonstration here
  2. Add a 5px border to the parent element, see demonstration here
  3. Add a 5px margin to the child element, see demonstration here
  4. Combining any of the above methods for a 5px increase
  5. Add overflow:hidden to the parent element, see demonstration here. Strangely enough, this workaround appears to resolve the issue effectively

Note that the 5px adjustment may vary depending on your specific case. Experimentation may be required to determine the optimal value for your elements. In some instances, I observed that less padding or border width was needed, possibly influenced by the size of the elements. However, I confirmed that the fifth solution worked smoothly in their examples as well.

Answer №2

Utilizing the stop function within the draggable interaction, you have the ability to adjust the resizable parameters effectively. This method proved beneficial for me when encountering similar issues while trying to set containment on both resizable and draggable interactions.

It's worth noting that this approach also works seamlessly with a locked aspect ratio on the resizable interaction.

Example:

<div id="container" style="width: 320px; height: 240px; background-color: #000000;">
    <div id="subject" style="width: 240px; height: 180px; background-color: #ffffff;">
    </div>
</div>

<script type="text/javascript" language="javascript">
jQuery(document).ready(function() {
    jQuery("#subject").resizable(
    {
        aspectRatio: 4 / 3,
        minHeight: 120,
        minWidth: 160,
        maxHeight: 240,
        maxWidth: 320
    }).draggable(
    {
        containment: "#container",
        stop: function(evt, ui) {
            var container = jQuery("#container");
            var subject = jQuery("#subject");

            var containerPosition = container.position();
            var subjectPosition = subject.position();

            var relativeLeft = subjectPosition.left - containerPosition.left;
            var relativeTop = subjectPosition.top - containerPosition.top;

            var maxWidth = (container.width() - relativeLeft) | 0;
            var maxHeight = (container.height() - relativeTop) | 0;

            subject.resizable("option", "maxWidth", maxWidth);
            subject.resizable("option", "maxHeight", maxHeight);
        }
    });
});
</script>

Answer №3

Fixing a containment bug in jQuery when using resizable() and draggable() together: Even though this thread is closed, the solution provided recommends adding position:relative to the parent or container element.

Interestingly, using overflow:hidden did not yield positive results for one user (specifically in Firefox).

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

How to retrieve the type of a computed keyof T as a generic type within TypeScript

I am working with two different interfaces: interface PersonRequirements{ user:string, password:string, id:number } export interface Requirement<R> { name: keyof R & string, save: () => any,/* I want this return type to be ...

Steps for modifying a cell within a table using a button click

Hello, I am currently working on a project where I want to display a specific div portion when a user clicks on an image icon within a table. However, I am encountering an issue with the JavaScript code as it only shows the div portion in the first row and ...

Creating a compact array from a larger array in JavaScript

I am currently using the jquery.bracket library and I am looking to split a large array into pairs like ["'Team 1', 'Team 2'"],["'Team 3', 'Team 4'"] from var all= ["'Team 1', 'Team 2'","'T ...

Safari in iOS8 experiencing problems with -webkit-overflow-scrolling: touch;

Our team has been developing a web app for a client that functioned perfectly on iOS7 in Safari. However, upon testing the app on iOS8, some significant bugs were discovered. After a user opens and closes the sidebar, content within a <section style=" ...

Unable to access Bootstrap nav link

There seems to be an issue with a link inside the Bootstrap nav-menu. The link is not opening when clicked on. I am facing trouble with the Cart file as it does not open when I click on the Cart hyperlink. I have double-checked the path and it appears to ...

What is the method for graphing data points that include two different y values?

I have been on the lookout for a solution to this question for quite some time now. While there are a few options that come close, I am yet to find the perfect answer. My goal is to create a line chart displaying water levels over time. I would like the d ...

How can I create two left-aligned columns in CSS, with the first column being fixed in place?

Is there a way to create two columns without using HTML tables? I'm looking for the first column to contain an image and the second column to display text aligned left, extending to the rest of the page width. I found this CSS solution for the first ...

Is it possible to organize and filter a dropdown menu in JQuery based on a secondary value (new attribute)?

Can the drop-down list be sorted by value2? <select id="ddlList"> <option value="3" value2="3">Three</option> <option value="1" value2="1">One</option> <option value="Order_0" value2="0">Zero</option> </sele ...

Guide to obtaining ngPrime autocomplete text when the button is clicked within Angular 6

I am currently utilizing the ngPrime autocomplete feature to retrieve data from a service. My goal is to capture the text entered in the autocomplete field whenever a button is clicked. I attempted to access the value using getElementById.value within th ...

The menu shifts position or overlaps next to the current active menu option

It would be helpful to take a look at the URL provided (http://ymm.valse.com.my/) in order to understand my question better. Upon clicking on any menu item, I noticed that the menu next to the active tab overlaps. I believe the issue lies in the width pro ...

I am experiencing an issue with the Jquery Datepicker not appearing within a Bootstrap modal pop-up in an ASP

The Jquery Date function is not displaying in the popup model. Here is the code I am using: <div class="modal fade" id="DateModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-di ...

At what point in the lifecycle of my component am I able to begin using this.$refs?

Exploring ways to integrate HTML5 audio events while initializing a Vue.js component that consists of a single file: <template> <audio ref="player" v-bind:src="activeStationUrl" v-if="activeStationUrl" controls autoplay></audio> < ...

Guarantee the successful execution of a server-side function using my client-side function

I am currently in the process of creating a website that utilizes Javascript and Asp.net. My code contains numerous functions on the client side, within my html code, while my server side functions are called using a webservice. How can I ensure that my c ...

Converting a Javascript array to an NSArray in Xcode

As a complete beginner to Xcode and programming in general, I recently built an app using javascript and html and integrated it into Xcode. Currently, my main focus is on extracting multiple arrays from the html/javascript file and exporting them as a csv ...

Left-align elements within a div container that is centered

I want to left-align my elements within a div container that is centered. I used the text-align property to center the overall div, but now I'm having trouble aligning another content container's text to the left within the center-aligned div. H ...

Unable to establish a connection with the docker container

I have developed a server-api application. When running the app on my localhost, only two simple commands are needed to get it up and running: npm install npm start With just these commands, the app runs perfectly on port 3000. Now, I am trying to dock ...

The navigation pills in Bootstrap 5 are experiencing functionality issues

Hey everyone, I'm currently tackling a new project and running into an issue with Bootstrap 5 nav pills. I need them to toggle between column and grid view, but they aren't behaving as expected (content displays fine on the first pill, but nothin ...

Modify the website address and show the dynamic content using AJAX

$(function(){ $("a[rel='tab']").click(function(e){ //capture the URL of the link clicked pageurl = $(this).attr('href'); $("#Content").fadeOut(800); setTimeout(function(){ $.ajax({url:pageurl+'?rel=tab&apo ...

Unable to see my Vue component within Laravel

My Vue component is not appearing on the screen and I am unable to identify the issue. I have checked my console for errors and I am using npm run hot Here is my app.js file: require('./bootstrap'); window.Vue = require('vue').default ...