手机及PC端web多图片上传及回显

适用于电脑端及手机端

示例图:

在这里插入图片描述

目录结构

在这里插入图片描述

css样式

直接保存为.css文件或copy至html头部

.suncsf-upload-context {
    width: 90%;
    margin: 1% 5%;
}
.suncsf-upload-image {
    width: 22%;
    padding: 1%;
    float: left;
}
.suncsf-upload-image input {
    display: none;
}
.suncsf-upload-image img {
    width: 100%;
}
js代码

直接保存为.js文件或copy至html底部

(function ($) {
    let ptname = 'suncsf-upload-context';
    let endid = 'suncsf-upload-end';
    let accept = 'image/*';
    let name = '';
    $.extend({
        initSuncUploadImg: function (_name, _endid,m) {
            if (_endid) {
                endid = _endid;
            }
            if(m){
                accept = m;
            }
            if(_name){
                name = _name;
            }
            $.createuploadimg();
        },
        createuploadimg: function () {
            //--------------------
            //创建单个加载上传图片容器
            let content = document.createElement('div');
            content.className = 'suncsf-upload-image add';
            //调用 input file的click即选择事件
            content.onclick = function () {
                if ($(this).hasClass('add')) {
                    let fileinput = $(this).children('input[type=file]')[0];
                    fileinput.click();
                    return;
                }
                if (confirm('是否移除?')) {
                    $(this).remove();
                }
            };
            //--------------------
            //创建图片加载
            let uplaodimg = document.createElement('img');
            uplaodimg.src = './img/添加.png';
            //加入容器
            content.appendChild(uplaodimg);

            //--------------------
            //隐藏的input file 
            let fileinput = document.createElement('input');
            fileinput.name = name+'temp';
            fileinput.type = 'file';
            fileinput.accept = accept;
            //设置change事件 回显图片
            fileinput.onchange = function () {
                // let _this = this;
                let files = this.files;
                var count = files.length;
                // alert(count);
                if (count > 0) {
                    let file = files[0];
                    var img = $(this).prev('img');
                    if (img) {
                        var fr = new FileReader();
                        fr.onload = function () {
                            img.attr('src', this.result);

                        }
                        fr.readAsDataURL(file);
                        $(this).parent().removeClass('add');
                        $(this).prop('name',name);
                        $.createuploadimg();
                    }
                }
            };
            //加入容器
            content.appendChild(fileinput);
            //--------------------加入到主容器内清除浮动div元素之前
            document.getElementsByClassName(ptname)[0].insertBefore(content, document.getElementById(endid));
            // $('#suncsf-upload-end').before('<div class="suncsf-upload-image" οnclick="uploadimgclick(this);"><img src="./img/添加.png" /><input type="file" name="file" οnchange="uploadimgload(this);" /></div>');
        }
    })
})(jQuery);
html代码

直接保存为.html文件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,
            minimum-scale=1.0,maximum-scale=1.0, user-scalable=no" />
    <title>Document</title>
    <link href="./css/suncsf.upload.image.css" rel="stylesheet">
</head>

<body>
    <div class="suncsf-upload-context">
        <div id="suncsf-upload-end" style="clear: both;"></div>
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="./js/suncsf.upload.image.js"></script>
    <script>
        $(function () {
            $.initSuncUploadImg('file');
        })
    </script>
</body>

</html>

  1. 上传前input[type=file]name=指定的name+temp,上传后会变为指定的name
  2. 上传前img父级样式为"suncsf-upload-image add",上传后删除"add"样式,样式"add"作为上传的识别按钮功能。

链接:https://pan.baidu.com/s/1W5da6gKuQIAfVmvLH9kMIw
提取码:kp9d

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值