Based on Vue The core idea of this function is to control the left and top margins of the node on the page through JavaScript code. Different style positioning methods have different solutions. This solution adopts CSS style core code // Parent container core style width: 100%; height: 100%; //Subcontainer core style position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); The parent container occupies the entire visible range of the browser through the JavaScript core code for logic controlFirst, let's break down the steps and corresponding events required to achieve node movement.
As long as you use the three events onMousedown, onMousemove and onMouseup, you can achieve the simplest movement /* * Get the top and left position of the child container relative to the parent container when the child container is created */ mounted () { this.left = this.$refs.fatherBox.offsetLeft this.top = this.$refs.fatherBox.offsetTop } /* * When the mouse is pressed* 1. Enable the flag that allows child containers to move * 2. Record the location information when the mouse is clicked*/ mouseDown (e) { // Set the flag to allow pop-up window to move this.moveFlag = true // Save the starting position of the mouse this.startLeft = e.clientX - this.left this.startTop = e.clientY - this.top } /* * When the mouse moves* 1. Determine whether the flag allows the child container to move* 2. Set the left position of the pop-up box* 3. Set the right position of the pop-up box*/ move (e) { // Check if flag allows movement if (!this.moveFlag) return // Set the left position of the bullet box this.left = e.clientX - this.startLeft // Set the right position of the bullet box this.top = e.clientY - this.startTop } /* * When the mouse button is released* 1. Turn off the flag that allows child containers to move */ mouseUp (e) { this.flag = false } Through these methods, you can get the top and left offsets of the mouse when the mouse is pressed and moved. By exposing this offset to the parent component, the parent component sets the top and left values of the child component in real time to make the child container follow the movement of the mouse. Parent component codeThe parent component sets the ref and zIndex values of the child component, and the backValue method of the parent component receives the zIndex value from the child component, and uses the zIndex to identify the specific child component instance. /* * Parent component code snippet jsx version */ export default { props: { playList: { type: Array, required: true } }, render () { return ( <div style={{width: '100%', height: '100%'}} ref={'father'}> { this.playList && this.playList.map((item, index) => { return ( <ChildComponents key={index} ref={index} zIndex={index} visible={true} backValue={this.backValue} info={item} width={600} height={400} /> ) }) } </div> ) }, methods: { backValue (left, top, zIndex) { this.$refs[zIndex].$el.style.top = `${top}px` this.$refs[zIndex].$el.style.left = `${left}px` } } } <!-- Parent component code snippet vue file version --> <template> <div ref="father" style"width: 100%, height: 100%" > <ChildComponents v-for="(item, index) in playList" :key="index" :ref="index" :visible="true" :z-index="index" :back-value="backValue" :info="item" :close="close" :width="600" :height="400" /> </div> </template> <script> export default { components: VideoPlayerModal }, props: { playList: { type: Array, required: true } }, methods: { backValue (left, top, zIndex) { this.$refs[zIndex][0].$el.style.top = `${top}px` this.$refs[zIndex][0].$el.style.left = `${left}px` } } } </script> Set the fence range of the child componentThis function only needs to determine whether the top and left of the child container are beyond the visible range of the browser in the onmousemove event. /* * 1. this.width data is the width value passed in by the parent component, or the default value set by the child component itself* 2. this.height data is the height value passed in by the parent component, or the default value set by the child component itself*/ move (e) { // Check if flag allows movement if (!this.moveFlag) return // Determine whether it exceeds the left view if (this.$refs.fatherBox.offsetLeft < this.width / 2) { // Disable the pop-up box from moving this.moveFlag = false // Set the left position of the bullet box this.left = this.width / 2 + 10 // Call the callback function to expose the offset to the parent component this.backValue(this.left, this.top, this.zIndex) return } // Determine whether it exceeds the right view if (this.$refs.fatherBox.offsetLeft > document.body.clientWidth - this.width / 2) { // Disable the pop-up box from moving this.moveFlag = false // Set the right position of the bullet box this.left = document.body.clientWidth - this.width / 2 - 10 // Call the callback function to expose the offset to the parent component this.backValue(this.left, this.top, this.zIndex) return } // Determine whether it exceeds the top view if (this.$refs.fatherBox.offsetTop < this.height / 2 + 70) { // Disable the pop-up box from moving this.moveFlag = false // Set the top position of the bullet box this.top = this.height / 2 + 70 + 10 // Call the callback function to expose the offset to the parent component this.backValue(this.left, this.top, this.zIndex) return } // Determine whether it exceeds the bottom view if (this.$refs.fatherBox.offsetTop > document.body.clientHeight - this.height / 2 - 50) { // Disable the pop-up box from moving this.moveFlag = false // Set the bottom position of the bullet box this.top = document.body.clientHeight - this.height / 2 - 50 - 10 // Call the callback function to expose the offset to the parent component this.backValue(this.left, this.top, this.zIndex) return } // Set the left position of the bullet box this.left = e.clientX - this.startLeft // Set the right position of the bullet box this.top = e.clientY - this.startTop // Call the callback function to expose the offset to the parent component this.backValue(this.left, this.top, this.zIndex) } The subcomponent also needs to set an onmouseout event when the mouse exceeds the subcontainer to prevent unexpected bugs. mouseOut (e) { this.moveFlag = false } This is the end of this article about HTML drag-and-drop function. For more relevant HTML drag-and-drop function content, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! |
<<: CSS tips for implementing Chrome tab bar
For individual webmasters, how to make their websi...
The use of vue3 Teleport instant movement functio...
Click here to return to the 123WORDPRESS.COM HTML ...
I want to use the marquee tag to set the font scro...
Table of contents 1. Introduction 2. Main text 2....
MySQL master-slave replication allows data from o...
Table of contents 1. Introduction to Portainer 2....
When writing the HTTP module of nginx, it is nece...
Today I will share with you a source code contain...
Table of contents Preface 1. Linux changes the yu...
1. Basic Concepts //Any container can be specifie...
Table of contents introduction Indexing principle...
Table of contents 1. The origin of fork 2. Early ...
1. What is In react applications, event names are...
Preface Whether it is a stand-alone lock or a dis...