In the mini program, you can select a preview image and long-press to delete the image at the same time.

不言
Release: 2018-08-10 14:18:37
Original
3971 people have browsed it

The content of this article is about the code that allows you to select preview images and long press to delete images in the mini program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

I am currently working on a small program project. It is my first contact with it and I am learning while doing it. Currently, I am encountering problems related to image processing. I will record it here and provide some help to friends in need. Similar to posting pictures in WeChat Moments, long press the specified picture to delete

In the mini program, you can select a preview image and long-press to delete the image at the same time.In the mini program, you can select a preview image and long-press to delete the image at the same time.In the mini program, you can select a preview image and long-press to delete the image at the same time.
. Press and hold the picture to delete. If there are less than 9 pictures, you can continue to add
In the mini program, you can select a preview image and long-press to delete the image at the same time.
Picture preview effect

Implementation ideas:

  • Adjust the page to achieve the display effect

  • Use wx.chooseImage to take pictures and select photos

  • Use wx.previewImage to preview pictures

  • Use bindlongpress custom image deletion function

Without further ado, here’s the code:
wxml code


  
    
      
        
          图片上传
          {{files.length}} / 9
        
        
          
            
              
                
              
            
          
          
            
          
        
      
    
  
Copy after login

js Code

Page({
  data: {
    files: []
  },
  chooseImage: function(e) {
    var that = this;    var images = that.data.files;
    wx.chooseImage({
      count: 9 - images.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
      }
    })
  },
  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id,
      urls: this.data.files
    })
  },
  deleteImage: function(e) {
    var that = this;    var images = that.data.files;    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    wx.showModal({
      title: '系统提醒',
      content: '确定要删除此图片吗?',
      success: function(res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {          return false;
        }
        that.setData({
          files: images
        });
      }
    })
  }
})
Copy after login

Related recommendations:

How to customize the showmodal pop-up box in the WeChat applet (with code)

In the WeChat applet How to implement multi-layer nested loops for list rendering

Use rich-text in the applet to implement the ul function (code)

The above is the detailed content of In the mini program, you can select a preview image and long-press to delete the image at the same time.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!