What is the best way to enable an element to be dragged from just a single point?

I am facing a challenge with a parent div (such as "#strip17") that contains multiple child divs. Most of these child divs consist of a canvas where the user can draw using their mouse. However, the last child div should act as a handle that allows the user to drag the parent div around.

The entire parent div should be draggable, so users can grab the drag-child and move it wherever they need. I am looking for a way to create a handle for dragging the parent div as a whole.

The issue I am encountering is that I can only apply the .draggable() function to either the parent or the drag-child div. If I apply it to the parent div, drawing on the canvas elements is disabled as it is interpreted as dragging. On the other hand, if I apply it to the drag-child div (".camber-move"), only that specific div moves while the rest of the parent div remains static but drawable.

I believe it is possible to make the parent div draggable by applying .draggable() to ".camber-move" and somehow instructing jQuery-UI to move both the ".camber-move" div and its parent "#strip17". But I am unsure how to achieve this.

// '#<newStripID>' represents the ID of the new parent element
// '#<newStripID> .camber-move' refers to the ID of the element that should act as the drag handle
$('#' + newStripID).draggable({
     "containment": "#content"
});

html:

<div id="strip17">
    <div class="strip-camber-1"><canvas></canvas></div>
    <div class="strip-camber-1"><canvas></canvas></div>
    <div class="strip-camber-1"><canvas></canvas></div>
    <div class="strip-camber-1"><canvas></canvas></div>
    <div class="camber-move"></div>
</div>

Answer №1

Take a look at this Demo: https://jqueryui.com/draggable/#handle

Implementing this in your scenario:

$(function() {
  $("#strip17").draggable({
    containment: "#content",
    handle: "div.camber-move"
  });
});
.strip-camber-1 {
  border: 1px solid #CCC;
}

.camber-move {
  border: 2px solid #222;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="strip17">
  <div class="strip-camber-1"><canvas></canvas></div>
  <div class="strip-camber-1"><canvas></canvas></div>
  <div class="strip-camber-1"><canvas></canvas></div>
  <div class="strip-camber-1"><canvas></canvas></div>
  <div class="camber-move"></div>
</div>

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

Animate a box moving continuously from the right to the left using jQuery

I'm trying to create a continuous motion where a box moves right, then left, and repeats the cycle. However, I can only get it to complete one cycle. $(document).ready(function() { function moveBox() { $('#foo').css({ 'ri ...

Error encountered when attempting to load files from the same domain due to CORS restrictions

I'm currently developing a website at and I am trying to load the content of an HTML file into an element. I have specified the element as $('.about-us-first-col') and I am loading the content using the code: $('.about-us-first-col&apo ...

Ways to receive responses from PHP with jQuery AJAX

Hey there, I'm currently working on echoing specific messages from my PHP code back to my AJAX function. Usually, I only have one message to echo, but this time I have two. I'm not sure how to assign each echo to a different .html() element. $(" ...

How can I access clipboard information within the ng-paste directive?

Currently, I am working with angularjs 1.3.2 and I'm seeking a way to retrieve the clipboard data during a paste event. Can anyone provide guidance on how to achieve this? This question is similar to the one found at: "Paste" event in Angul ...

Form validation errors were detected

Currently, I am working with a formgroup that contains input fields with validations set up in the following manner: <mat-form-field class="mat-width-98" appearance="outline"> <mat-label>Profession Oc ...

How to remove every instance of an item in an array based on its unique identifier using JavaScript

Is there a way to clean up an array of objects by removing duplicates with the same id? In this case, I only want to keep the object with id 1. This is my approach: let data = [{ "selected": true, "id": 3, "ProductName": "Aniseed Syrup", ...

Retrieve the Flot graph object using Javascript

Seeking advice on a function that should return a JavaScript object. function get_temp(){ var datasets_temp = { "sens1": {data: [[1340803113,22.9],[1340803474,23],[1340803836,23],[1340804197,23.1]] }, "sens2": {data: [[134084 ...

Why is it that the reduce function is not returning the object I expected?

[['id', '1111'], ['name', 'aaaaa']] I came across this list. { id: '1111', name: 'aaaa' } Now, I would like to transform the list into a different format. My attempt to convert the list into t ...

Analyzing an array through its sub arrays

Imagine we are dealing with an array of varying length and we need to process it in chunks of maximum size 100, aiming to use the fewest number of chunks. For example, if the array's length is 241, we would need to divide it into 3 subarrays: 41, 100, ...

How to position the figcaption within a card?

My card design features a masonry-style layout similar to what you can see here https://codepen.io/daiaiai/pen/VPXGZx I'm trying to add some additional information on top of the images using figcaptions, but my attempts with position:relative haven&a ...

Using jQuery to loop through and remove elements

Within my JavaScript array of objects, each object holds key/value pairs. I am attempting to loop through this array and remove any object that does not match a specific value for a particular key (e.g. "Industry"). However, despite my code snippet provide ...

Navigating with Buttons using React Router

Header: I need help figuring out how to properly redirect to a new page when clicking on a Material UI button using the onClick method. Specifically, I am unsure of what to include in my handleClickSignIn function. Here is a snippet of code from my Header ...

Invoke a Java script function for Regular Expression validation failure in ASP.NET

I have a <asp:RegularExpressionValidator> that validates a text box, and I also have a JavaScript function that prevents entering non-numerical values in the textbox. When I use the expression validator alone, it works fine. However, as soon as I add ...

Tips on causing a forEach loop to pause for a regex substitution to occur

I have a project in progress for an email web app where I am working on replacing certain keywords (first name, last name, email) with the actual properties of the user. As of now, I am going through a list of recipients and modifying the email content to ...

`Generating dynamic content based on ajax response``

Hey there! I've got a little challenge on my hands. I've set up 3 input boxes that act as table cells (the header). When the input in one of these boxes changes, a script runs. This script triggers a function that pulls data from my database thro ...

The CSS attribute for setting the background color of Shell in css/default.css does not produce any visible

Currently working my way through the book "Eclipse 4 Plug-in Development by Example Beginner's Guide", specifically on the section titled "Time for action – styling the UI with CSS". The guide instructs to edit the "css/default.css" file and add th ...

Contrasting the process of generating DOM elements by using createElement, setting properties, etc., versus constructing a string of elements and then appending them

I am curious about the distinction between these two methods for creating HTML elements. I cannot seem to find any difference. I simply want to understand if there are any variations based on performance or any other factors. The first technique involves ...

I'm encountering the error message "Controller not a function, received undefined" even though I have not declared the controller globally

Encountering the same ERROR Argument 'AveragesCtrl' is not a function, got undefined. Despite attempting various solutions found on SO, I am still unable to resolve this issue. Any insights into what might be causing my error? <div > ...

Selecting items in an HTML table using a drag-and-drop feature for selecting square or rectangular sections with colspan and rowspan

I am currently working on an implementation for selecting cells in a table. The challenge I am facing is that the cells in question can have a colspan or rowspan, which means that the selection is not restricted to a square or rectangular shape. For exampl ...

The Datatable is only displaying a single record

After fetching data from a firestore collection and storing them in an array, I encountered an issue where only one entry is displayed in the datatable despite retrieving all documents from the firestore collection. What could be causing this problem? Belo ...