原生js實(shí)現(xiàn)上傳圖片控件
本文實(shí)例為大家分享了js實(shí)現(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" /> <!-- 鼠標(biāo)移入展示 查看 與 刪除 操作 --> <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>
上傳成功后展示
三、實(shí)現(xiàn)圖片預(yù)覽
寫一個(gè) 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)聽點(diǎ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'); // 點(diǎn)擊預(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ù)覽效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)上傳圖片的三種方法并實(shí)現(xiàn)預(yù)覽圖片功能
- js實(shí)現(xiàn)上傳圖片預(yù)覽的方法
- 上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實(shí)現(xiàn)示例
- 微信JSSDK上傳圖片
- js實(shí)現(xiàn)上傳圖片之上傳前預(yù)覽圖片
- js實(shí)現(xiàn)ctrl+v粘貼上傳圖片(兼容chrome、firefox、ie11)
- Javascript 驗(yàn)證上傳圖片大小[客戶端]
- jsp中實(shí)現(xiàn)上傳圖片即時(shí)顯示效果功能
- JS上傳圖片前的限制包括(jpg jpg gif及大小高寬)等
- js 上傳圖片預(yù)覽問題
相關(guān)文章
JS實(shí)現(xiàn)點(diǎn)擊事件統(tǒng)計(jì)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇JS實(shí)現(xiàn)點(diǎn)擊事件統(tǒng)計(jì)的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-07-07javascript實(shí)現(xiàn)非常簡(jiǎn)單的小數(shù)取整功能示例
這篇文章主要介紹了javascript實(shí)現(xiàn)非常簡(jiǎn)單的小數(shù)取整功能,結(jié)合具體實(shí)例形式分析了javascript數(shù)學(xué)運(yùn)算取整相關(guān)操作技巧,需要的朋友可以參考下2017-06-06JS寫的數(shù)字拼圖小游戲代碼[學(xué)習(xí)參考]
昨天沒事做,就用JS寫了個(gè)數(shù)字拼圖的小游戲,自?shī)首詷贰? 可惜關(guān)于逆序數(shù)的問題還沒解決,現(xiàn)在有時(shí)是拼不成的,大家見諒了。2008-10-10微信小程序?qū)崿F(xiàn)長(zhǎng)按 識(shí)別圖片二維碼(兩種方案)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)長(zhǎng)按 識(shí)別圖片二維碼(兩種方案),第一種方案只需要在image里面加一個(gè)屬性就可以了,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01js動(dòng)態(tài)添加onload、onresize、onscroll事件(另類方法)
window 的 onload、onresize、onscroll 事件,跟其他的事件不一樣,它不能用 attachEvent 或 addEventListener 來(lái)添加于是本人想了一些另類的方法,需要了解的朋友可以參考下2012-12-12js實(shí)現(xiàn)多選項(xiàng)切換導(dǎo)航菜單的方法
這篇文章主要介紹了js實(shí)現(xiàn)多選項(xiàng)切換導(dǎo)航菜單的方法,可實(shí)現(xiàn)動(dòng)態(tài)生成多選項(xiàng)切換導(dǎo)航菜單的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2015-02-02javascript在IE下trim函數(shù)無(wú)法使用的解決方法
這篇文章主要介紹了javascript在IE下trim函數(shù)無(wú)法使用的解決方法,分別敘述了javascript以及jQuery下的解決方案,對(duì)于WEB前端javascript設(shè)計(jì)人員進(jìn)行瀏覽器兼容性調(diào)試有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-09-09JS獲取CSS樣式(style/getComputedStyle/currentStyle)
這篇文章主要為大家介紹了JS獲取CSS樣式的方法,介紹了CSS樣式的三種分類情況,對(duì)獲取樣式做一個(gè)簡(jiǎn)單的封裝,感興趣的小伙伴們可以參考一下2016-01-01