Is it true that JqueryUI resize and draggable functions are incompatible?

I am experiencing issues with using both jqueryui resize and draggable in the same element. How can I make them work together seamlessly? Currently, I am able to resize the image but unable to drag it as intended. The goal is to allow dragging of the image within a larger div without resizing it beyond the dimensions of the inner div. Can anyone provide guidance on how to successfully implement both draggable and resizable functionalities in the same element?

You can view the code in action on JSFiddle.

Here is the HTML structure:

<div id="container">
<div id="area">
    <div class="testi"><img class="imageHere" width="200" src="http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png" /></div>
</div>

CSS styling:

#area{
position:absolute;
top:67px;
left:75px;
border:solid 2px;
height:215px;
width:600px;
background-color:#FFF;
}
.imageHere{

}
#container{
position:relative;
float:left;
margin-left:50px;
border:solid 2px;
height:350px;
width:750px;    
}

Javascript code snippet:

$(function() {
 $( ".imageHere" ).draggable({ cursor: "move",containment: "#container"      }).resizable({containment: "#container"});
});

Answer №1

To make it work, ensure you set the resizable before the draggable.

$(function() {
     $( ".imageHere" ).
     resizable({
         containment: "#container"
     }).
     draggable({
         cursor: "move",
         containment: "#container"
     });
});

Check out this JSFiddle example

Answer №2

After dealing with a similar problem, I found a useful solution.

When you resize images, they are enclosed in a wrapper div. To make an image both resizable and draggable, you should make the wrapper element draggable.

$('img')
    .resizable()
    .parent()
        .draggable();

However, this approach has its limitations. Users shouldn't have to understand how resizable works in order to use draggable, and you need to make the image resizable before making it draggable. Additionally, if you were to remove the resizing functionality with .resizable('destroy'), the draggable feature would also be removed when the wrapper is deleted.

Answer №3

Is this the solution to your query:

$(document).ready(function() {
    $( ".testi" ).draggable(); 
    $( ".imageHere" ).resizable();
});

Check out the code on JSFiddle

Make sure to adjust your draggable to the parent div of the image.

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

In HTML, the <div> element will automatically receive a width setting without requiring any

I designed a maze that adjusts its background size based on the height and width values entered by the user. The background color of the maze object is set, but I want it to dynamically resize along with the width of the maze itself. How can I achieve this ...

Guide to adding items to an array in json-server using a JavaScript-created database

I built a JSON-server app that dynamically generates the database using JavaScript when it starts running. The data I create is organized within a 'users' object, as illustrated below: { "users": [ { "id": "user_id_0", ...

Put the title tag within a script in the shell

Within an HTML snippet, I currently have a <title> tag that I need to change on a monthly basis when generating reports. While I can manually update it each month, I am looking for a way to automate this process so the title reflects the relevant rep ...

sending an alert via a function's return statement

Could you explain to me why this code isn't functioning properly? When I call s.A, the alert message doesn't appear. Can you help me understand why? var s = { A: function () { alert("test A"); }, B: function () { alert("test B"); } }; ...

How can you convert Promise.all to a single Promise?

I have successfully implemented two API calls using Promise.all method with no issues. Even though one API call is sufficient to retrieve the results, I decided to stick with Promise.all and it's still working fine. I attempted to replace Promise.al ...

please transmit the id (or retrieve the id from the router path)

I have a basic blog built with React/Redux for the frontend, featuring user registration and articles. I ran into an issue when trying to pass the article ID to the editor component. The form is the same, but the paths differ for adding new articles and ed ...

Only update the iframe src when specific selections are made in the drop-down menu

My goal is to dynamically construct a URL based on the selections made in 4 form input fields. Two of these fields are select boxes, while the other two are text input boxes for entering search queries. I've written a function to handle this task. &l ...

Is it feasible to restrict access to specific fields of an object in the Firebase database for viewing purposes using Firebase permissions?

Consider this JSON structure representing a Firebase DB: { "users": { "user0": { "name": "Mike", "age": 20, "relationship": "married", "friends": [...] }, "user1": { " ...

Struggling to set the value for a variable within an Angular factory?

When dealing with a variable as an array, I have no trouble pushing objects inside and retrieving the values within the controller. However, when trying to directly assign an object to that variable, I run into issues. If anyone can assist me in achieving ...

The Reactjs application is failing to display the real-time data produced by the state

I am encountering an issue with looping through a JavaScript array of objects on a ReactJS component. Upon inspecting the Chrome browser console, the following output is displayed from console.log --- 0: {id: 1, title: "This is a post 1"} 1: {id: ...

Generating a clickable link from information pulled from a database and utilizing the get() method

Currently experimenting with a form I created and everything is functioning smoothly. My current objective involves retrieving specific rows from my database and presenting them as a summary on a separate page. However, I want these rows to appear as hyper ...

Hide the Modal Content using JavaScript initially, and only reveal it once the Onclick Button is activated. Upon clicking the button, the Modal should then be displayed to

While trying to complete this assignment, I initially attempted to search for JavaScript code that would work. Unfortunately, my first submission resulted in messing up the bootstrap code provided by the professors. They specifically requested us to use Ja ...

Angular controller fails to fetch scope variable in subsequent view

I'm currently working on implementing a login feature that directs a validated user to a personalized HTML view that greets them with their name. The initial view is a login page. When the user clicks the "Log In" button, the ng-click function is sup ...

What is the best approach for managing Create/Edit pages in Next.js - should I fetch the product data in ServerSideProps or directly in the component?

Currently, I am working on a form that allows users to create a product. This form is equipped with react-hook-form to efficiently manage all the inputs. I am considering reusing this form for the Edit page since it shares the same fields, but the data wil ...

Card Alignment with Bootstrap

I need help aligning the last two elements in the card. I want the first one (a button) to be on the left, and another element (a Div with multiple social icon images) on the right side. Can someone provide guidance on how to achieve this layout? <!D ...

Implementing a parallax scroll effect using CSS and dynamically updating the background-position value with JavaScript

How can different Y position percentages be dynamically assigned to multiple layered background images in CSS using JavaScript so that they update as the page scrolls? <style> body { background-image: url(img/bg-pattern-01.png), ur ...

Tips for utilizing the Apollo cache consistently during page transitions in NextJs

In the official examples repository of NextJS, I found this apolloClient.js file: import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' import { concatPagination } from '@apollo/client/utilities' import merge from &apos ...

Can you explain the function of this particular jQuery statement?

function setReplicate() { $('.replicate').live("click",function(){ var nm=$(this).attr('name'); var cntr=$(this).attr('cntr'); var vpmo=$(this).attr('vpmo'); var vl=$(this).parent().prev ...

Node async does not function asynchronously

I am attempting to utilize the async function to retrieve additional data and add it to my array. To achieve this, I have implemented the following code: User_has_academy_module.findAll({ include: [{model: Module, include: [Category, ModuleType]} ...

Trouble with .nextAll function not updating with dynamic content

Within the post request code block, the JSON response is assigned to the variable 'data' var a=data.Data.overview; var b=data.Data.extras; //summary var result=''; result+='<div class="extra-upper-block">'; result+=&apo ...