首先什么是html5拖放?
答:html5拖放是h5標準的組成部分
拖動開始:
ondrapstart:調(diào)用一個函數(shù),drag,規(guī)定了被拖動的數(shù)據(jù)
設置拖動數(shù)據(jù):
setData():設置被拖數(shù)據(jù)的數(shù)據(jù)類型和值
放入位置
ondropover:時間規(guī)定在何處放置被拖動的數(shù)據(jù)
放置:
ondrop:當放置被拖數(shù)據(jù)時,會發(fā)生drop事件
demo開始:
首先對要放入的框框定義ondragover的方法,并且去除系統(tǒng)默認設定,代碼如下:
window.onload=function(){
box1Div=document.getElementById('box1');
box1Div.ondragover=function(e){
e.preventDefault();
}}
對于被拖放的圖片進行設置其數(shù)據(jù)類型和值,代碼如下
img1.ondragstart=function(e){
e.dataTransfer.setData("imgId","img1");
}
要放入的框框定義被拖數(shù)據(jù)時的drop事件
box1Div.ondrop=function(e){
e.preventDefault();
var img=document.getElementById(e.dataTransfer.getData("imgId"));
e.target.appendChild(img);
}
以上可以實現(xiàn)圖片拖動放入框內(nèi)的功能,整段功能如下:
