Challenge with jQuery UI Draggable: Div moves slower than mouse cursor

My issue is that when I move the mouse, the mouse moves faster than the div underneath it. I am using jQuery UI 1.12.1 and jQuery 3.3.1 with default options.

Here is my HTML:

<div class="popup"></div>

And here is my jQuery code:

$(function()
{
    $(".popup").draggable();
});

Do you have any thoughts on why this behavior might be occurring?

Answer №1

Yes, indeed! In case you encounter such an issue, it may be due to the positioning of the mouse cursor in relation to the active element. To resolve this, simply ensure that the cursor aligns with the center of the element when dragging begins. By implementing this solution, the problem should be rectified as the mouse cursor will stay centered within the active div until it is no longer hovered over. Below is the code snippet illustrating the aforementioned concept:

<!-- JavaScript and jQuery-->

$('div').on('mousedown', function (e) {

    $(this).addClass('active').parents().on('mousemove', function (e) {

        $('.active').offset({

            top: e.pageY - $('.active').outerHeight() / 2,
            left: e.pageX - $('.active').outerWidth() / 2

        }).on('mouseup', function () {

            $(this).removeClass('active');

        });
        
    });
    
    return false;    
});
div {
    cursor: move;
    width:70px;
    height:70px;
    background:black;
    z-index: 1;
    margin:2px;
    float:left;
}
.active {
    background: grey;
    z-index: 2;
}
<!-- HTML-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<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

Implementing a jQuery Popup to Show Error Messages

Is there a way to display error messages in a popup and hide all form fields when a success message appears? Currently, I am placing the success message above the form and closing it after the form is submitted. The error message div is placed inside the s ...

ReactJS is in need of extracting certain values from a promise

Within my Firebase database, I have organized data into two Documents: "users" and "posts". Each post in the "posts" collection is linked to a specific user using their unique id from the "users" collection. My goal is to retrieve user data associated wi ...

Using jQuery to detect when the play button on an HTML5 video is clicked

I am trying to figure out how to register a click on the play button of an HTML5 video. I can successfully register a click on the <video> tag itself, but any click on the controls (such as play/pause) is not being registered. How can I make sure tho ...

Unable to retrieve JSON data from a PHP file hosted on the server

I have a server-side database with a table called "customers" and a column named "names". My goal is to send a request to the server asking for the first two records in the "customers" table. However, when I execute the program, the browser does not displa ...

Question: How can I use the jQuery datepicker to ensure that the selected date remains in the

I recently created a webpage using a combination of HTML, PHP, and jQuery. The PHP script is designed to load all data where the date matches the one selected in the text input field generated by the jQuery datepicker. However, I encountered a problem whe ...

How can we dynamically resize page content to fit varying browser window sizes across different devices with the help of jQuery/JavaScript?

Looking for a solution involving an HTML form that includes a text field and a submit button. The text field is where the user enters a URL, and upon clicking the submit button, they are redirected to that URL. Seeking a way to adjust the size of the new ...

Exploring AngularJS: Filtering a nested JSON array

Just starting out with AngularJS and I could use some assistance. I have a JSON file that I've been working on, and I'm attempting to create a search filter by ingredient using AngularJS. Can anyone provide guidance? { "id": 01, "recipe- ...

Is it permissible to use jQuery within a table tag?

My code structure is a combination of php, jquery, and html as follows: <table> <?php while ($r = mysql_fetch_array($sth)){ ?> <tr> <td> Some HTML tag here </td> </tr> <script ...

Encasing the Angular 2 component template in a <div> tag

Currently, I have a parent component managing multiple child components. My goal is to enclose each child component's template with a *ngIf directive for conditional rendering. The number of children in the parent component can vary. Here is an examp ...

Dealing with multiple ajax requests while utilizing a single fail callback

Looking for a solution: I have two arrays containing deferreds. In order to detect failures in the outer or inner 'when' statements, I currently need to use double 'fail' callbacks. Is there a way to consolidate errors from the inner &a ...

The images on the browser are not showing up correctly

I'm struggling to understand why my website appears distorted on Android browsers. Interestingly, it displays perfectly on iOS devices, and occasionally looks fine on some Android phones; however, more often than not, it appears poorly structured. Ch ...

Continuously improving the form as they evolve

I am interested in creating a form that automatically sends data when users make changes to it. For instance: Imagine a scenario where a moderator is editing an article and changes values in a select field. As soon as the change is made, an ajax request ...

What is the method for generating a 2D array in JavaScript when the array's length is unknown?

I'm trying to create a 2D array that groups animals by their first letter of the alphabet. However, the output is not what I expected. I want each letter to have its own sub-array with corresponding animal names. Here's the code I tried: functio ...

What causes the "error cannot set headers after they have been sent" message to appear in this specific method when using Express?

I have encountered an issue with my nodejs app using express. Whenever I attempt to call the method below, I receive an error in the console saying "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client". I have read through ...

Utilizing Fancybox with a polymer paper-card: A step-by-step guide

I have a collection of paper-cards and I am looking for guidance on integrating the fancybox library to display the content of each paper-card. Here is a sample of a paper-card: <paper-card heading="Emmental" image="http://placehold.it/350x150/FF ...

Error: The callback specified is not a valid function

Here is a function example: reportAdminActions.reportMemberList(project, function(data) { console.log(data); }); This particular function gets called by another ajax operation, similar to the one shown below: reportMemberList: function(projectId, ca ...

equal child width as parent column container

I'm in the process of developing a custom dropdown search bar that presents results as the user enters text. One issue I'm facing is that the list of results must have a position: absolute. This causes it to disregard the width of the parent col ...

the values being shown in a read-only text field

Here is a table structure displaying text inputs, total marks, and marks remaining: Marks Per Answer Total Marks Marks Remaining (blank text input) 4 4 (blank text input) 6 6 (blank di ...

Determine whether the array of an object includes any elements from another array using Lodash

Each product in the array contains a unique structure that includes an ID, name, and materials list. The materials list consists of various materials with their own IDs and names. { id: 1, name: "product 1", materials: [ { id: 1, nam ...

The progress bar is only displayed during the initial consecutive file uploads in Angular 6

Within my Angular application, I have implemented a dedicated form for uploading profile pictures. The process involves triggering a confirmation modal upon uploading a new image, followed by the display of a progress bar on the page. This progress bar ind ...