当前位置: 首页 > 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/222530.html

相关文章:

  • github 上传代码报错 fatal: Authentication failed for ‘xxxxxx‘
  • Linux虚拟网络设备之bridge
  • 最后一个大更新!Win11 2023正式发布:Copilot终于来了
  • pandas教程:Reading and Writing Data in Text Format (以文本格式读取和写入数据)
  • 软考高级系统架构设计师系列之:软考高级系统架构设计师论文专题
  • 目标检测中的评价指标
  • 【AI编程】ai编程插件汇总iFlyCode、codegeex
  • 算法通关村第八关|黄金挑战|二叉树的最近公共祖先
  • 亚马逊云科技产品测评』活动征文|通过使用Amazon Neptune来预测电影类型初体验
  • 【获奖论文】2023年数学建模国赛优秀获奖论文
  • 美团三年,总结的10条血泪教训
  • 【CSP认证考试】202309-1:坐标变换(其一)100分解题思路+代码
  • 剩余参数和展开运算符的区别
  • ES6的基础用法
  • standard_init_linux.go:211: exec user process caused “exec format error“
  • python的format函数的用法及实例
  • BigDecimal 类型的累加操作
  • 基于单片机的土壤温湿度控制系统
  • 服务器数据库中了elbie勒索病毒怎么办,elbie勒索病毒解密,数据恢复
  • 接口测试及接口测试工具
  • JUC包工具类介绍二
  • 第8章_聚合函数
  • Mysql8与mariadb的安装与常用设置
  • 深入剖析Golang中单例模式
  • Java之SpringCloud Alibaba【八】【Spring Cloud微服务Gateway整合sentinel限流】
  • 深入解析 Redis 分布式锁原理
  • [unity]多脚本情况下update函数的执行顺序
  • Maven中<scope>中等级的区别
  • 小明和完美序列(HashMap、Map、Entry)
  • 【hexo博客配置】hexo icarus主题配置