javascript - Drag and Drop HTML5 not working? -
i trying understand drag , drop html5, can drag , have element suppose take dragged element accept it. cant ondrag or ondragenter, or @ least functions events call work i'm not sure i'm doing wrong.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>week 3 drag , drop</title> <link rel="stylesheet" type="text/css" href="week3draganddropcss.css"> <script src="week3draganddropjs.js"></script> </head> <body> <header> <h1>week 3 drag , drop</h1> </header> <div style="height:200px;width:200px;background: red" id="obj1"draggable="true"></div> <div style="height:200px;width:200px;background: green"id="obj2"draggable="true"></div> <div style="height:200px;width:200px;background: orange" id="obj3"draggable="true"></div> <div style="height:200px;width:200px;background-image: url ('trash.png');background-repeat: no-repeat;" id="trashcan" ondragover="allowdrop(event)" ondragenter="setborder(event,'5px')" ondragleave="setborder(event,'2px')" ondrop="drop(event)"></div> <footer> </footer> </body> </html>
then here js:
function drag(evt){ evt.datatransfer.setdata("color", ev.target.background); } function allowdrop(evt){ evt.preventdefault(); } function drop(evt){ evt.preventdefault(); var color = evt.datatransfer.getdata("color"); evt.currenttarget.style.background = color; } function setborder(evt,size){ evt.currenttarget.border = (size +"solid black"); }
minor syntax mistakes reffer following fiddle
function drag(evt){ evt.datatransfer.setdata("color", evt.target.style.backgroundcolor); } function allowdrop(evt){ evt.preventdefault(); } function drop(evt){ evt.preventdefault(); var color = evt.datatransfer.getdata("color"); evt.currenttarget.style.background = color; } function setborder(evt,size){ evt.currenttarget.style.border = (size +" solid black"); }
<body> <header> <h1>week 3 drag , drop</h1> </header> <div style="height:200px;width:200px;background: red" id="obj1"draggable="true" ondragstart="drag(event)"></div> <div style="height:200px;width:200px;background: green"id="obj2"draggable="true" ondragstart="drag(event)"></div> <div style="height:200px;width:200px;background: orange" id="obj3"draggable="true" ondragstart="drag(event)"></div> <div style="height:200px;width:200px;background-image: url ('trash.png');background-repeat: no-repeat;" id="trashcan" ondragover="allowdrop(event)" ondragenter="setborder(event,'5px')" ondragleave="setborder(event,'2px')" ondrop="drop(event)"></div> <footer> </footer> </body>
Comments
Post a Comment