2016-12-06 5 views
1

私は、アップロードするイメージで背景イメージを置き換えるスクリプトを用意しています。ファイルのアップロード時にスパン要素を非表示にする

ここに例があります:DEMOです。

しかし、スパン(.form-file-upload span)を隠して、イメージ全体にその醜い白いブロックがないようにしたいと思います。これはどうすればいいですか?

は、ここに私の現在のコードです:ファイルが選択されている場合

$("input[name='FileOne']").on("change", function(event1) { 
 
     \t src1 = URL.createObjectURL(event1.target.files[0]); 
 
     \t document.querySelector("[for=FileOne]").style.backgroundImage = 'url('+src1+')'; 
 
});
.form-file-upload-container { 
 
    float: left; 
 
    width: 100%; 
 
    margin-bottom: 12px; 
 
    margin-top: 12px 
 
} 
 

 
@media (min-width: 640px) { 
 
    .form-file-upload-container { 
 
     margin-bottom: 20px; 
 
     margin-top: 20px 
 
    } 
 
} 
 

 
.form-file-upload { 
 
    position: relative; 
 
    float: left; 
 
    width: 48.5%; 
 
    height: auto; 
 
    margin-right: 3%; 
 
    margin-bottom: 3% 
 
} 
 

 
@media (min-width: 450px) { 
 
    .form-file-upload { 
 
     width: 31.3333% 
 
    } 
 
} 
 

 
@media (min-width: 750px) { 
 
    .form-file-upload { 
 
     width: 22.75% 
 
    } 
 
} 
 

 
@media (min-width: 1600px) { 
 
    .form-file-upload { 
 
     width: 14.1666667%; 
 
     margin-right: 2% 
 
    } 
 
} 
 

 
.form-file-upload:nth-child(2n) { 
 
    margin-right: 0 
 
} 
 

 
@media (min-width: 450px) { 
 
    .form-file-upload:nth-child(2n) { 
 
     margin-right: 3% 
 
    } 
 
} 
 

 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(2n) { 
 
     margin-right: 2% 
 
    } 
 
} 
 

 
@media (min-width: 450px) { 
 
    .form-file-upload:nth-child(3n) { 
 
     margin-right: 0 
 
    } 
 
} 
 

 
@media (min-width: 750px) { 
 
    .form-file-upload:nth-child(3n) { 
 
     margin-right: 3% 
 
    } 
 
} 
 

 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(3n) { 
 
     margin-right: 2% 
 
    } 
 
} 
 

 
@media (min-width: 750px) { 
 
    .form-file-upload:nth-child(4n) { 
 
     margin-right: 0 
 
    } 
 
} 
 

 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(4n) { 
 
     margin-right: 2% 
 
    } 
 
} 
 

 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(6n) { 
 
     margin-right: 0 
 
    } 
 
} 
 

 
.form-file-upload-inner { 
 
    width: 100%; 
 
    height: 0; 
 
    padding-top: 133.33333% 
 
} 
 

 
.form-file-upload label { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    background: white; 
 
    z-index: 2; 
 
    cursor: pointer; 
 
    border: 1px solid #AFAFB4; 
 
    background-size: cover; 
 
    background-position: center; 
 
    background-repeat: no-repeat; 
 
    color: #AFAFB4; 
 
    -webkit-transition: background 0.2s ease, border 0.2s ease; 
 
    -moz-transition: background 0.2s ease, border 0.2s ease; 
 
    -o-transition: background 0.2s ease, border 0.2s ease; 
 
    transition: background 0.2s ease, border 0.2s ease 
 
} 
 

 
.form-file-upload span { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 0; 
 
    width: 100%; 
 
    font-size: 15px; 
 
    text-align: center; 
 
    -webkit-transform: translateY(-50%); 
 
    -moz-transform: translateY(-50%); 
 
    -ms-transform: translateY(-50%); 
 
    -o-transform: translateY(-50%); 
 
    transform: translateY(-50%) 
 
} 
 

 
.form-file-upload span:before { 
 
    content: ''; 
 
    background: url(../img/image-upload-icon.png) no-repeat center center #fff; 
 
    background: url(../img/image-upload-icon.svg) no-repeat center center #fff; 
 
    background-size: 65px 56px; 
 
    display: block; 
 
    width: 65px; 
 
    height: 56px; 
 
    margin: 0 auto; 
 
    margin-bottom: 5px 
 
} 
 

 
.form-file-upload input { 
 
    position: relative; 
 
    opacity: 0; 
 
    z-index: 1 
 
} 
 

 
.form-file-upload .close { 
 
    display: none; 
 
    position: absolute; 
 
    top: -13px; 
 
    right: -13px; 
 
    width: 26px; 
 
    height: 26px; 
 
    font-size: 18px; 
 
    text-align: center; 
 
    line-height: 26px; 
 
    background: white; 
 
    z-index: 3; 
 
    -webkit-transition: background 0.2s ease, color 0.2s ease; 
 
    -moz-transition: background 0.2s ease, color 0.2s ease; 
 
    -o-transition: background 0.2s ease, color 0.2s ease; 
 
    transition: background 0.2s ease, color 0.2s ease 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-file-upload"> 
 
<div class="form-file-upload-inner"> 
 
     <label for="FileOne"><span>Select Image</span></label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileOne" size="40" class="wpcf7-form-control wpcf7-file" id="FileOne" aria-invalid="false" /></span><button class="close" type="button"></button> 
 
    </div> 
 
</div>

答えて

1

隠す.form-file-upload spandocument.querySelector(".form-file-upload span").style.display= 'none';を使用して

$("input[type='file']").on("change", function(event1) { 
 
    src1 = URL.createObjectURL(event1.target.files[0]); 
 
    this.parentNode.parentNode.querySelector("label").style.backgroundImage = 'url(' + src1 + ')'; 
 
    this.parentNode.parentNode.querySelector("label span").style.display = 'none'; 
 
});
.form-file-upload-container { 
 
    float: left; 
 
    width: 100%; 
 
    margin-bottom: 12px; 
 
    margin-top: 12px; 
 
} 
 
@media (min-width: 640px) { 
 
    .form-file-upload-container { 
 
    margin-bottom: 20px; 
 
    margin-top: 20px 
 
    } 
 
} 
 
.form-file-upload { 
 
    position: relative; 
 
    float: left; 
 
    width: 48.5%; 
 
    height: auto; 
 
    margin-right: 3%; 
 
    margin-bottom: 3%; 
 
} 
 
@media (min-width: 450px) { 
 
    .form-file-upload { 
 
    width: 31.3333% 
 
    } 
 
} 
 
@media (min-width: 750px) { 
 
    .form-file-upload { 
 
    width: 22.75% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload { 
 
    width: 14.1666667%; 
 
    margin-right: 2% 
 
    } 
 
} 
 
.form-file-upload:nth-child(2n) { 
 
    margin-right: 0 
 
} 
 
@media (min-width: 450px) { 
 
    .form-file-upload:nth-child(2n) { 
 
    margin-right: 3% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(2n) { 
 
    margin-right: 2% 
 
    } 
 
} 
 
@media (min-width: 450px) { 
 
    .form-file-upload:nth-child(3n) { 
 
    margin-right: 0 
 
    } 
 
} 
 
@media (min-width: 750px) { 
 
    .form-file-upload:nth-child(3n) { 
 
    margin-right: 3% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(3n) { 
 
    margin-right: 2% 
 
    } 
 
} 
 
@media (min-width: 750px) { 
 
    .form-file-upload:nth-child(4n) { 
 
    margin-right: 0 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(4n) { 
 
    margin-right: 2% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(6n) { 
 
    margin-right: 0 
 
    } 
 
} 
 
.form-file-upload-inner { 
 
    width: 100%; 
 
    height: 0; 
 
    padding-top: 133.33333% 
 
} 
 
.form-file-upload label { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    background: white; 
 
    z-index: 2; 
 
    cursor: pointer; 
 
    border: 1px solid #AFAFB4; 
 
    background-size: cover; 
 
    background-position: center; 
 
    background-repeat: no-repeat; 
 
    color: #AFAFB4; 
 
    -webkit-transition: background 0.2s ease, border 0.2s ease; 
 
    -moz-transition: background 0.2s ease, border 0.2s ease; 
 
    -o-transition: background 0.2s ease, border 0.2s ease; 
 
    transition: background 0.2s ease, border 0.2s ease 
 
} 
 
.form-file-upload span { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 0; 
 
    width: 100%; 
 
    font-size: 15px; 
 
    text-align: center; 
 
    -webkit-transform: translateY(-50%); 
 
    -moz-transform: translateY(-50%); 
 
    -ms-transform: translateY(-50%); 
 
    -o-transform: translateY(-50%); 
 
    transform: translateY(-50%) 
 
} 
 
.form-file-upload span:before { 
 
    content: ''; 
 
    background: url(../img/image-upload-icon.png) no-repeat center center #fff; 
 
    background: url(../img/image-upload-icon.svg) no-repeat center center #fff; 
 
    background-size: 65px 56px; 
 
    display: block; 
 
    width: 65px; 
 
    height: 56px; 
 
    margin: 0 auto; 
 
    margin-bottom: 5px 
 
} 
 
.form-file-upload input { 
 
    position: relative; 
 
    opacity: 0; 
 
    z-index: 1 
 
} 
 
.form-file-upload .close { 
 
    display: none; 
 
    position: absolute; 
 
    top: -13px; 
 
    right: -13px; 
 
    width: 26px; 
 
    height: 26px; 
 
    font-size: 18px; 
 
    text-align: center; 
 
    line-height: 26px; 
 
    background: white; 
 
    z-index: 3; 
 
    -webkit-transition: background 0.2s ease, color 0.2s ease; 
 
    -moz-transition: background 0.2s ease, color 0.2s ease; 
 
    -o-transition: background 0.2s ease, color 0.2s ease; 
 
    transition: background 0.2s ease, color 0.2s ease 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-file-upload"> 
 
    <div class="form-file-upload-inner"> 
 
    <label for="FileOne"><span>Select Image</span> 
 
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileOne" size="40" class="wpcf7-form-control wpcf7-file" id="FileOne" aria-invalid="false" /></span> 
 
    <button class="close" type="button"></button> 
 
    </div> 
 
</div> 
 
<div class="form-file-upload"> 
 
    <div class="form-file-upload-inner"> 
 
    <label for="FileTwo"><span>Select Image</span> 
 
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileTwo" size="40" class="wpcf7-form-control wpcf7-file" id="FileTwo" aria-invalid="false" /></span> 
 
    <button class="close" type="button"></button> 
 
    </div> 
 
</div> 
 
<div class="form-file-upload"> 
 
    <div class="form-file-upload-inner"> 
 
    <label for="FileThree"><span>Select Image</span> 
 
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileThree" size="40" class="wpcf7-form-control wpcf7-file" id="FileThree" aria-invalid="false" /></span> 
 
    <button class="close" type="button"></button> 
 
    </div> 
 
</div>

+0

を削除する場合は、ファイルを検索し、この作品は、IFう私のフォームには複数のファイルフィールドがありますか? – michaelmcgurk

+0

私は答えで複数のアップロードを更新しました。これは、画像アップロードの複数のセットを持っている場合でも動作します – jafarbtech

+1

そのような素敵でクリーンなソリューション - ありがとう、@ jafarbtech: - D – michaelmcgurk

1

https://jsfiddle.net/vafk2Lhy/2/

$("input[name='FileOne']").on("change", function(event1) { 
    src1 = URL.createObjectURL(event1.target.files[0]); 
    document.querySelector("[for=FileOne]").style.backgroundImage = 'url('+src1+')'; 
    if(src1) 
    document.getElementById("holder").style.display = "none"; 
}); 

存在を隠すスパン

+0

私は1つのフォームに複数のファイルアップロードフィールドがある場合、これは動作しますか?ありがとう – michaelmcgurk

+0

@michaelmcgurk(src1)現在src1が[0]ファイルをチェックしていれば、あなたが望むものでこの条件を置き換えることができます。event1.target.files.length> 0そしてファイルがない場合は隠す選択してelse条件を.display = "block"に設定してください:) –

+1

ありがとう、Vinod :-)私はこれに変更しなければならなかったが、それを動作させました:https://jsfiddle.net/vafk2Lhy/3/ – michaelmcgurk

0

background-imagecssspan:beforeの擬似要素関心のうち

$("input[name='FileOne']").on("change", function(event1) { 
 
    src1 = URL.createObjectURL(event1.target.files[0]); 
 
    document.querySelector("[for=FileOne]").style.backgroundImage = 'url(' + src1 + ')'; 
 
    
 
});
.form-file-upload-container { 
 
    float: left; 
 
    width: 100%; 
 
    margin-bottom: 12px; 
 
    margin-top: 12px 
 
} 
 
@media (min-width: 640px) { 
 
    .form-file-upload-container { 
 
    margin-bottom: 20px; 
 
    margin-top: 20px 
 
    } 
 
} 
 
.form-file-upload { 
 
    position: relative; 
 
    float: left; 
 
    width: 48.5%; 
 
    height: auto; 
 
    margin-right: 3%; 
 
    margin-bottom: 3% 
 
} 
 
@media (min-width: 450px) { 
 
    .form-file-upload { 
 
    width: 31.3333% 
 
    } 
 
} 
 
@media (min-width: 750px) { 
 
    .form-file-upload { 
 
    width: 22.75% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload { 
 
    width: 14.1666667%; 
 
    margin-right: 2% 
 
    } 
 
} 
 
.form-file-upload:nth-child(2n) { 
 
    margin-right: 0 
 
} 
 
@media (min-width: 450px) { 
 
    .form-file-upload:nth-child(2n) { 
 
    margin-right: 3% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(2n) { 
 
    margin-right: 2% 
 
    } 
 
} 
 
@media (min-width: 450px) { 
 
    .form-file-upload:nth-child(3n) { 
 
    margin-right: 0 
 
    } 
 
} 
 
@media (min-width: 750px) { 
 
    .form-file-upload:nth-child(3n) { 
 
    margin-right: 3% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(3n) { 
 
    margin-right: 2% 
 
    } 
 
} 
 
@media (min-width: 750px) { 
 
    .form-file-upload:nth-child(4n) { 
 
    margin-right: 0 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(4n) { 
 
    margin-right: 2% 
 
    } 
 
} 
 
@media (min-width: 1600px) { 
 
    .form-file-upload:nth-child(6n) { 
 
    margin-right: 0 
 
    } 
 
} 
 
.form-file-upload-inner { 
 
    width: 100%; 
 
    height: 0; 
 
    padding-top: 133.33333% 
 
} 
 
.form-file-upload label { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    background: white; 
 
    z-index: 2; 
 
    cursor: pointer; 
 
    border: 1px solid #AFAFB4; 
 
    background-size: cover; 
 
    background-position: center; 
 
    background-repeat: no-repeat; 
 
    color: #AFAFB4; 
 
    -webkit-transition: background 0.2s ease, border 0.2s ease; 
 
    -moz-transition: background 0.2s ease, border 0.2s ease; 
 
    -o-transition: background 0.2s ease, border 0.2s ease; 
 
    transition: background 0.2s ease, border 0.2s ease 
 
} 
 
.form-file-upload span { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 0; 
 
    width: 100%; 
 
    font-size: 15px; 
 
    text-align: center; 
 
    background: transparent; 
 
    -webkit-transform: translateY(-50%); 
 
    -moz-transform: translateY(-50%); 
 
    -ms-transform: translateY(-50%); 
 
    -o-transform: translateY(-50%); 
 
    transform: translateY(-50%) 
 
} 
 
.form-file-upload span:before { 
 
    content: ''; 
 
    background-size: 65px 56px; 
 
    display: block; 
 
    width: 65px; 
 
    height: 56px; 
 
    margin: 0 auto; 
 
    margin-bottom: 5px 
 
} 
 
.form-file-upload input { 
 
    position: relative; 
 
    opacity: 0; 
 
    z-index: 1 
 
} 
 
.form-file-upload .close { 
 
    display: none; 
 
    position: absolute; 
 
    top: -13px; 
 
    right: -13px; 
 
    width: 26px; 
 
    height: 26px; 
 
    font-size: 18px; 
 
    text-align: center; 
 
    line-height: 26px; 
 
    background: white; 
 
    z-index: 3; 
 
    -webkit-transition: background 0.2s ease, color 0.2s ease; 
 
    -moz-transition: background 0.2s ease, color 0.2s ease; 
 
    -o-transition: background 0.2s ease, color 0.2s ease; 
 
    transition: background 0.2s ease, color 0.2s ease 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-file-upload"> 
 
    <div class="form-file-upload-inner"> 
 
    <label for="FileOne"><span>Select Image</span> 
 
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileOne" size="40" class="wpcf7-form-control wpcf7-file" id="FileOne" aria-invalid="false" /></span> 
 
    <button class="close" type="button"></button> 
 
    </div> 
 
</div>

+0

複数のファイルフィールドを持つ場合、これはまだ動作しますか? – michaelmcgurk

+0

はい。代わりに 'background'を透明なイメージに設定するか' background-size'を '0px 0px'に調整することができます – guest271314

関連する問題