Home > Web Front-end > JS Tutorial > How to use JavaScript to implement drag and drop functionality

How to use JavaScript to implement drag and drop functionality

WBOY
Release: 2023-06-15 12:35:38
Original
6510 people have browsed it

With the continuous development of Web applications, the drag-and-drop function has become an indispensable part of Web interface design. This function allows users to complete certain operations by dragging objects, and the operating experience is very smooth and natural. In this article, I will show you how to use JavaScript to implement drag and drop functionality.

Step One: Preparation

Before implementing the drag and drop function, we need to clarify some prerequisites. First, you need to determine the element that needs to be dragged and dropped, and then set the corresponding CSS properties for the element so that it can be dragged. The sample CSS code is as follows:

.draggable {
  cursor: move; /*将鼠标指针变为可移动形状*/
}
Copy after login

Step 2: Event Binding

Next, we need to bind events so that elements can be dragged. In JavaScript, we can use the addEventListener() method to bind events and add mousedown, mousemove, and mouseup event listeners.

In the mousedown event, we need to record the offset of the mouse position and element position. In the mousemove event, we update the element's position based on how far the mouse has moved. In the mouseup event, we need to stop dragging and release the event binding.

The following is the JavaScript code to implement the drag and drop function:

const draggableElement = document.querySelector('.draggable');
let offsetX, offsetY;

draggableElement.addEventListener('mousedown', startDragging);

function startDragging(event) {
  offsetX = event.clientX - draggableElement.offsetLeft;
  offsetY = event.clientY - draggableElement.offsetTop;
  
  document.addEventListener('mousemove', drag);
  document.addEventListener('mouseup', stopDragging);
}

function drag(event) {
  draggableElement.style.left = event.clientX - offsetX + 'px';
  draggableElement.style.top = event.clientY - offsetY + 'px';
}

function stopDragging() {
  document.removeEventListener('mousemove', drag);
  document.removeEventListener('mouseup', stopDragging);
}
Copy after login

Step 3: Limit the drag range

If you need to restrict the element from being dragged in a certain area, We can add corresponding judgment conditions in the drag() function, as shown in the following code:

function drag(event) {
  let left = event.clientX - offsetX;
  let top = event.clientY - offsetY;
  
  // 限制拖拽范围
  let maxX = window.innerWidth - draggableElement.offsetWidth;
  let maxY = window.innerHeight - draggableElement.offsetHeight;
  left = Math.min(Math.max(0, left), maxX);
  top = Math.min(Math.max(0, top), maxY);
  
  draggableElement.style.left = left + 'px';
  draggableElement.style.top = top + 'px';
}
Copy after login

In this example, we use the Math.min() and Math.max() functions to ensure that the elements are in Move within the specified range. Use window.innerWidth and window.innerHeight to get the width and height of the window.

Step 4: Implement the alignment function

It is often necessary to align elements to other elements or guides on the page. You can define a function that rounds the position of an element to the nearest reference position.

The following is a sample code to implement the alignment function:

function snapToGrid(element, gridSize) {
  let snapLeft = Math.round(element.offsetLeft / gridSize) * gridSize;
  let snapTop = Math.round(element.offsetTop / gridSize) * gridSize;
  
  element.style.left = snapLeft + 'px';
  element.style.top = snapTop + 'px';
}
Copy after login

In this example, we first calculate the position of the left and top of the element in the grid, and then use Math.round( ) function rounds it to the nearest integer multiple. Finally use the calculated position to update the element's position.

Using these techniques, we can easily implement the drag function and make the drag effect more natural and flexible.

The above is the detailed content of How to use JavaScript to implement drag and drop functionality. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template