Home Web Front-end Layui Tutorial How to upload multiple images in layui to implement deletion function

How to upload multiple images in layui to implement deletion function

Jan 06, 2020 pm 05:39 PM
layui

How to upload multiple images in layui to implement deletion function

在使用layui的多图上传时发现没有删除功能

How to upload multiple images in layui to implement deletion function

在网上搜索解决办法时有的感觉太复杂有的不符合自己所需要的所以就自己动手

How to upload multiple images in layui to implement deletion function

下面附上代码

HTML:

1

2

3

4

5

6

7

8

9

<div class="layui-upload">

    <button type="button" class="layui-btn" id="test2">多图片上传</button>

    <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;width: 88%">

        预览图:

        <div class="layui-upload-list uploader-list" style="overflow: auto;" id="uploader-list">

                                     

        </div>

    </blockquote>

</div>

Copy after login

CSS:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

<style type="text/css">

        .uploader-list {

            margin-left: -15px;

        }

  

        .uploader-list .info {

            position: relative;

            margin-top: -25px;

            background-color: black;

            color: white;

            filter: alpha(Opacity=80);

            -moz-opacity: 0.5;

            opacity: 0.5;

            width: 100px;

            height: 25px;

            text-align: center;

            display: none;

        }

  

        .uploader-list .handle {

            position: relative;

            background-color: black;

            color: white;

            filter: alpha(Opacity=80);

            -moz-opacity: 0.5;

            opacity: 0.5;

            width: 100px;

            text-align: right;

            height: 18px;

            margin-bottom: -18px;

            display: none;

        }

  

        .uploader-list .handle i {

            margin-right: 5px;

        }

  

        .uploader-list .handle i:hover {

            cursor: pointer;

        }

  

        .uploader-list .file-iteme {

            margin: 12px 0 0 15px;

            padding: 1px;

            float: left;

        }

    </style>

Copy after login

js:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

upload.render({

            elem: &#39;#test2&#39;

            ,url: &#39;&#39;

            ,multiple: true

            ,before: function(obj){

                layer.msg(&#39;图片上传中...&#39;, {

                    icon: 16,

                    shade: 0.01,

                    time: 0

                })

            }

            ,done: function(res){

                layer.close(layer.msg());//关闭上传提示窗口

                //上传完毕

                $(&#39;#uploader-list&#39;).append(

                    &#39;<div id="" class="file-iteme">&#39; +

                    &#39;<div class="handle"><i class="layui-icon layui-icon-delete"></i></div>&#39; +

                    &#39;<img    style="max-width:90%" src=&#39;+ res.data.src +&#39; alt="How to upload multiple images in layui to implement deletion function" >&#39; +

                    &#39;<div class="info">&#39; + res.data.title + &#39;</div>&#39; +

                    &#39;</div>&#39;

                );

            }

        });

Copy after login

1

2

3

4

5

6

7

8

9

10

11

$(document).on("mouseenter mouseleave", ".file-iteme", function(event){

            if(event.type === "mouseenter"){

                //鼠标悬浮

                $(this).children(".info").fadeIn("fast");

                $(this).children(".handle").fadeIn("fast");

            }else if(event.type === "mouseleave") {

                //鼠标离开

                $(this).children(".info").hide();

                $(this).children(".handle").hide();

            }

        });

Copy after login

1

2

3

4

// 删除图片

       $(document).on("click", ".file-iteme .handle", function(event){

           $(this).parent().remove(); 

       });

Copy after login

thinkphp处理上传文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

public function upload(){

        //exit(ROOT_PATH . &#39;public&#39; . DS . &#39;uploads&#39;);

        // 获取表单上传文件 例如上传了00How to upload multiple images in layui to implement deletion function

        $file = request()->file(&#39;file&#39;);

        // 移动到框架应用根目录/public/uploads/ 目录下

        if($file){

            $info = $file->validate([&#39;size&#39;=>2097152,&#39;ext&#39;=>&#39;jpg,png,gif&#39;])->move(ROOT_PATH . &#39;public&#39; . DS . &#39;uploads&#39;); //限定2MB

            if($info){

                $src=&#39;./uploads/&#39;.str_replace(&#39;\\&#39;,"/",$info->getSaveName());

                $image = Image::open($src);

                $image->thumb(750, 750)->save($src);//压缩图片大小

                $res[&#39;code&#39;]=0;

                $res[&#39;msg&#39;]=&#39;上传成功!&#39;;

                $res[&#39;data&#39;][&#39;src&#39;]=&#39;/uploads/&#39;.str_replace(&#39;\\&#39;,"/",$info->getSaveName());

                $res[&#39;data&#39;][&#39;title&#39;]=$info->getFilename();

            }else{

                // 上传失败获取错误信息

                $res[&#39;code&#39;]=1;

                $res[&#39;msg&#39;]=&#39;上传失败!&#39;.$file->getError();

            }

            return $res;

        }

    }

Copy after login

更多layui知识请关注PHP中文网layui使用教程栏目。

The above is the detailed content of How to upload multiple images in layui to implement deletion function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up jump on layui login page How to set up jump on layui login page Apr 04, 2024 am 03:12 AM

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

How to get form data in layui How to get form data in layui Apr 04, 2024 am 03:39 AM

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

How layui implements self-adaptation How layui implements self-adaptation Apr 26, 2024 am 03:00 AM

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

How to transfer data in layui How to transfer data in layui Apr 26, 2024 am 03:39 AM

The method of using layui to transmit data is as follows: Use Ajax: Create the request object, set the request parameters (URL, method, data), and process the response. Use built-in methods: Simplify data transfer using built-in methods such as $.post, $.get, $.postJSON, or $.getJSON.

What is the difference between layui and vue? What is the difference between layui and vue? Apr 04, 2024 am 03:54 AM

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

What does layui mean? What does layui mean? Apr 04, 2024 am 04:33 AM

layui is a front-end UI framework that provides a wealth of UI components, tools and functions to help developers quickly build modern, responsive and interactive web applications. Its features include: flexible and lightweight, modular design, rich components, Powerful tools and easy customization. It is widely used in the development of various web applications, including management systems, e-commerce platforms, content management systems, social networks and mobile applications.

What language is layui framework? What language is layui framework? Apr 04, 2024 am 04:39 AM

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.

The difference between layui framework and vue framework The difference between layui framework and vue framework Apr 26, 2024 am 01:27 AM

layui and vue are front-end frameworks. layui is a lightweight library that provides UI components and tools; vue is a comprehensive framework that provides UI components, state management, data binding, routing and other functions. layui is based on a modular architecture, and vue is based on a componentized architecture. layui has a smaller ecosystem, vue has a large and active ecosystem. The learning curve of layui is low, and the learning curve of vue is steep. Layui is suitable for small projects and rapid development of UI components, while vue is suitable for large projects and scenarios that require rich functions.

See all articles