vue+ts realizes the effect of element mouse drag

vue+ts realizes the effect of element mouse drag

This article example shares the specific code of vue+ts to achieve the element mouse drag effect for your reference. The specific content is as follows

Achieve results

Related usage attributes

// clientX is the x-axis coordinate of the mouse relative to the upper left corner of the browser; it does not change with the scroll bar;
// clientY The coordinate of the mouse on the y-axis relative to the upper left corner of the browser; does not change with the scroll bar;
// element.offsetTop refers to the distance between element and the upper or upper control, integer, unit pixel.
// element.offsetLeft refers to the position of element from the left or upper control, integer, unit pixel.
// element.offsetWidth refers to the width of the element control itself, integer, unit pixel.
// element.offsetHeight refers to the height of the element control itself, integer, unit pixel.
// clientHeigh = height + top and bottom padding
// clientWidth = width + left and right padding

Implement the complete code

<template>
  <div class="to-do-list" ref="parentBox">
    <div class="search-title">
      <h1 class="add-bold left-box" style="margin-left:35px">
        <a-icon type="unordered-list" style="margin-right: 10px;" />Element drag</h1>
    </div>
    <a-button ref="moveBtn" style="width: 100px;height: 40px;transition:none" class="move-btn" type="primary"
      >Drag button</a-button
    >
  </div>
</template>

<script lang="ts">
import { Component, Emit, Inject, Prop, Ref, Vue, Watch } from 'vue-property-decorator';

@Component({
  components: {},
})
export default class BriberyInformation extends Vue {
  @Ref() readonly moveBtn;
  @Ref() readonly parentBox;

  btnDown() {
    let box = this.moveBtn.$el; //Get the button's box DOM element let parentBox = this.parentBox; //Get the button's parent DOM element let parentH = parentBox.clientHeight; //Get the height of the button's parent element
    let parentW = parentBox.clientWidth; //Get the width of the button parent element

    let x, y;
    let moveX, moveY; //moving distancelet maxX, maxY; //maximum moving distancelet isDrop = false;

    box.onmousedown = e => {
      x = e.clientX - box.offsetLeft; // e.clientX mouse coordinate relative to the x-axis of the upper left corner of the browser - the position of the button upper control y = e.clientY - box.offsetTop;
      isDrop = true;
    };
    document.onmousemove = e => {
      if (isDrop) {
        e.preventDefault();
        moveX = e.clientX - x; //Get the moving distance from the left side moveY = e.clientY - y; //Get the moving distance from the top //Maximum movable distance maxX = parentW - box.offsetWidth;
        maxY = parentH - box.offsetHeight;

        //Calculation of effective moving distance //console.log(Math.min(-1, 4, 6, 12)); //Output -1-----Multiple parameters, return the minimum value moveX = Math.min(maxX, Math.max(0, moveX));

        moveY = Math.min(maxY, Math.max(0, moveY));
        box.style.left = moveX + 'px';
        box.style.top = moveY + 'px';
      } else {
        return;
      }
    };
    document.onmouseup = e => {
      e.preventDefault();
      isDrop = false;
    };
  }

  mounted() {
    this.btnDown();
  }
}
</script>
<style scoped lang="less">
.to-do-list {
  position: relative;
  min-height: 600px;
  max-height: 600px;
  width: 600px;
  overflow: hidden;
  border: 2px solid black;
  .move-btn {
    position: absolute;
  }
}
</style>

Reference source: Using JavaScript to implement the mouse drag effect of div

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue global custom instruction-element drag implementation code
  • Vuejs achieves adaptiveness by changing the width of elements by dragging
  • Detailed code about Vue custom instructions to implement element dragging

<<:  Write a shopping mall card coupon using CSS in three steps

>>:  VMware + Ubuntu18.04 Graphic Tutorial on Building Hadoop Cluster Environment

Recommend

Detailed explanation of Nginx timed log cutting

Preface By default, Nginx logs are written to a f...

Analyze how to automatically generate Vue component documentation

Table of contents 1. Current situation 2. Communi...

How to configure multiple projects with the same domain name in Nginx

There are two ways to configure multiple projects...

Implementation of MySQL multi-version concurrency control MVCC

Transaction isolation level settings set global t...

How to view and execute historical commands in Linux

View historical commands and execute specified co...

MySQL index knowledge summary

The establishment of MySQL index is very importan...

Mysql implements null value first/last method example

Preface We already know that MySQL uses the SQL S...

How to install mysql database in deepin 2014 system

Deepin 2014 download and installation For downloa...

MySQL 8.0.13 installation and configuration graphic tutorial

Msyql database installation, for your reference, ...

How to configure mysql5.6 to support IPV6 connection in Linux environment

Introduction: This article mainly introduces how ...

VUE+SpringBoot implements paging function

This article mainly introduces how to implement a...

JavaScript Sandbox Exploration

Table of contents 1. Scenario 2. Basic functions ...

Detailed explanation of Javascript basics loop

Table of contents cycle for for-in for-of while d...

Implementation of Docker deployment of Nuxt.js project

Docker official documentation: https://docs.docke...