Obtaining the Position of an Element Being Dragged in VueJS Drag and Drop Plugin

I am currently developing a calendar day view, and I have implemented draggable cards for events.

My current challenge is figuring out how to determine the destination of the card being moved. I can easily detect the mouse position, but what I really need is the top position of the element I am dragging.

<script setup> 
    import {ref} from "vue";
    import draggable from "vuedraggable";
    const appointments = ref([ // appointment data ]);

    function findTopPosition(event) {
        console.log(event)
        console.log('ClientY = ' + event.clientY);
        console.log('PageY = ' + event.pageY);
        console.log('ScreenY = ' + event.screenY);
        console.log('LayerY = ' + event.layerY);
        console.log('OffsetY = ' + event.offsetY);
    }
</script>

<template>
    <draggable
        v-model="appointments"
        tag="ol"
        @drag="findTopPosition"
    >
        <li>Appointment...</li>
    </draggable>
</template>

Answer №1

@drag is the default HTML event that you can utilize. However, it's recommended to use @move emitted from the draggable component instead (https://github.com/SortableJS/vue.draggable.next#move). The @move event comes with a set of properties detailed here (https://github.com/SortableJS/Sortable#move-event-object):

  • to: HTMLElement
  • from: HTMLElement
  • dragged: HTMLElement
  • draggedRect: DOMRect
  • related: HTMLElement — element that is related to the movement
  • relatedRect: DOMRect
  • willInsertAfter: Boolean — true if the element will be inserted after the target (or false if before)

The most useful properties for you would likely be draggedRect.x and draggedRect.y

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

Editing the dimensions of the input in Bootstrap 4

Is there a way to adjust the width of the input and select elements? I want the input element to take up more space while the select element occupies less space. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel=" ...

Using TypeScript with React: Initializing State in the Constructor

Within my TypeScript React App, I have a long form that needs to dynamically hide/show or enable/disable elements based on the value of the status. export interface IState { Status: string; DisableBasicForm: boolean; DisableFeedbackCtrl: boolean; ...

Leverage the power of Angular CLI within your current project

I am currently working on a project and I have decided to utilize the angular cli generator. After installing it, I created the following .angular-cli file: { "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "project": { "name": " ...

Setting up a webpack proxy for handling HTTP requests in a VueJs application with Axios

I am currently developing a VueJs application that utilizes Axios for handling http requests. Here is an example of how it is implemented: load () { let vm = this this.$http .get('/api/posts') .then(response => { // success ...

I am configuring Jest in my Vite and TypeScript-powered React project

I am having trouble with the relative path of the file I imported in App.test.tsx. It keeps showing me this error message: Cannot find module '@/components/items/card.tsx' from 'src/__tests__/App.test.tsx' Below is the code snippet: // ...

Implementing standard Header and Footer elements in Vue.js single-page applications

I'm currently working on a VueJS project for a spa. I've already created common components for the Header and Footer, and now I want to include them in the main App.vue file. Here is the structure of my code: https://i.sstatic.net/qdLam.png Le ...

Guidance on implementing a Cypress assertion for a JavaScript object retrieved via ag-Grid

Seeking guidance as I navigate the world of UI automation and Cypress, specifically in setting up assertions on JavaScript objects returned by the cypress-ag-grid package Currently, my code is extracting data from ag-grid cy.get("#myGrid").getAg ...

Modifying the copied array will influence changes in the original array as well

Encountering a Puzzling Dilemma: I recently created a copy of an array and was surprised to find that when I made changes to the copied array, the original array also changed. Why did this happen? const groups = [[1, 2, 3, 4 ,5], [2,3]]; const sets = ...

Fetching external data in a Cordova application from a remote server

I have encountered multiple questions similar to mine, but none of them have been able to solve my issue. Currently, I am developing a Cordova app for testing on Android and iOS platforms. My goal is to retrieve data in JSON format from my webserver using ...

Adding a class to an element in AngularJS

I have a question about entering empty values in an input field and highlighting its boundary. I added a new class 'warning' for this purpose and created the following code. HTML: `<body ng-app="TestPage"> <form ng-controller="TestFor ...

Is it necessary to include a back button when navigating through paginated tables?

Within my AngularJS application, I have implemented pagination on the user list page. This allows me to retrieve ten users at a time from the server, with each click loading another set of ten users on a new page. The user details are presented in a tabl ...

Adapting body designs in Nuxt/Vue layout

Struggling to apply body styles in my Nuxt template, encountering issues. Attempting to utilize the beforeCreate, but facing an error: document is not defined. Desire to maintain SSR while adding styling to the body element. How can this be achieved? < ...

Looking to compare the values of objects in one array with the values of objects in another array

Within this code snippet, I am attempting to retrieve the id of a variant that aligns with the selected objects const selected = [ { "id": 14 }, { "id": 95 } ] const variants = [ { "id": 1, "option_values": ...

Feathers.js - Incorporating Static Content

Recently, I've been exploring the capabilities of feathers.js for a new project and have been quite impressed with its potential. Intrigued by what it offers, I decided to embark on creating a basic content management system as a way to further my lea ...

Refreshing Yii Dropdown List After Adding a New Item

Here is the code snippet I am working with: <p> <?php echo $form->labelEx($model,'phone_type'); ?> <span class="field"> <?php echo $form->dropDownList($model,'phone_type', CHtml::listData(PhonesT ...

Locate a specific text within a complex array of objects and retrieve the objects that contain the match as

I have an array of objects named input as shown below. Each object in the array contains a property vertical of type string, an array called platformList, and a nested object named radar_metadata. I am looking to implement a search functionality where I c ...

Incorporating user input into a div element

So, I'm in the process of building my own Task Tracker using JavaScript to enhance my skills, but I've hit a roadblock. I successfully implemented adding a new div with user-inputted tasks, however, there's no styling involved. <div cla ...

Access a different tab through the utilization of JavaScript functions

Hi everyone, I recently created tabs using bootstrap, but I am facing a challenge with a specific scenario. I need the active tab to switch to another tab when a submit button is clicked. <div id="checkout-progress"> <ul class="nav nav-tabs"& ...

Show a particular attribute from an array

My goal is to create a function that displays only minivans from an array of cars when a button is pressed. However, I'm having trouble getting anything to display when the button is clicked, even though there are no errors in the program. Any advice ...

I'm interested in altering the color of each bar in a bar graph individually

I am utilizing PHP to fetch data from MySQL in order to populate an array, which I then use to populate chart data. My goal is to change the color of bars that have a value greater than 50. Despite trying several examples found on Stack Overflow, I haven&a ...