Web前端之路让前端飞

Angular2-仿照微信UI实现9张图片上传和预览

2017-10-18  本文已影响326人  Husbin

样式--weui.css

样例--weui.io

主要步骤

官方ui显示如下,图片上传的ui在Uploader中。

image.png

图片上传的源码从审查元素中可获取,如下所示:

<div class="page uploader js_show">
    <div class="page__hd">
        <h1 class="page__title">Uploader</h1>
        <p class="page__desc">上传组件,一般配合<a class="link" href=" ">组件Gallery</a >来使用。</p >
    </div>
    <div class="page__bd">
        <div class="weui-gallery" id="gallery" style="opacity: 0; display: none;">
            <span class="weui-gallery__img" id="galleryImg" style="background-image:url(./images/pic_160.png)"></span>
            <div class="weui-gallery__opr">
                <a href="javascript:" class="weui-gallery__del">
                    <i class="weui-icon-delete weui-icon_gallery-delete"></i>
                </a >
            </div>
        </div>

        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <div class="weui-uploader">
                        <div class="weui-uploader__hd">
                            <p class="weui-uploader__title">图片上传</p >
                            <div class="weui-uploader__info">0/2</div>
                        </div>
                        <div class="weui-uploader__bd">
                            <ul class="weui-uploader__files" id="uploaderFiles">
                                <li class="weui-uploader__file" style="background-image:url(./images/pic_160.png)"></li>
                                <li class="weui-uploader__file" style="background-image:url(./images/pic_160.png)"></li>
                                <li class="weui-uploader__file" style="background-image:url(./images/pic_160.png)"></li>
                                <li class="weui-uploader__file weui-uploader__file_status" style="background-image:url(./images/pic_160.png)">
                                    <div class="weui-uploader__file-content">
                                        <i class="weui-icon-warn"></i>
                                    </div>
                                </li>
                                <li class="weui-uploader__file weui-uploader__file_status" style="background-image:url(./images/pic_160.png)">
                                    <div class="weui-uploader__file-content">50%</div>
                                </li>
                            </ul>
                            <div class="weui-uploader__input-box">
                                <input id="uploaderInput" class="weui-uploader__input" type="file" accept="image/*" multiple="">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="page__ft j_bottom">
        <a href="javascript:home()">< img src="./images/icon_footer_link.png"></a >
    </div>
</div>
  1. 图片预览/删除部分:
<div class="weui-gallery" id="gallery">
  <!--显示预览-->
  <span class="weui-gallery__img" id="galleryImg"></span>
  <!--删除按钮-->
  <div class="weui-gallery__opr">
    <a href="javascript:" class="weui-gallery__del">
      <i class="weui-icon-delete weui-icon_gallery-delete"></i></a >
  </div>
</div>
  1. 图片缩略图列表部分:
 <ul class="weui-uploader__files" id="uploaderFiles">
   <!--每张图片是一个<li>标签-->
   <li class="weui-uploader__file" style="background-image:url(./images/pic_160.png)"></li>
</ul>

1. 图片缩略图显示

<li class="weui-uploader__file" style="background-image:url(./images/pic_160.png)"></li>

<ul class="weui-uploader__files picture-preview" id="uploaderFiles" >
                                <li *ngFor="let img of picturesUrl"
                                    class="weui-uploader__file"
                                    [ngStyle]="{'background-image':img}">
                                </li>
</ul>

<!--img实例-->
<!--'url(https://img.haomeiwen.com/i7166236/ed8a621900728c39.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)'-->
picturesUrl = [
    'url(https://img.haomeiwen.com/i7166236/40ed406c30ef20a0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)',
    'url(https://img.haomeiwen.com/i7166236/d79762ed654342bf.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)',
    'url(https://img.haomeiwen.com/i7166236/64e1a458e5e29d59.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)',
    'url(https://img.haomeiwen.com/i7166236/9a267a540acb8688.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)',
    'url(https://img.haomeiwen.com/i7166236/283f5687cb73eea8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)',
  ]; //存储图片Url
  title = 'app';
  shown = false;    //是否显示预览,初始化为否
  selectImageUrl: string; //用于存放选中图片的url

2. 图片预览显示与消失

<!--预览隐藏的样式-->
<div class="weui-gallery" id="gallery" style="opacity: 0; display: none;">
 
</div>

<!--预览显示的样式-->
<div class="weui-gallery" id="gallery" style="display: block; opacity: 1;">
  
</div>
<div  class="weui-gallery" id="gallery" style="display: block"
             *ngIf="shown">
            <span class="weui-gallery__img" (click)="touchEvent()" [ngStyle]="{'background-image':selectImageUrl}"></span>
            <div class="weui-gallery__opr">
                <a href="javascript:" (click)="onDelete()" class="weui-gallery__del">
                    <i class="weui-icon-delete weui-icon_gallery-delete"></i>
                </a >
            </div>
</div>
//点击缩略图显示预览
  showPicture($event){
    console.log("$event.target.backgroundImage:" + $event.target.style.backgroundImage);
    this.selectImageUrl = $event.target.style.backgroundImage;
    this.shown = true;
  }

//点击屏幕退出预览
touchEvent(){
    this.shown = false;
  }

3. 图片删除

onDelete(){
    if(isUndefined(this.selectImageUrl)){
      console.log("查看图片预览,图片url未定义,this.selectImageUrl:" + this.selectImageUrl);
      return;
    }
  //正则去除URL中的双引号
    this.selectImageUrl = this.selectImageUrl.replace(/"/g,"");
    console.log("(this.picturesUrl.indexOf(this.selectImageUrl):"+this.picturesUrl.indexOf(this.selectImageUrl));
    //判断图片URL是否存在
    if(this.picturesUrl.indexOf(this.selectImageUrl)!== -1){
      this.picturesUrl.splice(this.picturesUrl.indexOf(this.selectImageUrl) , 1);
      setTimeout(()=>{
          this.shown = false;
        },
        20);
    }else{
      console.log("删除图片出错,获取URL或URL格式出错出错:" + this.selectImageUrl )
    }
  }

效果如下:

显示缩略图:

image.png

显示预览:

image.png

点击下面的删除栏:

image.png

源码戳->demo源码

在线demo->戳你狗头

上一篇下一篇

猜你喜欢

热点阅读