[练习]JS鼠标拖拽(DnD)操作

news/2024/7/1 4:47:00

拖放(Drag and Drop,DnD)操作因为涉及到与底层OS的结合,所以是较为复杂的交互操作。 这里先实现一个简单的拖拽图片到浏览器显示到操作, 主要用到了HTML5中的FileAPI:

先上DEMO
DnD demo

需要注意的是浏览器通过取消相应的拖拽事件来表明它对该事件有兴趣, 比如通过取消dragover来表明浏览器已经做好准备接受进一步的拖拽,接着说dragend最后到dropdrop事件发生时,就可以使用HTML5新的文件API也就是FileAPI进行数据交互,具体见本栗代码?
代码:

HTML

 <div id="holder"></div> 
<p id="status">File API可用</p>
<p>鼠标拖拽图片放入框中</p>

CSS

#holder { border: 10px dashed #ccc; width: 300px; height: 300px; margin: 20px auto;}
#holder.hover { border: 10px dashed #333; }
p {text-align:center;}

JS

var holder = document.getElementById('holder'),
    state = document.getElementById('status');
if (typeof window.FileReader === 'undefined') {
  state.className = 'fail';
} else {
 state.className = 'success';
  state.innerHTML = 'File API可用';
} 
 
holder.ondragover = function () {
 
 this.className = 'hover';
 console.log('dragover'); return false;
};
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
  //console.log(e)
  this.className = '';
  e.preventDefault();

  var file = e.dataTransfer.files[0],
      reader = new FileReader();
  reader.onload = function (event) {
    console.log(event.target);
    holder.style.background = 'url(' + event.target.result + ') no-repeat center';
  };
  console.log(file);
  reader.readAsDataURL(file);

  return false;
};

http://www.niftyadmin.cn/n/1207624.html

相关文章

JAVA操作properties文件

JAVA操作properties文件 java中的properties文件是一种配置文件。主要用于表达配置信息&#xff0c;文件类型为*.properties&#xff0c;格式为文本文件。文件的内容是格式是"键值"的格式&#xff0c;在properties 文件里&#xff0c;能够用"#"来作凝视&am…

Leetcode 算法面试冲刺 热题 HOT 100 刷题(75 76 78 79 84)(五十八)

文章目录75. 颜色分类76. 最小覆盖子串78. 子集79. 单词搜索84. 柱状图中最大的矩形75 78 7975. 颜色分类 就用了一个排序算法。 class Solution:def sortColors(self, nums: List[int]) -> None:"""Do not return anything, modify nums in-place instead.…

MyBatis学习总结(5)——实现关联表查询

一、一对一关联 1.1、提出需求 根据班级id查询班级信息(带老师的信息) 1.2、创建表和数据 创建一张教师表和班级表&#xff0c;这里我们假设一个老师只负责教一个班&#xff0c;那么老师和班级之间的关系就是一种一对一的关系。 1 CREATE TABLE teacher(2 t_id INT PRIMARY…

Leetcode 算法面试冲刺 热题 HOT 100 刷题(85 94 96 98 101)(五十九)

文章目录85. 最大矩形94. 二叉树的中序遍历96. 不同的二叉搜索树98. 验证二叉搜索树101. 对称二叉树85. 最大矩形 94. 二叉树的中序遍历 简单题 def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:if not root: return []res []def dfs(root, res):if n…

ifcfg、ip、ss,配置文件

ifcfg、ip、ss&#xff0c;配置文件一、ifcfg1、ifconfig命令&#xff1a;命令使用格式&#xff1a;ifonfig [INTERFACE]#ifconfig -a :显示所有接口&#xff0c;包括inactive状态的接口ifconfig interface [aftype] options | address..#ifconfig IFACE IP/MASK [up|down]例&a…

Leetcode 算法面试冲刺 热题 HOT 100 刷题(102 104 105 114 121)(六十)

文章目录102. 二叉树的层序遍历104. 二叉树的最大深度105. 从前序与中序遍历序列构造二叉树114. 二叉树展开为链表121. 买卖股票的最佳时机102. 二叉树的层序遍历 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val0, leftNone, rightNone…

二维码显示要求

1&#xff1a;鼠标挪到二维码时能正常显示 2&#xff1a;显示的速度 3&#xff1a;抽象出一个方法转载于:https://www.cnblogs.com/lxq0924/p/5082619.html

Leetcode 算法面试冲刺 热题 HOT 100 刷题(124 128 136 139 141)(六十一)

文章目录124. 二叉树中的最大路径和128. 最长连续序列136. 只出现一次的数字139. 单词拆分141. 环形链表124. 二叉树中的最大路径和 困难题&#xff0c;我先跳过。 128. 最长连续序列 不会。 class Solution {public int longestConsecutive(int[] nums) {Set<Integer&…