![How to upload multiple images in layui to implement deletion function](https://img.php.cn/upload/article/000/000/039/5e12ff7bb8b56327.jpg)
在使用layui的多图上传时发现没有删除功能
![1578303093148669.jpg How to upload multiple images in layui to implement deletion function](https://img.php.cn/upload/image/662/395/538/1578303093148669.jpg)
在网上搜索解决办法时有的感觉太复杂有的不符合自己所需要的所以就自己动手
![1578303116112508.jpg How to upload multiple images in layui to implement deletion function](https://img.php.cn/upload/image/291/944/755/1578303116112508.jpg)
下面附上代码
HTML:
1 2 3 4 5 6 7 8 9 | <div class = "layui-upload" >
<button type= "button" class = "layui-btn" id= "test2" >多图片上传</button>
<blockquote class = "layui-elem-quote layui-quote-nm" style= "margin-top: 10px;width: 88%" >
预览图:
<div class = "layui-upload-list uploader-list" style= "overflow: auto;" id= "uploader-list" >
</div>
</blockquote>
</div>
|
Copy after login
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <style type= "text/css" >
.uploader-list {
margin-left: -15px;
}
.uploader-list .info {
position: relative;
margin-top: -25px;
background-color: black;
color: white;
filter: alpha(Opacity=80);
-moz-opacity: 0.5;
opacity: 0.5;
width: 100px;
height: 25px;
text-align: center;
display: none;
}
.uploader-list .handle {
position: relative;
background-color: black;
color: white;
filter: alpha(Opacity=80);
-moz-opacity: 0.5;
opacity: 0.5;
width: 100px;
text-align: right;
height: 18px;
margin-bottom: -18px;
display: none;
}
.uploader-list .handle i {
margin-right: 5px;
}
.uploader-list .handle i:hover {
cursor: pointer;
}
.uploader-list .file-iteme {
margin: 12px 0 0 15px;
padding: 1px;
float: left;
}
</style>
|
Copy after login
js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | upload.render({
elem: '#test2'
,url: ''
,multiple: true
,before: function (obj){
layer.msg('图片上传中...', {
icon: 16,
shade: 0.01,
time: 0
})
}
,done: function (res){
layer.close(layer.msg());
$('#uploader-list').append(
'<div id= "" class = "file-iteme" >' +
'<div class = "handle" ><i class = "layui-icon layui-icon-delete" ></i></div>' +
'<img style= "max-width:90%" src='+ res.data.src +' alt= "How to upload multiple images in layui to implement deletion function" >' +
'<div class = "info" >' + res.data.title + '</div>' +
'</div>'
);
}
});
|
Copy after login
1 2 3 4 5 6 7 8 9 10 11 | $(document).on( "mouseenter mouseleave" , ".file-iteme" , function (event){
if (event.type === "mouseenter" ){
$(this).children( ".info" ).fadeIn( "fast" );
$(this).children( ".handle" ).fadeIn( "fast" );
} else if (event.type === "mouseleave" ) {
$(this).children( ".info" ).hide();
$(this).children( ".handle" ).hide();
}
});
|
Copy after login
1 2 3 4 | $(document).on( "click" , ".file-iteme .handle" , function (event){
$(this).parent().remove();
});
|
Copy after login
thinkphp处理上传文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public function upload(){
$file = request()->file('file');
if ( $file ){
$info = $file ->validate(['size'=>2097152,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . ' public ' . DS . 'uploads');
if ( $info ){
$src ='./uploads/'. str_replace ('\\', "/" , $info ->getSaveName());
$image = Image::open( $src );
$image ->thumb(750, 750)->save( $src );
$res ['code']=0;
$res ['msg']='上传成功!';
$res ['data']['src']='/uploads/'. str_replace ('\\', "/" , $info ->getSaveName());
$res ['data']['title']= $info ->getFilename();
} else {
$res ['code']=1;
$res ['msg']='上传失败!'. $file ->getError();
}
return $res ;
}
}
|
Copy after login
更多layui知识请关注PHP中文网layui使用教程栏目。
The above is the detailed content of How to upload multiple images in layui to implement deletion function. For more information, please follow other related articles on the PHP Chinese website!