拖拽功能

<!DOCTYPE HTML>

<html>

<head>

<style type="text/css">

#div1 {width:190px;height:210px;padding:10px;border:1px solid #aaaaaa;}

</style>

<script type="text/javascript">

function allowDrop(ev)

{

ev.preventDefault();//阻止控件的默认行为

}

 

function drag(ev)

{

//alert(ev.target.id);

ev.dataTransfer.setData("",ev.target.id);

}

 

function drop(ev)

{

ev.preventDefault();

var data=ev.dataTransfer.getData("");

ev.target.appendChild(document.getElementById(data));

}

</script>

</head>

<body>

 

<div id="div1" οndrοp="drop(event)"

οndragοver="allowDrop(event)"></div>

<img id="drag1" src="5364690867118.gif" draggable="true"

οndragstart="drag(event)" width="184" height="206" />

<a href="http://www.w3schools.com" draggable="true" οndragstart="drag(event)" id="aaa">Visit W3Schools.com! </a>

</body>

</html>

 

一、概述

 

       Drag and drop is a very common feature. It is when you "grab" an object and drag it to a different location.n HTML5, drag and drop is part of the standard, and any element can be draggable.

Make an Element Draggable

 

二、实现步骤

 

(1)

First of all: To make an element draggable, set the draggable attribute to true:

 

<img draggable="true" />

 

(2)

What to Drag - ondragstart and setData()

 

Then, specify what should happen when the element is dragged.

 

In the example above, the ondragstart attribute calls a function, drag(event), that specifies what data to be dragged.

 

The dataTransfer.setData() method sets the data type and the value of the dragged data:

 

function drag(ev)

{

ev.dataTransfer.setData("Text",ev.target.id);//把被拖到的组件放到Text中,在drop的时候再放到相应的组件中

}

In this case, the data type is "Text" and the value is the id of the draggable element ("drag1").

 

(3)

Where to Drop - ondragover

 

The ondragover event specifies where the dragged data can be dropped.

 

By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element.

 

This is done by calling the event.preventDefault() method for the ondragover event:

 

event.preventDefault()

 

Do the Drop - ondrop

 

When the dragged data is dropped, a drop event occurs.

 

In the example above, the ondrop attribute calls a function, drop(event):

 

function drop(ev)

{

ev.preventDefault();

var data=ev.dataTransfer.getData("Text");

ev.target.appendChild(document.getElementById(data));

}

Code explained:

 

Call preventDefault() to prevent the browser default handling of the data (default is open as link on drop)

Get the dragged data with the dataTransfer.getData("Text") method. This method will return any data that was set to the same type in the setData() method

The dragged data is the id of the dragged element ("drag1")

Append the dragged element into the drop element

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值