This article example shares the specific code of js to implement the upload image control for your reference. The specific content is as follows 1. Modify the native input styleHTML structure <div class="card"> <input id="upload" type="file" accept=".jpg" /> <div class="view"> <!-- After successful upload --> <div id="imgContainer" class="img-container"> <img id="img" /> <!-- Move the mouse to display the view and delete operations--> <div class="img-mask"> <span id="showImg">View</span> <span id="delImg">Delete</span> </div> </div> <!-- Before uploading successfully --> <span id="icon">+</span> </div> </div> CSS Styles .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; } Effect display 2. Upload pictures and display themListen to the input change event and create a URL after the image is uploaded successfully <script> const upload = document.getElementById('upload'); const imgContainer = document.getElementById('imgContainer'); const img = document.getElementById('img'); const icon = document.getElementById('icon'); let imgUrl = ''; // Create a URL after the image is uploaded successfully 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> Display after successful upload 3. Implement image previewWrite a modal box <!-- Modal box for previewing images--> <div id="modal"> <span id="closeIcon">Close</span> <div class="content"> <img id="modalImg"> </div> </div> Modal Style /* modal style */ #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; } Then get the element and listen for click events /* ...Continue with the above code*/ 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'); // Click to preview the image showImg.onclick = function () { modal.style.width = '100%'; modal.style.height = '100%'; modalImg.src = imgUrl; } // Close the modal box closeIcon.onclick = function () { modal.style.width = '0'; modal.style.height = '0'; modalImg.src = ''; } // Delete the uploaded image delImg.onclick = function () { upload.value = ''; imgUrl = ''; icon.style.display = 'block'; imgContainer.style.display = 'none'; } Preview effect picture The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Example analysis of the search function of MySQL regular expressions (regexp and rlike)
>>: Implementation of remote Linux development using vscode
Table of contents 1. Object literals 2. The new k...
Knowing which for loop or iterator is right for o...
This article example shares the specific code of ...
Server matching logic When Nginx decides which se...
Seeing the recent popular WeChat tap function, I ...
Use navicat to test and learn: First use set auto...
Table of contents 1. Introduction to calculator f...
Table of contents Preface 1. Install scp2 2. Conf...
Table of contents Preface How to encapsulate a To...
Table of contents 1. Prototype mode Example 1 Exa...
Preface Since vue3.0 was officially launched, man...
Introduction When writing SQL today, I encountere...
This article example shares the specific code of ...
Preface: Docker is an open source application con...
Table of contents Problem description: Solution 1...