当前位置: 首页 > news >正文

案例 - 拖拽上传文件,生成缩略图

直接看效果

实现代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>拖拽上传文件</title><style>/* 拖拽容器 */.box-container {position: relative;display: flex;justify-content: center;align-items: center;border: 1px dashed black;border-radius: 7px;width: 200px;height: 100px;}.box1 {position: absolute;width: 10px;height: 50px;background: black;}.box2 {position: absolute;width: 50px;height: 10px;background: black;}.dropbox {position: absolute;opacity: 0;width: 100%;height: 100%;line-height: 100px;text-align: center;font-size: 20px;font-weight: 900;background: rgb(0, 0, 0, 0.3);}/* 图片容器 */.img-container {width: 500px;height: 500px;border: 1px solid black;border-radius: 10px;box-shadow: 2px 2px 20px 3px black;overflow: auto;}.img-item {float: left;width: 50px;height: 50px;margin: 5px;border: 1px solid black;border-radius: 10px;}</style>
</head><body><div class="box-container"><div class="box1"></div><div class="box2"></div><div class="dropbox">拖拽上传文件</div></div><br><hr><br><div class="img-container"></div></body>
<script>let dropbox = document.querySelector('.dropbox')dropbox.addEventListener("dragenter", dragenter, false)dropbox.addEventListener("dragover", dragover, false)dropbox.addEventListener("drop", drop, false)dropbox.addEventListener('dragleave', dragleave, false)function dragenter(e) {e.stopPropagation();e.preventDefault();}// 进入拖拽容器function dragover(e) {e.stopPropagation();e.preventDefault();dropbox.style.cssText = `opacity: 1;`}// 离开拖拽容器function dragleave(e) {e.stopPropagation();e.preventDefault();dropbox.style.cssText = `opacity: 0;`}// 将拖拽标签放在拖拽容器上(鼠标松开)function drop(e) {e.stopPropagation();e.preventDefault();dropbox.style.cssText = `opacity: 0;`const dt = e.dataTransfer;const files = dt.files;console.log(dt.files);handleFiles(files);}let imgContainer = document.querySelector('.img-container')/** * handleFiles() 处理文件列表* * @param files 文件列表* @return */function handleFiles(files) {for (const file of files) {if (!file.type.startsWith("image")) {continue}const img = document.createElement('img')img.classList.add('img-item')// 读取文件流const reader = new FileReader()reader.onload = (e) => {img.src = e.target.resultimgContainer.appendChild(img)}reader.readAsDataURL(file)}}</script></html>

http://www.lryc.cn/news/226040.html

相关文章:

  • PHP 使用递归方式 将其二维数组整合为层级树 其中层级id 为一个uuid的格式 造成的诡异问题 已解决
  • rv1126-rv1109-添加分区,定制固件,开机挂载功能
  • 一台电脑使用多个gitee账号,以及提交忽略部分文件
  • 解析邮件文本内容; Mime文本解析; MimeStreamParser; multipart解析
  • 获取请求IP以及IP解析成省份
  • YOLOv8-seg改进:复现HIC-YOLOv5,HIC-YOLOv8-seg助力小目标分割
  • vscode 终端进程启动失败: shell 可执行文件“C:\Windows\System32\WindowsPower
  • 【中间件篇-Redis缓存数据库02】Redis高级特性和应用(慢查询、Pipeline、事务、Lua)
  • 栈 和 队列
  • 【推荐】一款AI写作大师、问答、绘画工具-「智元兔 AI」
  • 阿里云付费用户破100万 用户规模亚洲最大
  • 人工智能基础——Python:Matplotlib与绘图设计
  • Ubuntu 配置 Github 的 SSH keys
  • Flink—— Flink Data transformation(转换)
  • 前端读取文件当文件选择相同文件名的文件,内容不会变化
  • PHP 服装销售管理系统mysql数据库web结构layUI布局apache计算机软件工程网页wamp
  • 用于图像处理的高斯滤波器 (LoG) 拉普拉斯
  • 【h5 uniapp】 滚动 滚动条,数据跟着变化
  • ModStartBlog v8.5.0 评论开关布局调整,系统后台全面优化
  • django|报错SQLite 3.8.3 or later is required的解决方案
  • 通达OA get_datas.php前台sql注入-可获取数据库session登入后台漏洞复现 [附POC]
  • 苹果官方:所有国行iPhone 15系列都在中国生产!
  • Oracle 安装及 Spring 使用 Oracle
  • element-ui 表格 点击选中
  • 畅通工程之局部最小花费问题 (C++)
  • Sql 异常 + Error
  • 基于UNI-APP实现适配器并保证适配器和实现的调用一致
  • 使用jdk21预览版 --enable-preview
  • js中的跳转都有哪些格式
  • 无重复字符的最长子串