search
HomeWeb Front-endVue.jsHow to implement draggable layout in Vue?

With the continuous development of Web front-end technology, more and more developers are beginning to use the SPA (Single Page Application) architecture to build applications. As one of the most popular web front-end frameworks currently, Vue provides a rich component library and tools to make it easier to build SPA applications. Vue also provides some convenient solutions when it comes to implementing draggable layouts.

1. Using Vue-Grid-Layout

Vue-Grid-Layout is a responsive grid layout system based on Vue. It provides a set of components that allow you to easily implement draggable, resizable grid layouts while supporting gesture dragging and zooming on mobile devices. Using Vue-Grid-Layout is very simple. You first need to introduce it in the project:

npm install vue-grid-layout -S

Then use it in the Vue component, as follows:

<template>
  <vue-grid-layout :layout="layout" :col-num="12" :row-height="30" :is-draggable="true"
                   :is-resizable="true" :vertical-compact="false" :margin="[10, 10]"
                   :use-css-transforms="true" :auto-size="true">
    <vue-grid-item v-for="item in layout" :key="item.i" :x="item.x" :y="item.y" :w="item.w"
                   :h="item.h">
      <div class="grid-item-content">{{ item.i }}</div>
    </vue-grid-item>
  </vue-grid-layout>
</template>
<script>
import { VueGridLayout, VueGridItem } from 'vue-grid-layout';

export default {
  components: { VueGridLayout, VueGridItem },
  data() {
    return {
      layout: [
        { i: 'item1', x: 0, y: 0, w: 1, h: 2 },
        { i: 'item2', x: 1, y: 0, w: 1, h: 2 },
        { i: 'item3', x: 2, y: 0, w: 1, h: 2 },
        { i: 'item4', x: 3, y: 0, w: 1, h: 2 },
      ]
    }
  },
}
</script>

In the above code, we define A VueGridLayout component and binds the layout array to its layout property, where each item represents a grid item, including its id (i), x, y coordinates, and width (w) and height (h). We can also set the col-num attribute to specify the number of columns in the grid, the row-height attribute to set the height of each row, and the is-draggable and is-resizable attributes to specify whether the grid items can be dragged and resized respectively. Vue-Grid-Layout also provides various customization options that can be configured as needed.

2. Use Vue-Draggable

Vue-Draggable is another popular Vue plug-in, which allows you to implement draggable lists in Vue, such as drag-and-drop sorting and drag-and-drop changes. Parent etc. It is very suitable for implementing a drag-and-drop task panel similar to Trello. Using Vue-Draggable is also very simple. You need to introduce it first:

npm install vuedraggable --save

Then use it in the Vue component, as shown below:

<template>
  <draggable v-model="list" :options="dragOptions">
    <div v-for="(item, index) in list" :key="item.id">
      <h3 id="item-title">{{ item.title }}</h3>
      <p class="content">{{ item.content }}</p>
    </div>
  </draggable>
</template>
<script>
  import draggable from 'vuedraggable';

  export default {
    components: { draggable },
    data() {
      return {
        list: [
          { id: 1, title: 'Title 1', content: 'Content 1' },
          { id: 2, title: 'Title 2', content: 'Content 2' },
          { id: 3, title: 'Title 3', content: 'Content 3' },
          { id: 4, title: 'Title 4', content: 'Content 4' },
          { id: 5, title: 'Title 5', content: 'Content 5' },
        ],
        dragOptions: {
          group: {
            name: 'task',
            pull: 'clone',
            put: false
          },
          sort: false,
          animation: 150,
        },
      }
    },
  }
</script>

In the above code, we define a draggable component , and bind the list array to its v-model, each item represents a list item, including its id, title and content. We can also set various custom options through the options attribute, such as the group attribute to set drag and drop grouping, the sort attribute to set whether sorting is enabled, the animation attribute to set the animation duration, etc.

Summary

It is common to use Vue to implement draggable layout. Vue-Grid-Layout and Vue-Draggable are two good solutions. Vue-Grid-Layout is suitable for grid layout, which can realize draggable and resizable grid layout, and is suitable for building various complex interactive applications. Vue-Draggable is mainly used for drag-and-drop lists, and is very effective in implementing drag-and-drop task panels like Trello. Choosing the right solution depends on the specific use case, both are simple and flexible to use.

The above is the detailed content of How to implement draggable layout in Vue?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the disadvantages of VueJs?What are the disadvantages of VueJs?May 05, 2025 am 12:06 AM

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix: Unveiling Its Frontend FrameworksNetflix: Unveiling Its Frontend FrameworksMay 04, 2025 am 12:16 AM

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

Frontend Development with Vue.js: Advantages and TechniquesFrontend Development with Vue.js: Advantages and TechniquesMay 03, 2025 am 12:02 AM

Reasons for Vue.js' popularity include simplicity and easy learning, flexibility and high performance. 1) Its progressive framework design is suitable for beginners to learn step by step. 2) Component-based development improves code maintainability and team collaboration efficiency. 3) Responsive systems and virtual DOM improve rendering performance.

Vue.js vs. React: Ease of Use and Learning CurveVue.js vs. React: Ease of Use and Learning CurveMay 02, 2025 am 12:13 AM

Vue.js is easier to use and has a smooth learning curve, which is suitable for beginners; React has a steeper learning curve, but has strong flexibility, which is suitable for experienced developers. 1.Vue.js is easy to get started with through simple data binding and progressive design. 2.React requires understanding of virtual DOM and JSX, but provides higher flexibility and performance advantages.

Vue.js vs. React: Which Framework is Right for You?Vue.js vs. React: Which Framework is Right for You?May 01, 2025 am 12:21 AM

Vue.js is suitable for fast development and small projects, while React is more suitable for large and complex projects. 1.Vue.js is simple and easy to learn, suitable for rapid development and small projects. 2.React is powerful and suitable for large and complex projects. 3. The progressive features of Vue.js are suitable for gradually introducing functions. 4. React's componentized and virtual DOM performs well when dealing with complex UI and data-intensive applications.

Vue.js vs. React: A Comparative Analysis of JavaScript FrameworksVue.js vs. React: A Comparative Analysis of JavaScript FrameworksApr 30, 2025 am 12:10 AM

Vue.js and React each have their own advantages and disadvantages. When choosing, you need to comprehensively consider team skills, project size and performance requirements. 1) Vue.js is suitable for fast development and small projects, with a low learning curve, but deep nested objects can cause performance problems. 2) React is suitable for large and complex applications, with a rich ecosystem, but frequent updates may lead to performance bottlenecks.

Vue.js vs. React: Use Cases and ApplicationsVue.js vs. React: Use Cases and ApplicationsApr 29, 2025 am 12:36 AM

Vue.js is suitable for small to medium-sized projects, while React is suitable for large projects and complex application scenarios. 1) Vue.js is easy to use and is suitable for rapid prototyping and small applications. 2) React has more advantages in handling complex state management and performance optimization, and is suitable for large projects.

Vue.js vs. React: Comparing Performance and EfficiencyVue.js vs. React: Comparing Performance and EfficiencyApr 28, 2025 am 12:12 AM

Vue.js and React each have their own advantages: Vue.js is suitable for small applications and rapid development, while React is suitable for large applications and complex state management. 1.Vue.js realizes automatic update through a responsive system, suitable for small applications. 2.React uses virtual DOM and diff algorithms, which are suitable for large and complex applications. When selecting a framework, you need to consider project requirements and team technology stack.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools