Created
November 12, 2015 16:40
-
-
Save mykwillis/81d509f5ab7f4618a693 to your computer and use it in GitHub Desktop.
HTML drag and drop with Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script type="text/javascript" a | |
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script> | |
<style type="text/css"> | |
.dropTarget { | |
width: 300px; | |
height: 100px; | |
border: 1px solid green; | |
text-align: center; | |
} | |
.dropatwill { | |
background-color: green; | |
} | |
</style> | |
</head> | |
<body ng-app="app"> | |
<div class="dropTarget" file-drop> | |
<p>Drop file here</p> | |
</div> | |
</body> | |
</html> | |
<script type="text/javascript"> | |
angular.module('app', []) | |
.directive('fileDrop', function () { | |
return { | |
restrict: 'A', | |
link: function (scope, element, attr) { | |
element.on('dragenter', function () { | |
this.classList.add('dropatwill'); | |
}); | |
element.on('dragleave', function () { | |
this.classList.remove('dropatwill'); | |
}); | |
element.on('dragover', function (event) { | |
event && event.preventDefault(); | |
event.dataTransfer.effectAllowed = 'copy'; | |
return false; | |
}); | |
element.on('drop', function (event) { | |
event && event.preventDefault(); | |
this.classList.remove('dropatwill'); | |
return false; | |
}); | |
} | |
}; | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment