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

直接看效果

实现代码

html 复制代码
<!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.result
                imgContainer.appendChild(img)
            }
            reader.readAsDataURL(file)
        }
    }

</script>

</html>
相关推荐
Bigger13 小时前
Coco AI 技术演进:Shadcn UI + Tailwind CSS v4.0 深度迁移指南 (踩坑实录)
前端·css·weui
bug总结14 小时前
前端开发中为什么要使用 URL().origin 提取接口根地址
开发语言·前端·javascript·vue.js·html
八哥程序员16 小时前
从DOM结构到布局流:display: content的深度解析与实战应用
前端·css
前端达人20 小时前
CSS终于不再是痛点:2026年这7个特性让你删掉一半JavaScript
开发语言·前端·javascript·css·ecmascript
dreams_dream21 小时前
vue2动态更改css属性方法大全
前端·css
洞窝技术21 小时前
从原理到落地:大屏适配适配 + 高并发弹幕的企业级技术手册
前端·css
精神病不行计算机不上班21 小时前
[Java Web]在IDEA中完整实现Servlet的示例
java·servlet·tomcat·html·intellij-idea·web
顾安r1 天前
12.17 脚本工具 自动化全局跳转
linux·前端·css·golang·html
苏打水com1 天前
第十七篇:Day49-51 前端工程化进阶——从“手动”到“自动化”(对标职场“提效降本”需求)
前端·javascript·css·vue.js·html
码途潇潇1 天前
数据大屏常用布局-等比缩放布局(Scale Laylout)-使用 CSS Transform Scale 实现等比缩放
前端·css