Dragging a div with padding enabled

After implementing an innovative AngularJS directive to enable element dragging, I decided to apply it to a div element that will serve as a floating menu on my website.

A curious issue arises when I attempt to drag a draggable div that is nested within an element with specific padding values or has margin attributes set. During mouse movement, the draggable div suddenly jumps from its original position to an offset, which is not what I desire.

To illustrate this problem, I have prepared an example demonstration linked below. My question now is: How can I ensure smooth movement of my draggable divs without removing padding/margin attributes?

http://plnkr.co/edit/ZwiDo9gx5nAcz4Z0daTo?p=preview

<body style="padding-top:20%">
    <div class="col-lg-2 col-sm-3 col-xs-4" style="background-color:black;color:white;z-index:2;" ng-draggable="vm.dragOptions">

        <span class="col-xs-12 modal-header divheader" style="cursor:pointer">
            List of objects
        </span>
        <div style="margin:0px;" class="padding">
            ...
        </div>
    </div>

UPD

Although @user6028084 provided a snippet that resolved the issue in the Plnkr, it unfortunately interferes with my CSS styles in the actual application. Therefore, I welcome any alternative solutions that may be proposed.

.ng-isolate-scope{
  position:absolute;
}

Answer №1

By removing the padding-top:20%, the issue you mentioned is resolved. If you prefer to keep it, you will need to adjust for that amount.

In my browser, adding the specific amount (117 px in my case) to the script makes it function perfectly.

To ensure functionality across various browser resolutions, modifying line 56 in your script as follows is recommended:

startY = e.clientY - elem[0].offsetTop +117;

Edit: Another option is to eliminate the offset completely.

startY = e.clientY;

Edit2: I have updated how coordinates were being obtained and corrected the startX and startY values which were initially incorrect, along with the x and y assignment in the mousemove event.

Here is the revised plunker link for a fully operational version:

plunker

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

What is the reason behind Facebook's use of margin-left: -9999px to conceal elements?

While experimenting with firebug on Facebook's stream, I stumbled upon an interesting discovery regarding the style of the "update options" menu. It appears that they are hiding this menu by using margin-left: -9999px, and displaying it by overriding ...

Guide to displaying nine images in a table format with three images in each row using PHP

How can I display nine images in a table with three images in each row using PHP? ...

Getting rid of padding in Material UI SwipeableViews in React

Seeking assistance with the React Tabs/SwipeableViews component. I used the MaterialUI Tabs as a reference and it mostly works fine. However, I am facing a minor issue with padding in the content section. Current outcome: Current Outcome The button ...

Struggling to eliminate the remaining vertical paddings within the <div> element while utilizing Bootstrap 5

I want to eliminate all padding (top + bottom) for text inside a block using Bootstrap 5. When I use HTML without Bootstrap, setting padding: 0px works as expected: <div style="padding: 0px; background-color: gray;">no bootstrap</div&g ...

Issue with AngularJS project unable to communicate with WebAPI project

I am a newcomer to the world of AngularJS and WebAPI. My Visual Studio 2015 setup includes two projects: EventViewer (AngularJS) and AppData (WebAPI). To host the website, I have configured IIS Express 7. The path for the website is set to the location of ...

Struggling to make JavaScript read JSON data from an HTML file

I am currently working on developing a word search game using django. One of the tasks I need to accomplish is checking whether the entered word exists in a dictionary. To achieve this, I have been converting a python dictionary into JSON format with json. ...

Flexible design for both columns and rows

Currently utilizing Bootstrap 4, I am attempting to achieve the layout depicted in the image below: https://i.sstatic.net/P1g0t.png My current outcome looks like this: https://i.sstatic.net/AHPDl.png This issue arises on screens with a width exceeding ...

Using CSS in React does not override the CSS preview in Ant Design for React

Working with Tables using antD has been quite an experience. By default, hovering over a Table Row in antD triggers a subtle gray color transition as a highlight. However, I wanted to switch things up and change the color to something different. My attempt ...

Comparing Server-Side Frameworks with Client-Side Frameworks

Recently, I delved into the world of Django and it dawned on me that it operates as a server-side framework. Having previously worked with Vue and AngularJS, I mistakenly assumed that Django followed a similar structure due to shared features like for loop ...

Ways to trigger an alert based on a MySQL query output?

I've been searching all day but can't seem to figure it out. Basically, I have an HTML page with a jQuery function that sends data to a PHP file to fetch information from a database. Retrieving the data to the HTML page is working fine. Now, wh ...

Unable to display <iframe> containing a YouTube video on Mozilla Firefox page

I have included an <iframe> in my webpage to display videos from YouTube, but I am facing an issue with Mozilla Firefox. In Chrome, IE, and Edge, the videos display correctly using the following code: <iframe src="http://www.youtube.com/v/DNLh8p ...

Incorrect display of French characters in emails

Issues arise when sending emails with French characters, as they are not displaying correctly in the mail body. I made sure to include the following code in my HTML page: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> Despit ...

Bootstrap CSS: arranging columns for mobile devices

UPDATE: Having implemented the solution provided in the accepted answer below, I find myself back at square one with the original layout before any adjustments were made. The issue still remains unresolved. Any advice on how to achieve the desired design o ...

In what way could the border exceed the dimensions of the div?

<head> <meta charset="UTF-8"> <title>Document</title> <style> #Tri{ width:0px; height:0px; border-top: 50px solid yellow; border-right:50px solid red; border-bottom:50px solid green; border-left:50px solid blue; } </style&g ...

Trouble with displaying theme modifications in Liferay

Having recently delved into CSS and just starting out with Liferay, I am in the process of creating a custom theme. As an experiment, I attempted to modify the title text color for portlets on the page. In my custom.css file, I have included the following: ...

Tips for incorporating a custom CSS design into a jQueryUI tooltip

I am struggling to apply my custom CSS class on top of the jQueryUI tooltip. Although the tooltip is displaying correctly, my styles are not being applied. Here is the code I'm using: $(document).ready(function () { $("#roles").tooltip({ content: ...

I attempted to update the text on an HTML page using the content of "conetnt", however, it was unsuccessful

.custom-div { outline: none !important; width: 450px; margin: auto; background-color: #ccc !important; text-indent: -9999px;} .custom-div::before{content: 'sample';color: black;} ...

The removal of an object becomes unsuccessful when objects with lower indices have been deleted beforehand

Attempting to construct a multi-layer object representing a program; check out my progress here http://codepen.io/Irish1/pen/lbjdw Imagine adding 3 weeks, each with 3 days, and each day having 3 sessions. Removing these objects is feasible as long as caut ...

Achieving proper functionality of additional directives within uib-tab components

How can I utilize a callback function for uib-tab directives to refresh inner directives after tab rendering? I'm troubleshooting an issue with a third-party directive when used within the uib-tab directive from angular-bootstrap. The specific direct ...

The close menu button is not functioning properly when clicked outside the menu - the buttonevent.matches is not recognized as a

I am encountering an issue with the dropdown menu that is not closing when clicking outside of the menu button. Despite having faced this problem before, I can't seem to find a solution now. Any help would be greatly appreciated as I am uncertain why ...