简体   繁体   中英

Can you trigger some type of event with drag and drop boxes?

I'm trying to develop a small site where user can drag and drop pictures. So when user drags a particular picture to the box it would return some kind of a result in the form of text like details about that picture...

This is the current code that I'm using to accomplish drag and drop, but I can't figure out how to add functionality that I described above:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset ="utf-8">
    <style type="text/css">
        #dragBox {width:336px;height:221px;padding:10px;border:1px solid #aaaaaa;}
    </style>
    <script>
    function allowDrop(ev)
    {
        ev.preventDefault();
    }

    function drag(ev)
    {
        ev.dataTransfer.setData("Text",ev.target.id);
    }

    function drop(ev)
    {
        ev.preventDefault();
        var data=ev.dataTransfer.getData("Text");
        ev.target.appendChild(document.getElementById(data));
    }
    </script>
</head>
<body bgcolor="#333333">
<div div align="center">
    <img id="drag1" src="foodOne.jpg" draggable="true" ondragstart="drag(event)" width="336" height="221">
    <div id="dragBox" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
</body>
</html>

A drop event can have a files property. You can read the files with JavaScript. The common approach is to send it to the server which can then send the information back, like a path to a thumbnail.

There are a lot of scripts readily available already, just Google/Bing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM