Moveable elements that can be easily placed into a designated spot

After dropping the draggable div in the drop zone, it always moves to the top left corner. I want it to stay where I dropped it, right in the middle of the drop zone.

I've been trying to adjust the CSS properties for positioning, but so far I haven't found a solution that works properly.

Here is my current JavaScript code:

$('.tab-list img').draggable({
        helper: "clone"
    });

    $('#main').droppable({
        drop:function(event, ui) {
            var item = $(ui.draggable);

            var add;
            if(item.data('itemstate') == 'rectangle'){
                add = '<div class="pi-rectangle"></div>';
            }
            $(add).css('left', ui.position.left);
            $(add).css('top', ui.position.top);
            console.log(ui.position.top);
            console.log(ui.position.left);
            $(add).appendTo(this);
        }
    });

Answer №1

I managed to solve the problem on my own

$(add).css('left', ui.position.left-80+'px').css('top', ui.position.top-80+'px').appendTo(this);

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

Jumping occurs in Slick Carousel when adjusting the width of the active slide

Check out the fiddle link here: http://jsfiddle.net/br24p3Lr/ I've been attempting to perfect the animation when transitioning to a new active slide. Despite trying various approaches to resolve this issue, I seem to be stuck at the moment. The foll ...

Connecting an HTML button to PHP: A step-by-step guide

Can someone help me figure out how to connect an HTML button to a PHP URL without using JavaScript or jQuery? The button needs to call the PHP script when clicked, and the PHP script should register whether the switch is ON or OFF in my database along with ...

Having trouble with jQuery UI drag-and-drop feature when dropping onto an empty container or

Encountering an issue with the jQuery UI sortable widget where I am unable to drop any item into an empty container. Strangely, when the container already contains an item, it functions perfectly fine. The way I have called the widget is shown below: $(". ...

Managing responses from ajax requests that can handle both text and json formats

After submitting a form via Ajax and receiving a response from the servlet in either text or JSON format, I'm wondering if there is a way to handle both types of responses. I've read through the jQuery/Ajax documentation, but the JQuery Ajax page ...

AJAX nesting with varying input speeds

So, this is the situation - during a job interview I was presented with an interesting AJAX question: The scenario involves code where the onChange function triggers an AJAX call to the 'asdf server', and then that call initiates another AJAX ca ...

"Unlocking the Power of Twitter with Requests and API

I'm diving into the world of Twitter API for the first time, so please be patient with me: I'm struggling to figure out how to integrate the API into my website. Most tutorials suggest using a separate jQuery/Javascript file, but that just adds ...

How can I remove the bouncing effect of the top bar in Chrome when scrolling in Three

There is a frustrating problem I am encountering on my website with the joystick control. Whenever I try to move my joystick down, Chrome triggers either the refresh dropdown or the top bar bounces and goes in and out of fullscreen mode. ...

Encountering Issues with Making an Ajax Request Using Jquery

I am facing an issue while trying to log in using Jquery ajax. When I make the ajax call with the jQuery.ajax(...) method with a Java servlet, the method fails to execute. The Ajax library I am using can be found at http://ajax.googleapis.com/ajax/libs/jqu ...

What is the best way to compare a string with a specific object key in order to retrieve the corresponding value?

I'm looking to achieve a relatively simple task, or at least I think so. My goal is to compare the pathname of a page with key-value pairs in an object. For example: if("pathname" === "key"){return value;} That's all there is to it. But I&apos ...

Exploring the world of Django: Using model formsets with the power

I am struggling to use Ajax for submitting my zipped formsets. The code functions flawlessly without Ajax, but as soon as I try to incorporate Ajax, I encounter a ValidationError: [u'ManagementForm data is missing or has been tampered with'] Thi ...

Delay loading background image until a specific time has passed, preventing website from fully loading

My website features a vibrant backgroundImage Slideshow that functions seamlessly. However, I am looking to reload the images of the slideshow after a specific time interval. The issue is that the website keeps loading endlessly. Once the website has com ...

What could be causing the hover effect to not work on the cards in my Svelte component? (HTML+CSS)

Greetings everyone! This is my debut post on this platform, and I'm in urgent need of assistance as I am relatively new to the world of programming. I've been trying tirelessly to implement a hover effect on these cards, but for some reason, it ...

What is the reason why the VIEW SOURCE feature does not display filled dropdown list boxes?

The following code functions well in terms of dynamically populating two dropdown list boxes. Initially, the getBuildings function is called to make a request to the getBuildings.php file. This action fills the buildingID dropdown list box with options. ...

Determine the number of times a specific field value is recurring in a MySQL

Looking for information on the games table structure: `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `content` text COLLATE utf8_unicode_ci, `team1ID` int(7) unsigned DEFAULT NULL, `team ...

Engaging Conversations with jQuery

In my form, there is an image positioned next to each radio button. The number and content of these radio buttons are retrieved from a database. I am attempting to set up a click event on these images so that when clicked, a brief description of the corres ...

Data grid: Conceal or reveal rows based on filter criteria

My datatable function allows users to update the status to either NG or COMPLETED, with the default being a dash (-) sign. When a row is updated to COMPLETED, it is hidden, while updating to NG changes the row's color to red. The functionality is work ...

Controlling the z-index of Angular ngx daterangepicker

Having multiple forms in an expansion presents a unique challenge. I initially used the following code for a date picker: <mat-form-field> <input formControlName="date" matInput placeholder="Date" type="date"> </mat-form-field> This m ...

How come the variable in the function is not being displayed on the label?

My JavaScript code is not functioning as expected. I have a text box, a list item, and a button; the button should trigger the function cal(), which is a calculator that calculates and returns values like s1 and k1. I am trying to display this value in a ...

presentation not transitioning smoothly as the next slide appears

Initially, my aim is to center the slideshow on the page but I am uncertain about how to achieve it. This is how my current webpage appears: https://i.sstatic.net/E6bzQ.png Furthermore, as shown in the image, the sliding effect of the blue slide is movi ...

A step-by-step guide to creating a CSS menu using pure HTML markup

I have been working on the CSS for a menu using some HTML code, but I am stuck and uncertain about my next steps. Here is the CSS I have added so far: #menu-my-integra, ul.sub-menu { list-style: none; padding: 0; margin: 0; } #menu-my-integra li ...