zoukankan      html  css  js  c++  java
  • 工作笔记——上传图片

    html结构

    <div class="col-xs-12" id="product_image">
                  <div class="base-img-box">
                      
                  </div>
                  <div class="img-box">
                      <div class="add-img">
                           +
                      </div>
                      <input type="hidden" name="imgLen">
                      <input type="file" name="file"  class="img-input" id="img-input1"/>    
                  </div>
             </div> 
    

    js

    function UploadImg(context,fileInput){
            var that = this;
                that.context = context;
                that.fileInput = fileInput;
                that.init();
           };
           UploadImg.prototype = {
    
                init:function(){
                    var that = this;
                    $(that.context).find('.add-img').click(function(){
                        $(that.context).find(that.fileInput).eq(0).trigger('click');                            
                    })
    
                    that.readFile();
                    that.reviewImg();
                },
                readFile:function(){
                    var that = this;
                    var fileInput =$(that.context).find(that.fileInput)[0];
                    $(fileInput).on('change',function(){
                        that.showFile();
                   })
    
                },
                showFile:function(){
                    var that = this;
                    var thisInput = $(that.context).find(that.fileInput)[0];
                    var file = thisInput.files[0];
                    if(!/image/w+/.test(file.type)){
                        alert('请上传图片');
                        return false;
                    }
                    var reader = new FileReader();
                        reader.readAsDataURL(file);
                        reader.onload = function(){                                                       
                            var str = '<div class="img-box font-none"><img src="'+this.result+'" width="150" height="150"></img><input name="" type="hidden"  value="'+this.result+'"><i class="remove-img">X</i></div>';
                                $(that.context).find('.base-img-box').append(str);                                        
                                that.removeImg();
                                that.renameInput();
                                                               
                        }
                },
                removeImg:function(){
                    var that = this;
                    $(that.context).find('.base-img-box .remove-img').on('click',function(){                            
                        $(this).parents('.img-box').remove(); 
                        that.renameInput();                                                      
                    })
                },
                renameInput:function(){
                    var that = this;
                    var inputLen =$(that.context).find('.base-img-box .img-box').length;
                        $(that.context).find('[name="imgLen"]').val($(that.context).find('.base-img-box .img-box').length); 
                        $.each($(that.context).find('.base-img-box .img-box'),function(i,item){
                            var index = i+1;
                            $(item).find('input').attr('name','file'+index);
                        })                                                           
                },
                reviewImg:function(){
                	var that = this;
                	var vl = $('.vl');
                    var html = '';
                    $.each(vl,function(index,item){
                 	    html+='<div class="img-box font-none">';
                        html+= '<input type="hidden" name="" value="'+$(item).val()+'"/><img src="data:image/png;base64,'+$(item).val()+'" width="150" height="150"/>'
                        html+='<i class="remove-img">X</i></div>';
                    })
                    $('.base-img-box').append(html);
                    that.renameInput();
                    that.removeImg();
                }
           };
    
    
           new UploadImg('#product_image','#img-input1'); 
    

      

    css

    /*上传图片*/
    .img-box{
         float:left;
         position:relative;
         150px;
         height:150px;
         margin-right:15px;;
         line-height:150px;
         text-align:center;
         font-size:60px;
         font-weight:800;
         border:1px solid #e5e5e5;
         cursor:pointer;
         overflow:hidden;
         
    }
    .img-box.font-none{
        font-size:0;
    }
    .img-box img{
        150px;
        height:150px;
    }
    .img-input{
         display:none;
         visibility: hidden; 
         position: absolute;
    }
    .remove-img{
        position: absolute;
        display: block;
        30px;
        height:30px;
        top:10px;
        right:10px;
        font-size:20px;
        font-weight: 800;
        line-height: 30px;
        text-align: center;
        font-style: normal;
        color:#fff;
        background-color: rgba(154,7,7,.5);
        border-radius: 100%
    }
    .btn-xs{
    	float:right;
    }
    /*结束 上传图片*/
    

    效果图

    自己写的比较简陋的demo,各位看官手下留情  

  • 相关阅读:
    Discourse 如何不使用 Let’s Encrypt 而使用 CA 签名的密钥进行安装
    Discourse 重复安装过程中的密钥签发问题
    Discourse 升级后提示 https 混合内容
    CentOS 8 安装 docker 报错 containerd.io >= 1.2.2-3
    MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
    培养自己的5项能力
    高效率工作方式
    项目的架构演进过程
    如何预防后台被攻击,且看Tomcat的安全配置
    redis的缓存更新策略,缓存粒度控制
  • 原文地址:https://www.cnblogs.com/MonaSong/p/5942285.html
Copyright © 2011-2022 走看看