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

3 ways to create JavaScript objects

Table of contents 1. Object literals 2. The new k...

Which loop is the fastest in JavaScript?

Knowing which for loop or iterator is right for o...

vue+springboot realizes login verification code

This article example shares the specific code of ...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

Implementing WeChat tap animation effect based on CSS3 animation attribute

Seeing the recent popular WeChat tap function, I ...

Mysql queries the transactions being executed and how to wait for locks

Use navicat to test and learn: First use set auto...

Vue-CLI3.x automatically deploys projects to the server

Table of contents Preface 1. Install scp2 2. Conf...

Simple steps to encapsulate components in Vue projects

Table of contents Preface How to encapsulate a To...

Javascript design pattern prototype mode details

Table of contents 1. Prototype mode Example 1 Exa...

A brief analysis of the responsiveness principle and differences of Vue2.0/3.0

Preface Since vue3.0 was officially launched, man...

Using js to achieve waterfall effect

This article example shares the specific code of ...

Docker Basics

Preface: Docker is an open source application con...

Solution to the problem of repeated triggering of functions in Vue project watch

Table of contents Problem description: Solution 1...