JQuery element becomes misaligned with cursor during drag and drop functionality, but only occurs when scrolling down on the page

I found a helpful JQuery example on this website and attempted to implement it.

Despite making some progress, the solutions I found online did not work entirely. I tried setting position: relative for the sortable ID and ui-state-default classes, as well as adding a cursorAt value in the sortable call.

However, I encountered an issue where the first element aligns with the cursor on page reload but subsequent elements do not. I am at a loss for what to try next. Any suggestions?

Since I am using a Wordpress theme, I had to use !important to prevent other CSS styles from interfering.

https://i.sstatic.net/aFoIr.png

      <title>jQuery UI Sortable - Default functionality</title>
      <link rel='stylesheet' href='//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'>
      <link rel='stylesheet' href='/resources/demos/style.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>
      <script>
      $( function() {
        $('#sortable').sortable({cursorAt: { top: 0, left: 0 }});
        $( '#sortable' ).disableSelection();
        $( '#datepicker').datepicker();
      } );
      </script>

    <div class='formStyle'>

    ... (omitted for brevity)

CSS

#sortable {
    width: 250px;
    position: relative !important;
}

li.ui-state-default.ui-sortable-handle {
    position: relative !important;
}

Answer №1

Give this a shot

$(function() {
      $('#sortable').sortable({
          containment: "parent",
          tolerance: "pointer"
      });
      $('#sortable').disableSelection();
      $('#datepicker').datepicker();
});

Answer №2

Upon realizing that the issue was specific to Chrome, I researched offset problems with browser compatibility and found a solution by adding the "overflow: auto" parameter to the UL or container

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

Guide on Developing a JavaScript Library

After creating several JavaScript functions, I noticed that I tend to use them across multiple projects. This prompted me to take things a step further and develop a small JavaScript Library specifically for my coding needs. Similar to popular libraries l ...

A dynamic way to refresh select options in HTML with PHP and JavaScript powered by JQuery

Imagine a scenario with the following layout: There is a table called "Regions" with columns "id" and "name". Also, there is another table named "Cities" containing columns "id", "region_id", and "name". How can you efficiently refill an HTML select opt ...

Add a span tag with the class "javascript" around the index of a character within a string

Within an element, I have a string as such: <p>I like trains and planes</p> Using jQuery's .text(), I extract the text and store it in a variable. var str = $('p').text(); I aim to identify a specific character, let's sa ...

Errors and warnings caught off guard while running json-server with the --watch flag

I'm having some trouble using json-server in the following way: $ json-server --watch db.json Every time I try to run that command, I encounter errors or warnings depending on the version of json-server that is installed: 1.0.0-alpha.1-1.0.0-alpha.1 ...

Navigating through a JSON document using jQuery

I've been attempting to iterate through a JSON string (located in a separate file named language.json) but I can't seem to make it work { "language": [ { "name": "English Languages", "values": ["English 1", "English 2", " ...

Step-by-step guide for integrating a Firebase-exported database into mLab

After exporting my database from firebase, here is how it appears: { "a" : { "-LH-wWiC6Pt874i" : { "OwnerUserId" : "in63Syuyuyighjj", "Passengers" : { "dUCMzvi5UIBd81jPRQhg2" : { "HasPaid" : false, "IsAccept ...

Exploring the boundaries of arguments and object immutability in JavaScript

My recent assignment involves exploring scope and mutability concepts in JavaScript: The task at hand is to create two functions, reverseArray and reverseArrayInPlace. The first function, reverseArray, takes an array as input and returns a new array wit ...

Optimizing Divs for Maximum Layout Efficiency

My current issue involves a series of divs that contain varying amounts of content. These divs are floated left and I am striving to align them seamlessly without any vertical space between them (except for the 10px margin that I have included). You can v ...

Having trouble resolving a setInterval problem with JavaScript?

Yesterday, I learned about the setInterval function which allows me to execute a task or function after a specific amount of time. While I have successfully implemented the interval in my code, it keeps adding new lines with the date each time. What I re ...

Ways to analyze and contrast two ajax replies post parsing

Is there a different way to write code for making ajax calls for two pages within a single function? Also, I am looking for a method to compare the responses from both requests, specifically comparing objects y2 and y3. function compareResponses() { ...

Employing innerHTML in conjunction with Greasemonkey

In the process of creating a userscript (specifically for greasemonkey/firefox) that will be utilized on a webpage, I have encountered a challenge. This script includes a set of area tags with an onmouseover attribute that triggers a function: <area ...

Pass props in Reactjs depending on the component that has been loaded

I am seeking guidance on how to successfully pass a ReactJS prop from a component to my app during its loading process. I have managed to pass the value when my props are in app.js, but I am unsure of how to handle the prop when it is loaded from a separat ...

Creating a basic modal with jQuery

I am currently using SimpleModal with jQuery, and I have a single confirmation dialog set up. When the user selects 'Yes' in the dialog box, I need to load my.php within that same dialog. Despite my efforts, I'm still brainstorming for poten ...

techniques for accessing HTML source code through an AJAX call

I am trying to retrieve the HTML source of a specific URL using an AJAX call, Here is what I have so far: url: "http://google.com", type: "GET", dataType: "jsonp", context: document.doctype }).done(function ...

Conflict between jQuery and MooTools libraries

Currently, I am facing a challenge when trying to implement a small JavaScript code that utilizes jQuery. Here is the code: $(document).ready(function () { $("span.question").hover(function () { $(this).append('<div class="tooltip"&g ...

Converting an object with nested arrays into its original form (Ajax request using C#)

I am struggling with the deserialization of an object that has two properties. One property is a simple string, while the other property is an array of arrays, with each element in an array having a name and a value. The POST operation works fine until it ...

sending a form to an iframe located in a separate file

I am looking to submit a form and display the result in an iFrame located in a separate file. file1.html <form action="login.php" target="target_iframe"> <input type="text" id="login_id" /> <input type="text" id="login_ps" /> & ...

Issue with toggleClass not functioning properly after receiving data from an AJAX call

On my website, I have a link that triggers an AJAX request. However, when the response comes back and I try to use the toggleClass function in the following .js code snippet, it doesn't work as expected: $(document).ready(function(){ $("td").click( ...

Enhancing PHP Markdown to Support Custom CSS Classes

Markdown is a tool I use frequently in my PHP projects, but sometimes I find its output to be too simplistic. I wish I had more control over the layout and design of my content. For example, when inserting an image: <p> <img src="path/to/image. ...

Vue is refusing to display information for a certain API call

Within my next component, I have the following structure: <template> <div class="home"> <div class="container" v-if="data" > <Card v-for="result in data" :img="result.links[0]&q ...