Computer >> 컴퓨터 >  >> 프로그램 작성 >> HTML

HTML ondragleave 이벤트 속성

<시간/>

HTML ondragleave 이벤트 속성은 드래그 가능한 요소 또는 텍스트가 HTML 문서의 유효한 놓기 대상을 벗어날 때 트리거됩니다.

구문

다음은 구문입니다 -

<tagname ondragleave=”script”></tagname>

HTML ondragleave 이벤트 Attribute-

의 예를 살펴보겠습니다.

예시

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      color: #000;
      height: 100vh;
      background-color: #FBAB7E;
      background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
      text-align: center;
   }
   .drop-target {
      display: inline-block;
      width: 150px;
      height: 150px;
      border: 2px solid #FFF;
      margin: 1rem;
      vertical-align: middle;
      padding: 20px;
   }
   .circle {
      background: #db133a;
      height: 40px;
      width: 40px;
      border-radius: 50%;
   }
   .show {
      color: #fff;
      font-size: 1.2rem;
   }
</style>
</head>
<body>
<h1>HTML ondragleave Event Attribute Demo</h1>
<div class="drop-target" ondrop="drop(event)" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" ondragover="allowDrop(event)">
<p ondragstart="dragStart(event)" draggable="true" id="dragtarget" class='circle'></p>
</div>
<div class="drop-target" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<p>Now try to drag and drop the red circle</p>
<div class="show"></div>
<script>
   function dragStart(event) {
      event.dataTransfer.setData("Text", event.target.id);
   }
   function dragEnter(event) {
      if (event.target.className == "drop-target") {
         event.target.style.background = "#db133a3d";
         document.querySelector(".show").innerHTML = "Passed into the dropzone";
      }
   }
   function dragLeave(event) {
      if (event.target.className == "drop-target") {
         event.target.style.background = "transparent";
         document.querySelector(".show").innerHTML = "Passed out the dropzone";
      }
   }
   function allowDrop(event) {
      event.preventDefault();
   }
   function drop(event) {
      event.preventDefault();
      var data = event.dataTransfer.getData("Text");
      event.target.appendChild(document.querySelector('.circle'));
   }
</script>
</body>
</html>

출력

HTML ondragleave 이벤트 속성

이제 두 상자 사이에 빨간색 원을 끌어다 놓아 ondragstart 이벤트 속성이 작동하는 방식을 관찰합니다.

HTML ondragleave 이벤트 속성