首页 > 代码库 > 多次上传图片
多次上传图片
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title></title>
<script type="text/javascript" src="http://www.mamicode.com/__PUBLIC__/sima/server/js/rem.js" ></script>
<script type="text/javascript" src="http://www.mamicode.com/__PUBLIC__/sima/js/jq/jquery-3.1.1.js"></script>
<link rel="stylesheet" href="http://www.mamicode.com/__PUBLIC__/sima/server/css/reset.css" />
<link rel="stylesheet" href="http://www.mamicode.com/__PUBLIC__/sima/server/css/publick.css" />
<link rel="stylesheet" href="http://www.mamicode.com/__PUBLIC__/sima/server/iconfont4/iconfont.css" />
<style>
body{
width:100%;
overflow:hidden;
background-color:#f4f4f4;
}
.container{
width:92%;
padding:0 4%;
background:white;
}
.con{
width:100%;
min-height:4rem;
padding-top:1rem;
line-height:1rem;
font-size:0.65rem;
color:black;
outline: none;
}
.con:empty:before{
content:attr(placeholder);
font-size: 14px;
color: #b0b0b0;
}
.con:focus{
content:none;
}
ul{
width:96%;
overflow:hidden;
display: flex;
flex-wrap: wrap;
margin-right:0.5rem;
margin-top:0.8rem;
}
ul li{
margin-right:1.3rem;
margin-bottom:0.8rem;
position:relative;
}
ul li:nth-child(3n){
margin-right:0rem;
}
ul li p{
width:3.4rem;
height:3.4rem;
color:#d6d6d6;
font-size:3rem;
border:0.15rem solid #d6d6d6;
text-align:center;
}
ul li input{
width:3.4rem;
height:3.4rem;
position:absolute;
left:0;
top:0;
opacity: 0;
}
.wz{
width:100%;
height:1.65rem;
display:flex;
align-items: center;
font-size:0.7rem;
border-top:1px solid #f4f4f4;
}
.wz p:nth-child(2){
flex:1;
text-align: right;
}
.wz .iconfont:nth-child(1){
font-size:0.9rem;
margin-right:0.3rem;
color:#909090;
}
.wz .iconfont:nth-child(2){
font-size:0.6rem;
color:#c2c2c2;
}
.tj{
width:22%;
height:1.5rem;
text-align:center;
line-height:1.5rem;
border-radius: 0.2rem;
font-size:0.8rem;
font-weight:bold;
color:white;
background-color:#fe773e;
position:absolute;
left:39%;
bottom:2rem;
}
ul .pic{
width: 100%;
display: flex;
}
.pic li{
margin:0px;
padding:1px;
}
.pic li, .pic li img{
width: 3.425rem;
height: 3.425rem;
}
.z_photo img {
width: 1rem;
height: 1rem;
}
/*遮罩层*/
.z_mask {
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .5);
position: fixed;
top: 0;
left: 0;
z-index: 999;
display: none;
}
.z_alert {
width: 8.5rem;
height: 5.5rem;
border-radius: .5rem;
background: #fff;
font-size: .6rem;
text-align: center;
position: absolute;
left: 50%;
top: 50%;
margin-left: -4.5rem;
margin-top: -2rem;
}
.z_alert p:nth-child(1) {
line-height: 3.9rem;
}
.z_alert p:nth-child(2) span {
display: inline-block;
width: 49%;
height: .5rem;
line-height: 1.3rem;
float: left;
border-top: 1px solid #ddd;
}
.z_cancel {
border-right: 1px solid #ddd;
}
textarea{
width:100%;
height:3rem;
border:0px solid #bebebe;
border-radius: 0.2rem;
line-height:1rem;
resize : none;
font-size:0.65rem;
color:black;
outline-color: #bebebe;
width:100%;
min-height:4rem;
padding-top:1rem;
line-height:1rem;
font-size:0.65rem;
color:black;
outline: none;
}
</style>
</head>
<body>
<div class="container">
<form action="__MODULE__/user/cust/upload" enctype="multipart/form-data" method="post" >
<textarea name="content" placeholder="这一刻的想法!"></textarea>
<ul class=‘pic z_photo‘ id=‘dd‘>
</ul>
<ul class="upic">
<li >
<p>+</p>
<input type="file" num=‘0‘ name="file1" accept="image/*" class="imgChange" />
</li>
</ul>
</form>
<!--遮罩层-->
<div class="z_mask">
<!--弹出框-->
<div class="z_alert">
<p>确定要删除这张图片吗?</p>
<p>
<span class="z_cancel">取消</span>
<span class="z_sure">确定</span>
</p>
</div>
</div>
<div class="wz">
<p>
<span class="iconfont"></span>
所在位置
</p>
<p class="iconfont"></p>
</div>
</div>
<div class="tj">发表</div>
</body>
</html>
<script type="text/javascript">
$(function(){
$(‘.imgChange‘).change(function(){
imgChange(‘z_photo‘,‘z_file‘,this);
});
});
//px转换为rem
(function(doc, win) {
var docEl = doc.documentElement,
resizeEvt = ‘orientationchange‘ in window ? ‘orientationchange‘ : ‘resize‘,
recalc = function() {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if (clientWidth >= 640) {
// docEl.style.fontSize = ‘100px‘;
} else {
// docEl.style.fontSize = 100 * (clientWidth / 640) + ‘px‘;
}
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener(‘DOMContentLoaded‘, recalc, false);
})(document, window);
function imgChange(obj1, obj2,obj3) {
var num = 0;
//alert($(obj3).attr(‘name‘));
//获取点击的文本框
var file = document.getElementById("file");
var file = $(obj3)[0];
//存放图片的父级元素
var imgContainer = document.getElementsByClassName(obj1)[0];
//获取的图片文件
var fileList = file.files;
//文本框的父级元素
var input = document.getElementsByClassName(obj2)[0];
var imgArr = [];
//遍历获取到得图片文件
for (var i = 0; i < fileList.length; i++) {
num = $(‘input[type=file]‘).attr(‘num‘);
var imgUrl = window.URL.createObjectURL(file.files[i]);
imgArr.push(imgUrl);
var img = document.createElement("img");
img.setAttribute("src", imgArr[i]);
var imgAdd = document.createElement("li");
imgAdd.setAttribute("class", "z_addImg");
imgAdd.appendChild(img);
imgContainer.appendChild(imgAdd);
$(‘input[type=file]‘).attr(‘num‘,num*1+1);
};
//alert(1);
//点击之后删除p
var p = $(obj3).prev(‘p‘);
//克隆一份
var li = $(‘.upic li‘).eq(0).clone(true);
//移除
p.remove();
var newnum = $(‘input[type=file]‘).attr(‘num‘);
//alert(newnum);
//生成一个p
var str = ‘‘;
str += "<li>";
str += "<p>+</p>";
//str += ‘<input type="file" num=‘0‘ name="file2" accept="image/*" />‘;
str += ‘<input type="file" name="file5" accept="image/*" class="imgChange" />‘;
str += "</li>";
//alert(li);
//添加一个
$(‘.upic‘).prepend(li);
//改变name
$(‘.upic li‘).eq(0).find(‘input‘).attr(‘name‘,‘file‘+(newnum*1+1));
//alert();
// $(‘.section_my ul‘).prepend(str);
if(newnum == 9 || newnum >9 )
{
$(‘ul li p‘).css(‘display‘,‘none‘);
//clone bug
$(‘.upic li‘).first().remove();
//return false;
}
imgRemove();
};
function imgRemove() {
var imgList = document.getElementsByClassName("z_addImg");
var mask = document.getElementsByClassName("z_mask")[0];
var cancel = document.getElementsByClassName("z_cancel")[0];
var sure = document.getElementsByClassName("z_sure")[0];
for (var j = 0; j < imgList.length; j++) {
imgList[j].index = j;
imgList[j].onclick = function() {
var t = this;
mask.style.display = "block";
cancel.onclick = function() {
mask.style.display = "none";
};
sure.onclick = function() {
mask.style.display = "none";
t.style.display = "none";
$(‘ul li p‘).css(‘display‘,‘block‘);
};
}
};
};
</script>
<script type="text/javascript">
$(function(){
$(‘.con‘).blur(function(){
var text = $(this).html();
alert(text);
});
$(‘.tj‘).click(function(){
//console.log(‘onl‘);
var content = $(‘textarea‘).val();
//console.log(content);
var file = $(‘#file‘).val();
if(content == ‘‘ && file == ‘‘)
{
alert(‘请填写您要发表的内容或发表图片‘);
return false;
}
//clone bug
$(‘.upic li‘).first().remove();
$(‘form‘).submit();
});
});
</script>
多次上传图片