Implement mobile terminal drag floating window sliding effect in vue
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
- 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>
<draggable v-model="items">
<div v-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.
- 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>
<div v-ripple>Content</div>
</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.
- 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>
<div class="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:
<style scoped>
.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.
- 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>
<div class="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:
<style scoped>
.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>
<div class="float-window"
@touchstart="onTouchStart"
@touchmove="onTouchMove"
@touchend="onTouchEnd"
ref="floatWindow"
:style="{left: left + 'px', top: top + 'px'}"
>
<slot></slot>
</div>
</template>
<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
}
}
}
}
}
</script>
<style scoped>
.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>
import FloatWindow from './FloatWindow.vue'
export default {
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.
Related Articles
Various ways to format dates in Vue
Publish Date:2025/02/26 Views:68 Category:Vue
-
Vue is a popular front-end framework that provides many features, including multiple ways to format dates. In this article, we will introduce several methods that can help you format dates in Vue. Using moment.js moment.js is a popular
The difference between strict mode and non-strict mode in Vue
Publish Date:2025/02/26 Views:169 Category:Vue
-
Vue.js is a very popular JavaScript framework. Its design goal is to help developers build efficient and flexible web applications. Vue.js provides two different modes: strict mode and non-strict mode. These two modes are used in the develop
Click the button to switch the background color in vue
Publish Date:2025/02/26 Views:160 Category:Vue
-
Vue is a popular JavaScript framework for building interactive web applications. Vue provides many powerful features, one of which is the ability to easily toggle the background color when a button is clicked. In this article, we will show y
Introducing the bootstrap framework into the vue project
Publish Date:2025/02/26 Views:148 Category:Vue
-
Bootstrap is a popular front-end framework that provides a wealth of CSS and JavaScript components to help developers quickly build modern responsive websites and applications. Introducing the Bootstrap framework in a Vue project can provide
Using iframe to nest pages in Vue
Publish Date:2025/02/26 Views:177 Category:Vue
-
With the development of the Internet, websites are becoming more and more complex, and various technologies are needed to implement them. Among them, iframe technology is a commonly used technology that can embed a web page into another web
Different ways to send POST requests in Vue
Publish Date:2025/02/26 Views:82 Category:Vue
-
In Vue, sending POST requests is a very common operation, which can be achieved in many ways. This article will introduce several methods of sending POST requests in Vue, and will explain them in detail with examples. Method 1: Use Vue-resou
Importing JSON files into Vue
Publish Date:2025/02/26 Views:100 Category:Vue
-
Vue.js is a popular JavaScript framework that can quickly build single-page applications (SPA). In Vue.js, we can use JSON files to store data, which is very useful in development. This article will introduce how to introduce JSON files in V
Creating a sticky footer in Vue
Publish Date:2025/02/26 Views:116 Category:Vue
-
In web design, the footer is one of the most important parts of the page. It usually contains copyright information, contact information, and other relevant information about the website. However, when users scroll the page, the footer may d
How to introduce style files in Vue
Publish Date:2025/02/26 Views:181 Category:Vue
-
Vue is a popular front-end framework that allows developers to build single-page applications in a componentized way. During the development process, we usually need to introduce style files to beautify the page. This article will introduce