Home  >  Article  >  Web Front-end  >  An example explains how to use vue to implement a sidebar dragging function

An example explains how to use vue to implement a sidebar dragging function

PHPz
PHPzOriginal
2023-04-07 09:31:541240browse

Vue is a popular JavaScript framework that allows developers to quickly build modern, responsive web applications. One of the very interesting features is sidebar dragging, which is a very popular and practical feature. This article will introduce how to use Vue to implement sidebar dragging.

First, you need to install Vue.js. You can use npm or yarn to install it, and introduce Vue.js into the project:

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

In Vue.js, you can define components, in the components Write the code for sidebar dragging. In this example, we will create a component called DragSidebar. In the DragSidebar component, two data properties need to be defined: dragging and mouseX. dragging indicates whether the sidebar is being dragged, and mouseX indicates the X coordinate of the mouse.

<template>
  <div class="drag-container">
    <div class="sidebar" :style="{ transform: translate }" @mousedown="mousedown"
         @mouseup="mouseup" @mousemove="mousemove">
      <div class="content">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dragging: false,
      mouseX: 0,
      sidebarX: 0
    }
  },
  computed: {
    translate() {
      return `translate3d(${this.sidebarX}px, 0, 0)`
    }
  },
  methods: {
    mousedown(event) {
      this.dragging = true
      this.mouseX = event.clientX
    },
    mouseup() {
      this.dragging = false
    },
    mousemove(event) {
      if (this.dragging) {
        const diff = event.clientX - this.mouseX
        this.sidebarX += diff
        this.mouseX = event.clientX
      }
    }
  }
}
</script>

<style scoped>
.drag-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 320px;
  min-width: 320px;
  height: 100%;
  background-color: #F2F2F2;
  transition: transform .3s ease;
}

.content {
  padding: 24px;
}
</style>

In the above code, we defined three methods: mousedown, mouseup and mousemove, which handle press, release and move mouse events respectively. In mousedown, we set the dragging property to true, which means that the sidebar starts to be dragged and the X coordinate of the mouse is recorded. In mouseup, we set the dragging property to false, which means the sidebar stops being dragged. In mousemove, we adjust the sidebar's position based on how far the mouse has moved.

Finally, we use the DragSidebar component in the parent component and add some child components to it for testing. You may need to add some CSS styles yourself to suit your project needs.

<template>
  <div class="app">
    <drag-sidebar>
      <h1>Title</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Suspendisse consectetur pharetra ante sit amet bibendum.</p>
    </drag-sidebar>
    <div class="main">
      <h1>Main content</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Suspendisse consectetur pharetra ante sit amet bibendum.</p>
    </div>
  </div>
</template>

<script>
import DragSidebar from './components/DragSidebar.vue'

export default {
  components: {
    DragSidebar
  }
}
</script>

<style>
.app {
  height: 100vh;
  display: flex;
}

.main {
  flex-grow: 1;
  padding: 24px;
}
</style>

This is all about using Vue to implement sidebar dragging. Through the above steps, you can quickly implement a practical sidebar dragging.

The above is the detailed content of An example explains how to use vue to implement a sidebar dragging function. 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