Vue makes div height draggable

Vue makes div height draggable

This article shares the specific code of Vue to realize the draggable height of div for your reference. The specific content is as follows

Here is a ready-made demo that can realize the dragging function of the page div, but the effect is not quite the same as what I want, so I modified it again according to my actual needs. Let’s take a look at the current demo effect first.

<template>
  <div id="eagleMapContainer" style="border: 1px solid red;overflow-y: auto;" title="">
    <div id="tz" @mousedown="dragEagle" style="border: 1px solid blue;">
      <div title="Drag to resize" id="move_tz" style="border: 1px solid green;"></div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "eagleMap",
    data() {
      return {}
    },
    methods: {
      dragEagle: function (e) {
        var targetDiv = document.getElementById('eagleMapContainer'); 
        //Get the width and height of the map container when clicked:
        var targetDivHeight = targetDiv.offsetHeight;
        var startX = e.clientX;
        var startY = e.clientY;
        var _this = this;
        document.onmousemove = function (e) {
          e.preventDefault();
          //Get the width and height of the mouse drag: take the absolute value var distX = Math.abs(e.clientX - startX);
          var distY = Math.abs(e.clientY - startY);
          //Drag upwards:
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight + distY + 'px';
          }
          //Drag downwards:
          if (e.clientX < startX && e.clientY > startY) {
            targetDiv.style.height = (targetDivHeight - distY) + 'px';
          }
          if (parseInt(targetDiv.style.height) >= 300) {
            targetDiv.style.height = 300 + 'px';
          }
          if (parseInt(targetDiv.style.height) <= 150) {
            targetDiv.style.height = 150 + 'px';
          }
        }
        document.onmouseup = function () {
          document.onmousemove = null;
        }
      }
    },
  };
</script>

<style scoped>
  #eagleMapContainer {
    position: absolute;
    left: 13%;
    bottom: 10px;
    z-index: 200;
    overflow: hidden;
    visibility: visible;
    width: 200px;
    height: 200px;
  }

  #tz {
    position: absolute;
    right: 1px;
    top: 1px;
    width: 27px;
    height: 20px;
    cursor:ne-resize;
    z-index: 200001;
    background-image: url("");

  }

  #tz:hover {
    background-color: #666;
  }

  #move_tz {
    position: absolute;
    right: 0px;
    top: 0px;
    width: 27px;
    height: 20px;
    cursor:ne-resize;
    z-index: 100;
    background-image: url("");
    background-position: 0px 0px;
  }
</style> 

But the effect was not quite what I wanted, so I had to modify it a little bit.

The effect I want is: I have a div that contains a lot of small square lists. Because the scrolling is set beyond the limit, I add a drag to the div with the scroll bar to achieve height change.

The next step is to transform the demo above. To make it simple, just go to the code:

Add a div below the div that needs to be dragged above, and click on this div to start the dragging function.

<!-- Drag and drop small box -->
    <div id="tz" @mousedown="dragEagle">
      <div title="Drag to resize" id="move_tz"></div>
    </div>

You need to set an id for the div that changes its height based on dragging, assuming it is "fuDiv", and then write a method.

// dragEagle(e) {
        var targetDiv = document.getElementById('fuDiv');
        //Get the width and height of the map container when clicked:
        var targetDivHeight = targetDiv.offsetHeight;
        var startX = e.clientX;
        var startY = e.clientY;
        var _this = this;
        document.onmousemove = function (e) {
          e.preventDefault();
          //Get the width and height of the mouse drag: take the absolute value var distY = Math.abs(e.clientY - startY);

          //Drag upwards:
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight - distY + 'px';
          }
          //Drag downwards:
          if (e.clientX < startX && e.clientY > startY) {
            targetDiv.style.height = (targetDivHeight + distY) + 'px';
          }
          if (parseInt(targetDiv.style.height) >= 320) {
            targetDiv.style.height = 320 + 'px';
          }
          if (parseInt(targetDiv.style.height) <= 160) {
            targetDiv.style.height = 160 + 'px';
          }
        }
        document.onmouseup = function () {
          document.onmousemove = null;
        }
      },

Then set the CSS style for them. In fact, this part is casual and can be done according to your own preferences.

  #tz {
    width: 100%;
    height: 5px;
    cursor: s-resize;
    z-index: 200001;
  }
  
  #move_tz {
    width: 100%;
    height: 5px;
    cursor: s-resize;
    z-index: 100;
    background-image: url("");
    background-position: 0px 0px;
  }

Final result:

The effect is not particularly good, and there are still many areas that deserve optimization, which I will not write for now.

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:
  • How to implement draggable components in Vue
  • Vue implements draggable dialog box
  • Vue suspended draggable floating button example code
  • Vue implements the draggable function of element-ui dialog box
  • Implementing the draggable effect of el-dialog component in vue project

<<:  About the VMware vcenter unauthorized arbitrary file upload vulnerability (CVE-2021-21972)

>>:  Web page HTML code explanation: ordered list and unordered list

Recommend

How to install and persist the postgresql database in docker

Skip the Docker installation steps 1. Pull the po...

Specific use of Linux which command

We often want to find a file in Linux, but we don...

Detailed explanation of the functions of each port of Tomcat

From the tomcat configuration file, we can see th...

Detailed Analysis of the Selection of MySQL Common Index and Unique Index

Suppose a user management system where each perso...

HTML unordered list bullet points using images CSS writing

Create an HTML page with an unordered list of at l...

Document Object Model (DOM) in JavaScript

Table of contents 1. What is DOM 2. Select elemen...

How to build and deploy Node project with Docker

Table of contents What is Docker Client-side Dock...

Summary of Several Methods for Implementing Vertical Centering with CSS

In the front-end layout process, it is relatively...

Troubleshooting the cause of 502 bad gateway error on nginx server

The server reports an error 502 when synchronizin...

How to start a Java program in docker

Create a simple Spring boot web project Use the i...

Introduction to vim plugin installation under Linux system

Table of contents Install vim plugin manager Add ...

Solve the scroll-view line break problem of WeChat applet

Today, when I was writing a small program, I used...

MySQL 8.0.15 compressed version installation graphic tutorial

This article shares the installation method of My...