Native js to realize the upload picture control

Native js to realize the upload picture control

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 style

HTML 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 them

Listen 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 preview

Write 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:
  • JS implements three methods of uploading pictures and realizing the preview picture function
  • js method to realize the preview of uploaded pictures
  • Upload image preview JS script Input file image preview implementation example
  • WeChat JSSDK upload pictures
  • js to upload pictures and preview them before uploading
  • js implements ctrl+v to paste uploaded pictures (compatible with chrome, firefox, ie11)
  • Javascript verifies uploaded image size [client-side]
  • Realize the function of instant display of uploaded pictures in jsp
  • The restrictions before JS uploads pictures include (jpg, jpg, gif, size, height, width), etc.
  • js upload image preview problem

<<:  Example analysis of the search function of MySQL regular expressions (regexp and rlike)

>>:  Implementation of remote Linux development using vscode

Recommend

An article tells you how to write a Vue plugin

Table of contents What is a plugin Writing plugin...

How to implement Mysql scheduled task backup data under Linux

Preface Backup is the basis of disaster recovery....

A small question about the execution order of SQL in MySQL

I encountered a sql problem at work today, about ...

HTML simple web form creation example introduction

<input> is used to collect user information ...

Vue implements adding watermark to uploaded pictures

This article shares the specific implementation c...

MySQL 8.0.20 Installation Tutorial with Pictures and Text (Windows 64-bit)

1: Download from mysql official website https://d...

jQuery implements sliding tab

This article example shares the specific code of ...

Basic knowledge of HTML: a preliminary understanding of web pages

HTML is the abbreviation of Hypertext Markup Langu...

MySQL v5.7.18 decompression version installation detailed tutorial

Download MySQL https://dev.mysql.com/downloads/my...

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

WeChat applet implements jigsaw puzzle game

This article shares the specific code for impleme...

Hide div in HTML Hide table TABLE or DIV content css style

I solved a problem tonight that has been botherin...

The reason why MySQL uses B+ tree as its underlying data structure

We all know that the underlying data structure of...