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

当前题目:HTML5拖放功能怎么使用
标题来源:https://www.cdcxhl.com/article30/gseopo.html

成都网站建设公司_创新互联,为您提供网站维护网站导航服务器托管网页设计公司App设计静态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设