標籤:internet 瀏覽器 explorer firefox 智能手機
本文為 兄弟連IT教育 機構官方 HTML5培訓 教程,主要介紹:HTML5移動開發之路(16)——神奇的拖放功能
在智能手機發展飛速的現在拖放功能已經成為一種時尚,但是在我們的瀏覽器上是不是還缺少這種方便快捷的功能?在HTML5的新標準中就有關於拖放的標準,作為HTML5標準的一部分,任何元素都可以被拖放。
一、瀏覽器支援情況
Internet Explorer 9、Firefox、Opera 12、Chrome 以及 Safari 5 支援拖放。
注釋:在 Safari 5.1.2 中不支援拖放。
二、使用方法
首先,為了使元素可拖動,把 draggable 屬性設定為 true :
[html] view plain copy
print?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代碼片" width="12" height="12" style="border:0px;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到My Code片" width="12" height="12" style="border:0px;" />
<img draggable="true" />
然後,規定元素被拖動時,會發生什麼
讓ondragstart屬性調用一個函數,並給該函數返回一個拖動對象
[html] view plain copy
print?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代碼片" width="12" height="12" style="border:0px;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到My Code片" width="12" height="12" style="border:0px;" />
<img id="drag1" src="img_logo.gif" draggable="true"
ondragstart="drag(event)" width="336" height="69" />
dataTransfer.setData() 方法設定被拖資料的資料類型和值:(我們將被拖動對象的id傳給dataTransfer)
[html] view plain copy
print?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代碼片" width="12" height="12" style="border:0px;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到My Code片" width="12" height="12" style="border:0px;" />
function drag(ev)
{
<span style="white-space:pre"> </span>ev.dataTransfer.setData("Text",ev.target.id);
}
放到何處 - ondragover
ondragover 事件規定在何處放置被拖動的資料。
預設地,無法將資料/元素放置到其他元素中。如果需要設定允許放置,我們必須阻止對元素的預設處理方式。
這要通過調用 ondragover 事件的 event.preventDefault() 方法:
[html] view plain copy
print?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代碼片" width="12" height="12" style="border:0px;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到My Code片" width="12" height="12" style="border:0px;" />
event.preventDefault()
當放置被拖資料時,會發生 drop 事件。
在上面,ondrop 屬性調用了一個函數,drop(event):
[html] view plain copy
print?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代碼片" width="12" height="12" style="border:0px;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到My Code片" width="12" height="12" style="border:0px;" />
function drop(ev)
{
<span style="white-space:pre"> </span>ev.preventDefault();
<span style="white-space:pre"> </span>var data=ev.dataTransfer.getData("Text");
<span style="white-space:pre"> </span>ev.target.appendChild(document.getElementById(data));
}
代碼解釋:
二、執行個體說明
[html] view plain copy
print?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代碼片" width="12" height="12" style="border:0px;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到My Code片" width="12" height="12" style="border:0px;" />
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//阻止對元素的預設處理方式
function allowDrop(ev)
{
ev.preventDefault();
}
//將被拖動對象的id傳遞給dataTransfer中間對象
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
//取得拖動對象id找到對象執行個體並用DOM模型添加到<div>裡面
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<!--要被拖動到的地方-->
<div id="div1" ondrop="drop(event)"
ondragover="allowDrop(event)"
style="border:1px solid red;width:300px;height:200px;">
</div>
<!--要被拖動的對象-->
<img id="drag1" src="test.png" draggable="true"
ondragstart="drag(event)"/>
</body>
</html>
650) this.width=650;" src="http://img.my.csdn.net/uploads/201401/12/1389505406_5925.gif" style="border:0px;" />
HTML5移動開發之路(16)——神奇的拖放功能