欢迎光临
我们一直在努力

HTML5拖放功能怎么使用

本篇内容主要讲解“HTML5拖放功能怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“HTML5拖放功能怎么使用”吧!

拖放元素

HTML5拖放功能允许用户将元素拖放到另一个位置。放置位置可能是其他应用程序。拖动元素时,鼠标指针跟随该元素的半透明表示。

让我们尝试以下示例以了解其基本工作原理:

例试试这个代码»

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Using Drag and Drop</title>

<script>

    function dragStart(e) {

        // Sets the operation allowed for a drag source

        e.dataTransfer.effectAllowed = "move";

        // Sets the value and type of the dragged data

        e.dataTransfer.setData("Text", e.target.getAttribute("id"));

    }

    function dragOver(e) {

        // Prevent the browser default handling of the data

        e.preventDefault();

        e.stopPropagation();

    }

    function drop(e) {

        // Cancel this event for everyone else

        e.stopPropagation();

        e.preventDefault();

        // Retrieve the dragged data by type

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

        // Append image to the drop box

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

    }

</script>

<style>

    #dropBox {

        width: 300px;

        height: 300px;

        border: 5px dashed gray;

        background: lightyellow;

        text-align: center;

        margin: 20px 0;

        color: orange;

    }

    #dropBox img {

        margin: 25px;

    }

</style>

</head>

<body>

    <h3>Drag and Drop Demo</h3>

    <p>Drag and drop the image into the drop box:</p>

    <div id="dropBox" ondragover="dragOver(event);" ondrop="drop(event);">

        <!–Dropped image will be inserted here–>

    </div>

    <img src="../images/kites.jpg" id="dragA" draggable="true" ondragstart="dragStart(event);" width="250" height="250" alt="Flying Kites">

</body>

</html>

到此,相信大家对“HTML5拖放功能怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是云搜网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。