網頁實現多圖片上傳,預覽,放大等功能


功能需求:

  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";
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM