首页 > 其他分享 >写个方法,实现对某个节点的拖拽

写个方法,实现对某个节点的拖拽

时间:2024-12-10 09:57:57浏览次数:4  
标签:写个 offsetX element draggable isDragging 节点 dragging Optional 拖拽

function makeDraggable(element) {
  let isDragging = false;
  let offsetX, offsetY;

  element.addEventListener('mousedown', (e) => {
    isDragging = true;
    offsetX = e.clientX - element.offsetLeft;
    offsetY = e.clientY - element.offsetTop;

    // Optional: Add a visual cue that the element is being dragged
    element.classList.add('dragging');

    // Prevent default text selection
    e.preventDefault();
  });

  document.addEventListener('mousemove', (e) => {
    if (isDragging) {
      element.style.left = (e.clientX - offsetX) + 'px';
      element.style.top = (e.clientY - offsetY) + 'px';
    }
  });

  document.addEventListener('mouseup', () => {
    isDragging = false;

    // Optional: Remove the visual cue
    element.classList.remove('dragging');
  });


  //Optional: Prevent dragging from starting on child elements of the draggable element if you only want the parent to initiate dragging.
  //This is useful for elements with interactive content inside.
  element.addEventListener('mousedown', (e) => {
    if (e.target !== element) {
      return;
    }
     // Original mousedown logic here if needed. For example:
     isDragging = true;
     offsetX = e.clientX - element.offsetLeft;
     offsetY = e.clientY - element.offsetTop;
     element.classList.add('dragging');
     e.preventDefault();
  }, true); // Note the use of `true` for event capturing

};


// Example usage:
const myElement = document.getElementById('my-draggable-element');
makeDraggable(myElement);


Explanation and Key Improvements:

  • offsetX and offsetY: These variables store the initial difference between the mouse position and the element's top-left corner. This ensures the element moves smoothly relative to where you clicked it.
  • isDragging flag: This boolean variable tracks whether the dragging operation is active. This prevents unintended movement when the mouse moves without an initial mousedown on the element.
  • preventDefault(): This prevents the default behavior of text selection while dragging, which can interfere with the dragging experience.
  • Optional Visual Cue: The code includes an optional class toggle (dragging) to provide visual feedback during the drag. You can style this class in your CSS (e.g., changing the cursor or adding a drop shadow).
  • mouseup on document: Listening for the mouseup event on the document ensures the drag operation stops even if the mouse is released outside the element's boundaries.
  • Optional: Preventing dragging on children: The added event listener with the capture option set to true (the third argument of addEventListener) allows you to prevent dragging from being initiated if the mousedown event originates on a child element of the draggable element. This is particularly useful for elements with interactive content inside, where clicks on those inner elements should perform their intended function rather than start a drag.

CSS Considerations:

You'll likely need to add the following CSS to the draggable element:

#my-draggable-element {
  position: absolute; /* or fixed, depending on your needs */
  /* other styles... */
}

.dragging {
  cursor: move; /* or grabbing */
  /* other optional styles, e.g., box-shadow */
}
  • position: absolute or position: fixed: This is crucial. Without this, the left and top styles won't work correctly. absolute positions the element relative to its nearest positioned ancestor, while fixed positions it relative to the viewport.

This improved version provides a more robust and user-friendly dragging experience. Remember to replace 'my-draggable-element' with the actual ID of your element.

标签:写个,offsetX,element,draggable,isDragging,节点,dragging,Optional,拖拽
From: https://www.cnblogs.com/ai888/p/18596700

相关文章