aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/document/drag_event/index.html
blob: 5a1a2b49920741986df40f51285a787a565e6b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
title: 'Document: drag イベント'
slug: Web/API/Document/drag_event
tags:
  - API
  - DOM
  - Document
  - Drag
  - DragEvent
  - Event
  - Reference
  - Web
  - drag and drop
  - イベント
  - ウェブ
translation_of: Web/API/Document/drag_event
---
<div>{{APIRef}}</div>

<p><code>drag</code> イベントは、要素や選択されたテキストをユーザーがドラッグしている間に、数百ミリ秒ごとに発生します。</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">バブリング</th>
   <td>あり</td>
  </tr>
  <tr>
   <th scope="row">キャンセル可能</th>
   <td>はい</td>
  </tr>
  <tr>
   <th scope="row">既定のアクション</th>
   <td>ドラッグ&ドロップ操作を続ける</td>
  </tr>
  <tr>
   <th scope="row">インターフェイス</th>
   <td>{{domxref("DragEvent")}}</td>
  </tr>
  <tr>
   <th scope="row">イベントハンドラープロパティ</th>
   <td>{{domxref("GlobalEventHandlers/ondrag", "ondrag")}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Examples" name="Examples"></h2>

<p>このコードは <a href="http://jsfiddle.net/zfnj5rv4/">JSFiddle デモ</a>または以下で直接ご覧ください。</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;div class="dropzone"&gt;
  &lt;div id="draggable" draggable="true" ondragstart="event.dataTransfer.setData('text/plain',null)"&gt;
    This div is draggable
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class="dropzone"&gt;&lt;/div&gt;
&lt;div class="dropzone"&gt;&lt;/div&gt;
&lt;div class="dropzone"&gt;&lt;/div&gt;
</pre>

<h3 id="CSS">CSS</h3>

<pre class="brush: css">#draggable {
  width: 200px;
  height: 20px;
  text-align: center;
  background: white;
}

.dropzone {
  width: 200px;
  height: 20px;
  background: blueviolet;
  margin-bottom: 10px;
  padding: 10px;
}
</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">var dragged;

/* events fired on the draggable target */
document.addEventListener("drag", function(event) {

}, false);

document.addEventListener("dragstart", function(event) {
  // store a ref. on the dragged elem
  dragged = event.target;
  // make it half transparent
  event.target.style.opacity = .5;
}, false);

document.addEventListener("dragend", function(event) {
  // reset the transparency
  event.target.style.opacity = "";
}, false);

/* events fired on the drop targets */
document.addEventListener("dragover", function(event) {
  // prevent default to allow drop
  event.preventDefault();
}, false);

document.addEventListener("dragenter", function(event) {
  // highlight potential drop target when the draggable element enters it
  if (event.target.className == "dropzone") {
    event.target.style.background = "purple";
  }

}, false);

document.addEventListener("dragleave", function(event) {
  // reset background of potential drop target when the draggable element leaves it
  if (event.target.className == "dropzone") {
    event.target.style.background = "";
  }

}, false);

document.addEventListener("drop", function(event) {
  // prevent default action (open as link for some elements)
  event.preventDefault();
  // move dragged elem to the selected drop target
  if (event.target.className == "dropzone") {
    event.target.style.background = "";
    dragged.parentNode.removeChild( dragged );
    event.target.appendChild( dragged );
  }
}, false);</pre>

<p>{{EmbedLiveSample('Examples', '300', '200', '')}}</p>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
   <th scope="col">状態</th>
   <th scope="col">備考</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("HTML WHATWG", "interaction.html#dndevents", "drag event")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの対応</h2>

<p>{{Compat("api.Document.drag_event")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>その他のドラッグ&ドロップイベント:
  <ul>
   <li>{{domxref("Document/dragstart_event", "dragstart")}}</li>
   <li>{{domxref("Document/dragend_event", "dragend")}}</li>
   <li>{{domxref("Document/dragover_event", "dragover")}}</li>
   <li>{{domxref("Document/dragenter_event", "dragenter")}}</li>
   <li>{{domxref("Document/dragleave_event", "dragleave")}}</li>
   <li>{{domxref("Document/dragexit_event", "dragexit")}}</li>
   <li>{{domxref("Document/drop_event", "drop")}}</li>
  </ul>
 </li>
 <li>他のターゲットにおけるこのイベント:
  <ul>
   <li>{{domxref("Window")}}: {{domxref("Window/drag_event", "drag")}} イベント</li>
   <li>{{domxref("HTMLElement")}}: {{domxref("HTMLElement/drag_event", "drag")}} イベント</li>
   <li>{{domxref("SVGElement")}}: {{domxref("SVGElement/drag_event", "drag")}} イベント</li>
  </ul>
 </li>
</ul>