但是一测试我们就发现虽然元素可以拖拉,但并没有事件激活,那是应为我们没有为元素绑定事件,所以现在我们用addEventListener来将元素和事件绑定
- $(
- function() {
- [ ].forEach.call(document.querySelectorAll("div"),
- function(item) {
- item.addEventListener("dragstart", handleDragStart, false);
- }
- );
- }
- );
第二步,当我们拖放完成后,所有div恢复原先颜色,那自然是编写handleDragEnd
- function handleDragEnd(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- [ ].forEach.call(document.querySelectorAll("div"),
- function(item) {
- item.style.opacity = "1";
- }
- );
- }
- $(
- function() {
- [ ].forEach.call(document.querySelectorAll("div"),
- function(item) {
- item.addEventListener("dragstart", handleDragStart, false);
- item.addEventListener("dragend", handleDragEnd, false);
- }
- );
- }
- );
我们先需要为目标元素定义些事件函数
- function handleDragEnter(e) {
- $.log(e);
- }
- function handleDragOver(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- if (e.stopPropagation) {
- e.stopPropagation(); //停止事件的传播
- }
- $.log(e);
- return false;
- }
- function handleDragLeave(e) {
- $.log(e);
- }
- function handleDrop(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- if (e.stopPropagation) {
- e.stopPropagation(); //停止事件的传播
- }
- console.log(e);
- return false;
- }
注意我们使用了preventDefault和stopPropagation消除了浏览器默认的一些动作。
显然这些事件不是所有的元素都有的,只有互为倍数才有,所以我们要去修改handleDragStart函数了
修改后的代码大致如下
- function handleDragStart(e) {
- this.style.opacity = "0.5";
- e.dataTransfer.effectAllowed = "move";
- e.dataTransfer.setData("text/plain", this.innerHTML);
- //$.log(this.innerHTML);
- //$.log(e.target.innerHTML);
- //$.log(e.srcElement.innerHTML);
- //$.log(this.innerHTML);
- [ ].forEach.call(document.querySelectorAll("div"),
- function(item) {
- var a = parseInt(e.target.innerHTML);
- var b = parseInt(item.innerHTML);
- if (a % b != 0 && b % a != 0) {
- item.style.opacity = "0.1";
- }
- else {
- item.addEventListener("dragover", handleDragOver, false);
- item.addEventListener("dragenter", handleDragEnter, false);
- item.addEventListener("dragleave", handleDragLeave, false);
- item.addEventListener("drop", handleDrop, false);
- }
- });
- }
- function handleDragEnd(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- [ ].forEach.call(document.querySelectorAll("div"),
- function(item) {
- item.style.opacity = "1";
- item.removeEventListener("dragover", handleDragOver, false);
- item.removeEventListener("dragenter", handleDragEnter, false);
- item.removeEventListener("dragleave", handleDragLeave, false);
- item.removeEventListener("drop", handleDrop, false);
- }
- );
- }
- function handleDrop(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- if (e.stopPropagation) {
- e.stopPropagation(); //停止事件的传播
- }
- this.innerHTML = parseInt(this.innerHTML)+parseInt(e.dataTransfer.getData("text/plain"))
- console.log(e);
- return false;
- }
最后,我感觉黑色的字不好看,我们修改下初始化的函数
- $(
- function() {
- [ ].forEach.call(document.querySelectorAll("div"),
- function(item) {
- item.addEventListener("dragstart", handleDragStart, false);
- item.addEventListener("dragend", handleDragEnd, false);
- item.style.color = "White";
- }
- );
- }
- );
