본문 바로가기

Web/JavaScript

브라우저 이벤트 정리 3. drag and drop

728x90

 

 

https://html.spec.whatwg.org/multipage/dnd.html#dnd

 

HTML Standard

This section defines an event-based drag-and-drop mechanism. This specification does not define exactly what a drag-and-drop operation actually is. On a visual medium with a pointing device, a drag operation could be the default action of a mousedown event

html.spec.whatwg.org

 

 

 

drag and drop algorithm

 

mousedown -> mousemove -> mouseup (ondragstart cancel)

drag 시작 시에 포인터 초기 이동 기억 후 유지 (shiftX, shiftY)

elementFromPoint 로 드롭 요소 감지

 

  • mouseup에서 데이터를 변경하고, 요소를 이동하는 등 지적으로 드롭을 끝낼 수 있다
  • 특정 영역이나 방향으로 드래그하는 것을 제한할 수 있다.
  • mousedown/up에 이벤트 위임을 사용할 수 있다. event.target을 확인하는 넓은 영역의 이벤트 핸들러는 수백 개의 요소에 대한 드래그 앤 드롭을 관리할 수 있다.
728x90