vue图片查看器插件 vue-photo-preview

这个插件可以在移动端使用哟~~

第一步 在项目下载插件

npm install vue-photo-preview 

第二步 引用配置 main.js里添加一下代码

import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
Vue.use(preview)

第三步 在页面中直接使用

这里说一下渲染的几种情况

1.单个图片直接渲染

<img src="路径" alt="" preview="1"></swiper-item>

效果图:


2.分组图片直接渲染 preview值为相同则为一组

<img src="路径" alt="" preview="1"></swiper-item>
<img src="路径" alt="" preview="1"></swiper-item>
<img src="路径" alt="" preview="1"></swiper-item>
<img src="路径" alt="" preview="1"></swiper-item>

效果图:


3.异步加载图片渲染 图片加载完成后执行this.$previewRefresh()
html代码

<div class="uoload">
      <div class="item" v-for="(item, index) in 4" :key="index"  v-if="arr.length >= index">
        <img :src="arr[index]" alt="" v-if="arr.length >= index + 1" preview="1">
        <img src="../assets/images/clean.png" alt="" class="clean" v-if="arr.length >= index + 1" @click="arr.splice(index, 1)">
        <input type="file" @change="upload($event)" v-if="arr.length <= index">
      </div>
    </div>

js代码

data () {
  return {
    arr: []
  }
},
methods: {
  upload (e) {
    let file = e.target.files[0]
    let reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = (e) => {
      this.arr.push(e.target.result)
      this.$previewRefresh()
    }
  }
}

css代码

.uoload {
      display: flex;
      align-items: center;
      padding: 20px 0 20px 15px;
      .item {
        width:76px;
        height:76px;
        position: relative;
        background: url(../assets/images/upload.png) no-repeat;
        background-size: 100% 100%;
        margin-right: 15px;
        input {
          width:76px;
          height:76px;
          outline: none;
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          opacity: 0;
        }
        >img:nth-of-type(1) {
          width:76px;
          height:76px;
        }
        .clean {
          position: absolute;
          right: -7px;
          top: -7px;
          width:14px !important;
          height:14px !important;
        }
      }
    }

大致的效果图


相关文档: https://npm.taobao.org/package/vue-photo-preview

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    你猜_3214阅读 11,182评论 0 118
  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 1,178评论 0 1
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    王喂马_阅读 6,485评论 1 77
  • Vue学习总结(Vue基础,VueCli3,VueRouter,Vuex) 目录 vue基础知识(1-13)vue...
    Allenem阅读 4,198评论 0 22
  • 我今天加人有点小困难哦~不过就是这样子的啦,以后要换一下点位了,我太难了。 适应了维护跟要资源得平衡,姐...
    月霞係硪阅读 103评论 0 0