Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- </head>
- <body>
- <img src="close.png" ondrop="handleDrop(event)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event)"/>
- <script>
- 'use strict';
- (function () {
- var trashCanHeight = 450;
- var trashCount = 10;
- var documentFragment = document.createDocumentFragment();
- for (var i = 0; i < trashCount; i++) {
- var trash = document.createElement('img');
- trash.style.position = 'absolute';
- trash.style.top = getRandomPosition(trashCanHeight, screen.height - 250) + 'px';
- trash.style.left = getRandomPosition(0, screen.width - 250) + 'px';
- trash.setAttribute('id', i);
- trash.setAttribute('src', 'trash.png');
- trash.setAttribute('draggable', 'true');
- trash.setAttribute('ondragstart', 'handleDragStart(event)');
- trash.setAttribute('ondragend', 'handleDragEnd(event)');
- documentFragment.appendChild(trash);
- }
- document.body.appendChild(documentFragment);
- function getRandomPosition(start, end) {
- var random = Math.floor((Math.random() * (end - start) + start));
- return random;
- }
- }());
- function handleDragStart(e) {
- e.dataTransfer.setData("dragged-id", e.target.id);
- e.target.style.opacity = '0.5';
- }
- function handleDragOver(e) {
- e.preventDefault();
- e.target.setAttribute('src', 'open.png');
- }
- function handleDragLeave(e) {
- e.target.setAttribute('src', 'close.png');
- }
- function handleDragEnd(e) {
- e.target.style.opacity = '1';
- }
- function handleDrop(e) {
- e.preventDefault();
- var trashId = e.dataTransfer.getData("dragged-id");
- var trashNode = document.getElementById(trashId);
- trashNode.parentNode.removeChild(trashNode);
- e.target.setAttribute('src', 'close.png');
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment