implement mobile terminal drag floating window sliding effect in vue-ag捕鱼王app官网

current location:home > learning > web front-end > vue >

implement mobile terminal drag floating window sliding effect in vue

author:jiyik last updated:2025/02/26 views:

with the popularity of mobile applications, floating window sliding effect has become an increasingly common interactive method in mobile design. in vue, we can use some plug-ins or write code ourselves to achieve this effect.

1. use third-party plug-ins

  1. view-draggable

vue-draggable is a drag component library based on vue.js, which provides some common drag effects, including drag-sort, drag-copy, drag-delete, etc. we can use it to implement the drag effect of the floating window on the mobile terminal.

first, we need to import the vue-draggable dependency:

npm install vuedraggable --save

then, in the component that needs to use the drag effect, import and register vue-draggable:

import draggable from 'vuedraggable'
export default {
  components: {
    draggable
  },
  data() {
    return {
      items: [
        { id: 1, name: 'item 1' },
        { id: 2, name: 'item 2' },
        { id: 3, name: 'item 3' },
        { id: 4, name: 'item 4' },
        { id: 5, name: 'item 5' }
      ]
    }
  }
}

next, use the draggable component in the template to show the drag effect:

<template>
  <draggablev-model="items">
    <divv-for="item in items":key="item.id">{{ item.name }}div>
  draggable>
template>

in this way, we can realize the drag effect of the floating window on the mobile terminal.

  1. vue-touch-ripple

vue-touch-ripple is a touch ripple effect plugin based on vue.js, which can add touch ripple effect to any element. we can use it to achieve the sliding effect of mobile floating window.

first, we need to import the vue-touch-ripple dependency:

npm install vue-touch-ripple --save

then, in the component that needs to use the touch ripple effect, import and register vue-touch-ripple:

import vuetouchripple from 'vue-touch-ripple'
export default {
  directives: {
    'ripple': vuetouchripple.directive
  }
}

next, use the ripple directive in the template to display the touch ripple effect:

<template>
  <divv-ripple>contentdiv>
template>

in this way, we can achieve the sliding effect of the floating window on the mobile terminal.

2. write your own code

if we don’t want to use third-party plug-ins, we can also write our own code to implement the drag and slide effects of mobile floating windows.

  1. realize the drag effect of floating window

first, we need to add touchstart, touchmove and touchend events to the floating window component's template:

<template>
  <divclass="float-window"@touchstart="ontouchstart"@touchmove="ontouchmove"@touchend="ontouchend"
  >
    content
  div>
template>

next, define the ontouchstart, ontouchmove and ontouchend methods in the component's script:

export default {
  data() {
    return {
      startx: 0,
      starty: 0,
      offsetx: 0,
      offsety: 0,
      left: 0,
      top: 0
    }
  },
  methods: {
    ontouchstart(e) {
      this.startx = e.touches[0].clientx
      this.starty = e.touches[0].clienty
      this.offsetx = this.left
      this.offsety = this.top
    },
    ontouchmove(e) {
      const x = e.touches[0].clientx - this.startx   this.offsetx
      const y = e.touches[0].clienty - this.starty   this.offsety
      const maxx = window.innerwidth - this.$refs.floatwindow.offsetwidth
      const maxy = window.innerheight - this.$refs.floatwindow.offsetheight
      this.left = x < 0 ? 0 : (x > maxx ? maxx : x)
      this.top = y < 0 ? 0 : (y > maxy ? maxy : y)
    },
    ontouchend(e) {
      // do nothing
    }
  }
}

in the ontouchstart method, we record the position of the touch start point and the offset of the floating window. in the ontouchmove method, we calculate the new position of the floating window based on the touch movement distance and the offset of the floating window, and limit the movement range of the floating window. in the ontouchend method, we do not do anything for now.

finally, define the position of the floating window in the component's style:

<stylescoped>.float-window {
  position: fixed;
  left: 0;
  top: 0;
  transform: translate3d(0, 0, 0);
}
style>

in this way, we can realize the drag effect of the floating window on the mobile terminal.

  1. realize the sliding effect of floating window

next, we need to add touchstart, touchmove and touchend events to the floating window component's template:

<template>
  <divclass="float-window"@touchstart="ontouchstart"@touchmove="ontouchmove"@touchend="ontouchend"
  >
    content
  div>
template>

next, define the ontouchstart, ontouchmove and ontouchend methods in the component's script:

export default {
  data() {
    return {
      startx: 0,
      starty: 0,
      offsetx: 0,
      offsety: 0,
      left: 0,
      top: 0,
      isscrolling: false
    }
  },
  methods: {
    ontouchstart(e) {
      this.startx = e.touches[0].clientx
      this.starty = e.touches[0].clienty
      this.offsetx = this.left
      this.offsety = this.top
      this.isscrolling = false
    },
    ontouchmove(e) {
      const x = e.touches[0].clientx - this.startx   this.offsetx
      const y = e.touches[0].clienty - this.starty   this.offsety
      const maxx = window.innerwidth - this.$refs.floatwindow.offsetwidth
      const maxy = window.innerheight - this.$refs.floatwindow.offsetheight
      this.left = x < 0 ? 0 : (x > maxx ? maxx : x)
      this.top = y < 0 ? 0 : (y > maxy ? maxy : y)
      if (math.abs(e.touches[0].clientx - this.startx) > 5 || math.abs(e.touches[0].clienty - this.starty) > 5) {
        this.isscrolling = true
      }
    },
    ontouchend(e) {
      if (!this.isscrolling) {
        if (e.changedtouches[0].clientx < window.innerwidth / 2) {
          this.left = 0
        } else {
          this.left = window.innerwidth - this.$refs.floatwindow.offsetwidth
        }
      }
    }
  }
}

