您的位置:首页 > 其它

网页实现多图片上传,预览,放大等功能

2020-02-02 05:48 295 查看

功能需求:

  1.点击"+"按钮,选择一张图片(jpg、png、bmp、jpeg格式),添加到网页中实现预览,效果如图:

 

 

 

   2.点击图片,放大图片至屏幕中间,实现放大预览,再次点击回到原来的大小,效果如图:

实现:

   导入 lrz(图片压缩) 和 zoomify(图片放大)两个js工具 

链接:https://pan.baidu.com/s/1tfpjdorUh6bfkvxPuGF-HA
提取码:b5da

css:

body {
background-color: #f2f2f2;
}
.release_up_pic .tit {
padding: 12px;
font-size: 1.4rem;
color: #999;
}
.release_up_pic .tit h4 {
font-weight: normal;
}
.release_up_pic .tit h4 em {
font-size: 1.1rem;
}
.release_up_pic .up_pic {
background-color: #fff;
padding: 15px 12px;
font-size: 0;
margin-left: -3.33333%;
padding-bottom: 3px;
border-bottom: 1px solid #e7e7e7;
border-top: 1px solid #e7e7e7;
}
.release_up_pic .up_pic .pic_look {
width: 30%;
height: 80px;
display: inline-block;
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
box-sizing: border-box;
margin-left: 3.3333%;
margin-bottom: 12px;
position: relative;
}
.release_up_pic .up_pic .pic_look em {
position: absolute;
display: inline-block;
width: 25px;
height: 25px;
background-color: #ff0000;
color: #fff;
font-size: 18px;
right: 5px;
top: 5px;
text-align: center;
line-height: 22px;
border-radius: 50%;
font-weight: bold;
}

#imgList em{
position: absolute;
display: inline-block;
width: 15px;
height: 15px;
background-color: darkgray;
color: #fff;
right: 10px;
text-align: center;
line-height: 1;
border-radius: 50%;
font-style: normal;
cursor: pointer;

}

#chose_pic_btn {
width: 30%;
height: 80px;
position: relative;
display: inline-block;
background-image: url(../images/add.png);
box-sizing: border-box;
background-size: 30px 30px;
background-position: center center;
background-repeat: no-repeat;
border: 1px solid #dbdbdb;
margin-left: 3.3333%;
margin-bottom: 12px;
}
#chose_pic_btn:hover{
border: 1px solid #666;
}
#chose_pic_btn:active{
border: 1px solid #dbdbdb;
}

#chose_pic_btn input {
position: absolute;
left: 0px;
top: 0px;
opacity: 0;
width: 100%;
height: 100%;
}
.release_btn {
padding: 0 24px;
margin-top: 70px;
}
.release_btn button {
width: 100%;
background-color: #2c87af;
font-size: 1.4rem;
color: #fff;
border: none;
border-radius: 3px;
height: 45px;
outline: none;
}
.release_btn button.none_btn {
background-color: #f2f2f2;
color: #2c87af;
border: 1px solid #2c87af;
margin-top: 15px;
}

 

 

html代码:

<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="select" >添加图片:<br><samp>(jpg、png、bmp、jpeg格式)</samp></label>
<div class="col-md-6 col-sm-6 col-xs-12" >
<div id="imgList" >

</div>
<span id="chose_pic_btn" class="col-xs-3 col-md-3 " style="width: 70px;height: 70px;margin: 0px" >
<input type="file" id="inputImg" name="files" accept="image/*" title="" index="0">
</span>
</div>
</div>

 在<input>中定义了一个属性"index",目的是将图片标签<img>与<input>标签绑定。

 

 

js:

$(document).on('change',"#inputImg", function(){
//文件类型判断
var filepath=$(this).val();
if(filepath=="")
{
return;
}
var extStart=filepath.lastIndexOf(".");
var ext=filepath.substring(extStart,filepath.length).toUpperCase(); //获取文件后缀名
if(".jpg|.png|.bmp|.jpeg".toUpperCase().indexOf(ext.toUpperCase())==-1){
alert("只允许上传jpg、png、bmp、jpeg格式的图片");
return false;
}

var url=window.URL.createObjectURL(document.getElementById("inputImg").files.item(0));   //base64转编码,生成图片到前端
var index=parseInt($(this).attr('index'));  //获取当前index的值
lrz(url,{width:1600,height:900})    //压缩图片比例
.then(function (rst) {

// 处理成功会执行
var img = new Image();
var str="<div class='col-xs-2 col-md-2 ' style='width: 110px;height: 70px' index='"+index+"'><img data-scale='3' class='img-rounded zoomify' style='border-radius: 2px;transform: scale(1) translate(0px, 0px);height: 70px;width: 90px' src='"+rst.base64+"'><em style='visibility: hidden'>x</em></img></div>"
$('#imgList').append(str)
$('#imgList img').zoomify(); //调用zoomify,放大图片
//移入图片显示删除按钮
$('#imgList img').hover(function () {
$(this).next().css("visibility","visible");
})
//移除图片隐藏删除按钮
$('#imgList img').mouseleave(function () {
$(this).next().css("visibility","hidden");
})
//移入删除按钮,显示
$('#imgList em').hover(function () {
$(this).css("visibility","visible");
})
//点击删除按钮,删除照片
$('#imgList em').click(function () {
$(this).parent().remove();
var index=$(this).parent().attr("index");
$("input[index="+index+"]").remove();
})
$("input[index="+index+"]").css("display","none");
$("input[index="+index+"]").attr("id","");
$('#chose_pic_btn').append("<input type=\"file\" id=\"inputImg\" name=\"files\" accept=\"image/*\" title=\"\" index='"+(index+1)+"'>"); //每添加一次index自增一次
return rst;
})
.catch(function (err){
console.log(err)
// 处理失败会执行
})
.always(function () {
// 不管是成功失败,都会执行
});
})

 

后台java:

@RequestMapping(value="/goDataTable/saveInputTable",method=RequestMethod.POST)
public String saveInputTable(
@RequestParam("files") MultipartFile[] files,
Model model,HttpSession session,HttpServletRequest request){if(files!=null&&files.length>1){  /*上传文件不为空*/
for (int i = 0; i < files.length - 1; i++) {
MultipartFile file=files[i];
String fileName= FileTool.getFileNameTime(file);   /*获取文件名称*/
String filePath= FilenameUtils.concat(basePath+"/statics/img", fileName); /* 拼接图片存放路径 */
try {
file.transferTo(new File(filePath));
imageService.addImage(new Img(inputTable.getID(),"/statics/img/"+fileName)); /*将路径存入数据库*/
}catch (Exception e){
System.out.println(e.getMessage());
model.addAttribute("error","添加失败");
e.printStackTrace();
return "403";
}
}
}
return "redirect:/list";
}

 

转载于:https://www.cnblogs.com/yhood/p/11446896.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
bangwu5963 发布了0 篇原创文章 · 获赞 0 · 访问量 848 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: