Html css拖拽和dop文件上传风格

时间:2017-11-09 14:23:28

标签: jquery html css forms file

我正在制作一个html css和javascript拖放文件上传系统。我也设置了文件按钮的样式。但是,左上角显示了一些文件按钮背景。我想删除它。但是,我不能。还有另一个问题。当我在“云图标”或

上拖动图片时

签入JSFIDDLE

请见截图:

Html css drag and dop file uploading style

这是我的所有代码。 的 HTML

<form id="form1"> 
 <label for="imgInp" class="custom-file-upload"> 
      <i class="fa fa-cloud-upload"></i>Chosse file</label>
       <input type='file' id="imgInp" /></div>        
</form>
 <img id="blah" src="">  

CSS

input[type='file'] {
    border: 3px dashed #999;
    cursor: move;
    display: block;
    font-size: 0px;
    filter: alpha(opacity=0);
    min-height: 160px;
    min-width: 300px;
    opacity: 1;
    position: absolute;
    right: 0;
    text-align: right;
    top: 0;
    background: transparent;
    z-index:-99999999999999;
} 
img#blah {
    display: block;
}


.custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
    position: absolute;
    top: 83px;
    left: 50%;
    z-index: 9999;
    width:75px;
    margin-left:-50.5px;
}

#form1 div {
    position: relative;
    width: 300px;
    float: left;
    height: 170px;
}

input[type='file']:before {
    content: "Upload an Image";
    display: block;
    position: absolute;
    text-align: center;
    top: 50%;
    left: 50%;
    width: 200px;
    height: 40px;
    margin: -25px 0 0 -100px;
    font-size: 18px;
    font-weight: bold;
    color: #999;
}

JAVASCRIPT

function readURL(input) {
if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
        $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#imgInp").change(function(){
    readURL(this);
});

1 个答案:

答案 0 :(得分:1)

检查我为文件输入做了display:none并使用jquery使图像点击调用文件输入,并且还将文件输入的每个引用更改为input[type=file]input[type=image]的图像

function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            
            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }
            
            reader.readAsDataURL(input.files[0]);
        }
    }
    $("input[type='image']").click(function() {
    $("input[id='imgInp']").click();
});
    $("#imgInp").change(function(){
        readURL(this);
    });
input[type='image'] {
    border: 3px dashed #999;
    cursor: move;
    display: block;
    font-size: 0px;
    filter: alpha(opacity=0);
    min-height: 160px;
    min-width: 300px;
    opacity: 1;
    position: absolute;
    right: 0;
    text-align: right;
    top: 0;
    background: transparent;
    z-index:-99999999999999;
} 
img#blah {
    display: block;
}
       

.custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
    position: absolute;
    top: 83px;
    left: 50%;
    z-index: 9999;
    width:75px;
    margin-left:-50.5px;
}

#form1 div {
    position: relative;
    width: 300px;
    float: left;
    height: 170px;
}

input[type='image']:before {
    content: "Upload an Image";
    display: block;
    position: absolute;
    text-align: center;
    top: 50%;
    left: 50%;
    width: 200px;
    height: 40px;
    margin: -25px 0 0 -100px;
    font-size: 18px;
    font-weight: bold;
    color: #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1">
   <div>
   
 <label for="imgInp" class="custom-file-upload">  <i class="fa fa-cloud-upload"></i>Chosse file</label>
 <input type="image" id="blah">
 <input type='file' id="imgInp" style="display:none;"/></div>  
        
    </form>