温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

HTML5拖放功能怎么使用

发布时间:2022-03-07 17:10:56 来源:亿速云 阅读:146 作者:iii 栏目:web开发

本篇内容主要讲解“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拖放功能怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI