原生js實現(xiàn)上傳圖片控件
本文實例為大家分享了js實現(xiàn)上傳圖片控件的具體代碼,供大家參考,具體內(nèi)容如下
一、修改原生 input 樣式
html 結(jié)構(gòu)
<div class="card">
<input id="upload" type="file" accept=".jpg" />
<div class="view">
<!-- 上傳成功后 -->
<div id="imgContainer" class="img-container">
<img id="img" />
<!-- 鼠標移入展示 查看 與 刪除 操作 -->
<div class="img-mask">
<span id="showImg">查看</span>
<span id="delImg">刪除</span>
</div>
</div>
<!-- 上傳成功前 -->
<span id="icon">+</span>
</div>
</div>
css 樣式
.card {
position: relative;
width: 200px;
height: 140px;
padding: 5px;
margin-right: 20px;
border: 1px dashed #d9d9d9;
border-radius: 6px;
margin: 300px auto;
}
.card input {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
.card .view {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.card .view #icon {
display: inline-block;
font-size: 30px;
}
.card .view .img-container {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display: none;
}
.img-container img {
width: 100%;
height: 100%;
}
.img-container .img-mask {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
background: rgba(0, 0, 0, .3);
transition: all .5s ease;
display: flex;
justify-content: center;
align-items: center;
}
.img-container:hover .img-mask {
opacity: 1;
}
.img-mask span {
color: #fff;
margin: 0 10px;
cursor: pointer;
}
效果展示

二、上傳圖片并展示
監(jiān)聽 input 的 change 事件,圖片上傳成功后創(chuàng)建 URL
<script>
const upload = document.getElementById('upload');
const imgContainer = document.getElementById('imgContainer');
const img = document.getElementById('img');
const icon = document.getElementById('icon');
let imgUrl = '';
// 圖片上傳成功后創(chuàng)建 URL
upload.onchange = function (value) {
const fileList = value.target.files;
if (fileList.length) {
imgContainer.style.display = 'block';
icon.style.display = 'none';
imgUrl = window.URL.createObjectURL(fileList[0]);
img.src = imgUrl;
}
}
<script>
上傳成功后展示

三、實現(xiàn)圖片預(yù)覽
寫一個 modal 框
<!-- 預(yù)覽圖片的 modal 框 -->
<div id="modal">
<span id="closeIcon">關(guān)閉</span>
<div class="content">
<img id="modalImg">
</div>
</div>
modal 樣式
/* modal 樣式 */
#modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 0;
height: 0;
box-shadow: 0 0 10px #d9d9d9;
background: rgba(0, 0, 0, .3);
/* transition: all .1s ease-in-out; */
overflow: hidden;
}
#modal .content {
box-sizing: border-box;
width: 100%;
height: 100%;
padding: 45px 20px 20px;
display: flex;
justify-content: center;
}
#modal #modalImg {
height: 100%;
}
#modal #closeIcon {
position: absolute;
top: 10px;
right: 10px;
cursor: pointer;
}
然后獲取元素, 監(jiān)聽點擊事件
/* ...接以上代碼 */
const showImg = document.getElementById('showImg');
const delImg = document.getElementById('delImg');
const modal = document.getElementById('modal');
const modalImg = document.getElementById('modalImg');
const closeIcon = document.getElementById('closeIcon');
// 點擊預(yù)覽圖片
showImg.onclick = function () {
modal.style.width = '100%';
modal.style.height = '100%';
modalImg.src = imgUrl;
}
// 關(guān)閉 modal 框
closeIcon.onclick = function () {
modal.style.width = '0';
modal.style.height = '0';
modalImg.src = '';
}
// 刪除上傳的圖片
delImg.onclick = function () {
upload.value = '';
imgUrl = '';
icon.style.display = 'block';
imgContainer.style.display = 'none';
}
預(yù)覽效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
談?wù)凧avaScript類型系統(tǒng)之Math
Math 對象并不像 Date 和 String 那樣是對象的類,因此沒有構(gòu)造函數(shù) Math(),像 Math.sin() 這樣的函數(shù)只是函數(shù),不是某個對象的方法。您無需創(chuàng)建它,通過把 Math 作為對象使用就可以調(diào)用其所有屬性和方法2016-01-01
js動態(tài)生成按鈕并動態(tài)生成8位隨機數(shù)
用js生成按鈕,動態(tài)生成8位隨機數(shù)的腳本2008-09-09
JavaScript面向?qū)ο蟪绦蛟O(shè)計創(chuàng)建對象的方法分析
這篇文章主要介紹了JavaScript面向?qū)ο蟪绦蛟O(shè)計創(chuàng)建對象的方法,結(jié)合實例形式分析了javascript使用object構(gòu)造函數(shù)和對象字面量來創(chuàng)建對象的相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
郁悶!ionic中獲取ng-model綁定的值為undefined如何解決
很是郁悶!ionic中獲取ng-model綁定的值為undefined,如何解決?2016-08-08