in the ontouchstart method, we record the position of the touch start point and the offset of the floating window, and set the isscrolling flag to false. in the ontouchmove method, we calculate the new position of the floating window based on the touch movement distance and the offset of the floating window, and limit the movement range of the floating window. at the same time, if the touch movement distance exceeds 5px, we set the isscrolling flag to true. in the ontouchend method, if the isscrolling flag is false, it means that the touch is sliding rather than dragging, and we set the position of the floating window according to the touch end position.

finally, define the position of the floating window in the component's style:

<stylescoped>.float-window {
  position: fixed;
  left: 0;
  top: 0;
  transform: translate3d(0, 0, 0);
  transition: left 0.3s ease-out;
}
style>

in this way, we can achieve the sliding effect of the floating window on the mobile terminal.

example

suppose we need to implement a floating window component on the mobile terminal that can be dragged and slid. we can first write a floatwindow component:

<template>
  <divclass="float-window"@touchstart="ontouchstart"@touchmove="ontouchmove"@touchend="ontouchend"ref="floatwindow":style="{left: left   'px', top: top   'px'}"
  >
    <slot>slot>
  div>
template>
<script>exportdefault {
  data() {
    return {
      startx: 0,
      starty: 0,
      offsetx: 0,
      offsety: 0,
      left: 0,
      top: 0,
      isscrolling: false
    }
  },
  methods: {
    ontouchstart(e) {
      this.startx = e.touches[0].clientx
      this.starty = e.touches[0].clienty
      this.offsetx = this.left
      this.offsety = this.top
      this.isscrolling = false
    },
    ontouchmove(e) {
      const x = e.touches[0].clientx - this.startx   this.offsetx
      const y = e.touches[0].clienty - this.starty   this.offsety
      const maxx = window.innerwidth - this.$refs.floatwindow.offsetwidth
      const maxy = window.innerheight - this.$refs.floatwindow.offsetheight
      this.left = x < 0 ? 0 : (x > maxx ? maxx : x)
      this.top = y < 0 ? 0 : (y > maxy ? maxy : y)
      if (math.abs(e.touches[0].clientx - this.startx) > 5 || math.abs(e.touches[0].clienty - this.starty) > 5) {
        this.isscrolling = true
      }
    },
    ontouchend(e) {
      if (!this.isscrolling) {
        if (e.changedtouches[0].clientx < window.innerwidth / 2) {
          this.left = 0
        } else {
          this.left = window.innerwidth - this.$refs.floatwindow.offsetwidth
        }
      }
    }
  }
}
script>
<stylescoped>.float-window {
  position: fixed;
  left: 0;
  top: 0;
  transform: translate3d(0, 0, 0);
  transition: left 0.3s ease-out;
}
style>

we can then import the floatwindow component in the parent component and use it:

<template>
  <div>
    <float-window>
      floating window content
    float-window>
  div>
template>
<script>importfloatwindowfrom'./floatwindow.vue'exportdefault{
  components: {
    floatwindow
  }
}
script>

in this way, we can implement a floating window component that can be dragged and slid on the mobile terminal.

for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.

article url:

related articles

what is the use of the immediate property of watch in vue?

publish date:2025/03/01 views:65 category:vue

in vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. in most cases, watch will be delayed by default. this means that the watch will only be triggered when the value being monitored chang

setting up checkbox functionality in vue

publish date:2025/03/01 views:185 category:vue

in vue, checkboxes are a very common interactive component that allows users to select multiple options. this article will introduce how to set up checkbox functionality in vue and provide some practical examples.

what happens if a child component changes the data in props in vue?

publish date:2025/03/01 views:138 category:vue

in vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. first, you need to understand that props is a way to pass data from a parent component t

how to refresh the page in vue

publish date:2025/03/01 views:85 category:vue

vue is a popular javascript framework that provides many convenient tools and methods for building web applications. in vue, page updates are usually achieved through data binding and responsive systems. but sometimes you need to refresh the

how to find all elements by class name in vue

publish date:2025/03/01 views:182 category:vue

vue is a very powerful javascript framework that provides developers with a lot of convenient features and tools. one of them is finding all elements by class name. in this article, we will explore how to find all elements by class name in

how to get the initial state of a certain data in vue?

publish date:2025/03/01 views:146 category:vue

in vue, sometimes we want to get the initial state of a data in data so that we can compare or reset it in subsequent operations. in this article, we will introduce how to get the initial state of a data in data and provide an example to il

implementation of scheduling execution in vue project

publish date:2025/03/01 views:128 category:vue

in vue projects, scheduling execution refers to assigning tasks to different components or instances to implement functions such as data processing, rendering, and interaction. this article will introduce the implementation method of schedul

defining global components in vue

publish date:2025/03/01 views:99 category:vue

in vue, global components are a very important concept that allows us to use the same components everywhere. in this article, we will discuss how to define global components in vue and the relevant precautions.

scan to read all tech tutorials

social media
  • https://www.github.com/onmpw
  • qq:1244347461

recommended

tags

scan the code
easier access tutorial
网站地图