049_Image图片

1. Image图片

1.1. Image图片容器, 在保留原生img的特性下, 支持懒加载, 自定义占位、加载失败等。

1.2. Attributes

参数

说明

类型

可选值

默认值

src

图片源, 同原生

string

fit

确定图片如何适应容器框, 同原生object-fit

string

fill / contain / cover / none / scale-down

alt

原生alt

string

referrer-policy

原生referrerPolicy

string

lazy

是否开启懒加载

boolean

false

scroll-container

开启懒加载后, 监听scroll事件的容器

string / HTMLElement

最近一个 overflow值为auto或scroll的父元素

preview-src-list

开启图片预览功能

Array

z-index

设置图片预览的z-index

Number

2000

1.3. Events

事件名

说明

回调参数

load

图片加载成功触发

(e: Event)

error

图片加载失败触发

(e: Error)

1.4. Slots

名称

说明

placeholder

图片未加载的占位内容

error

加载失败的内容

2. Image图片例子

2.1. 使用脚手架新建一个名为element-ui-image折叠面板的前端项目, 同时安装Element插件。

2.2. 编辑index.js 

import Vue from 'vue'
import VueRouter from 'vue-router'
import Image from '../components/Image.vue'
import PlaceholderImage from '../components/PlaceholderImage.vue'
import ErrorImage from '../components/ErrorImage.vue'
import LazyImage from '../components/LazyImage.vue'
import PreviewImage from '../components/PreviewImage.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/Image' },
  { path: '/Image', component: Image },
  { path: '/PlaceholderImage', component: PlaceholderImage },
  { path: '/ErrorImage', component: ErrorImage },
  { path: '/LazyImage', component: LazyImage },
  { path: '/PreviewImage', component: PreviewImage }
]

const router = new VueRouter({
  routes
})

export default router

2.3. 在components下创建Image.vue

<template>
  <div>
    <h1>基础用法</h1>
    <h4>可通过fit确定图片如何适应到容器框, 同原生object-fit。</h4>
    <div class="imgContainer" v-for="fit in fits" :key="fit">
      <span class="title">{{ fit }}</span>
      <el-image style="width: 100px; height: 100px;" :src="url" :fit="fit"></el-image>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      fits: ['fill', 'contain', 'cover', 'none', 'scale-down'],
      url: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
    }
  }
}
</script>

<style scoped>
  .imgContainer {
    display: inline-block;
    width: 200px;
    text-align: center;
  }
  .title {
    display: inline-block;
    width: 100%;
  }
</style>

2.4. 在components下创建PlaceholderImage.vue

<template>
  <div>
    <h1>占位内容</h1>
    <h4>可通过slot = placeholder可自定义占位内容。</h4>
    <div class="imgContainer">
      <span class="title">默认</span>
      <el-image :src="src" style="width: 300px; height: 300px;"></el-image>
    </div>
    <div class="imgContainer">
      <span class="title">自定义</span>
      <el-image :src="src" style="width: 300px; height: 300px;">
        <div slot="placeholder">加载中<span>...</span></div>
      </el-image>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      src: 'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
    }
  }
}
</script>

<style scoped>
  .imgContainer {
    display: inline-block;
    width: 400px;
    text-align: center;
  }
  .title {
    display: inline-block;
    width: 100%;
  }
</style>

2.5. 在components下创建ErrorImage.vue

<template>
  <div>
    <h1>加载失败</h1>
    <h4>可通过slot = error可自定义加载失败内容。</h4>
    <div class="imgContainer">
      <span class="title">默认</span>
      <el-image style="width: 150px; height: 150px;"></el-image>
    </div>
    <div class="imgContainer">
      <span class="title">自定义</span>
      <el-image style="width: 150px; height: 150px; background-color: #F5F7FA;">
        <div slot="error" style="width: 100%; height: 100%; display: flex; justify-content: center; align-items: center;">
          <i class="el-icon-picture-outline"></i>
        </div>
      </el-image>
    </div>
  </div>
</template>

<style scoped>
  .imgContainer {
    display: inline-block;
    width: 200px;
    text-align: center;
  }
  .title {
    display: inline-block;
    width: 100%;
  }
</style>

2.6. 在components下创建LazyImage.vue

<template>
  <div>
    <h1>懒加载</h1>
    <h4>可通过lazy开启懒加载功能, 当图片滚动到可视范围内才会加载。可通过scroll-container来设置滚动容器, 若未定义, 则为最近一个overflow值为auto或scroll的父元素。</h4>
    <div class="image-lazy">
      <el-image v-for="url in urls" :key="url" :src="url" lazy></el-image>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      urls: [
        'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
        'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg',
        'https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg',
        'https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg',
        'https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg',
        'https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg',
        'https://fuss10.elemecdn.com/2/11/6535bcfb26e4c79b48ddde44f4b6fjpeg.jpeg'
      ]
    }
  }
}
</script>

<style scoped>
  .image-lazy {
    width: 400px;
    height: 400px;
    overflow-y: auto;
  }
  .image-lazy .el-image {
    display: block;
    min-height: 200px;
    margin-bottom: 30px;
  }
  .image-lazy .el-image:last-child {
    margin-bottom: 0;
  }
</style>

2.7. 在components下创建PreviewImage.vue

<template>
  <div>
    <h1>大图预览</h1>
    <h4>可通过previewSrcList开启预览大图的功能。</h4>
    <el-image style="width: 100px; height: 100px" :src="url" :preview-src-list="srcList"></el-image>
  </div>
</template>

<script>
export default {
  data () {
    return {
      url: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
      srcList: [
        'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
        'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg'
      ]
    }
  }
}
</script>

2.8. 运行项目, 访问http://localhost:8080/#/Image

2.9. 运行项目, 访问http://localhost:8080/#/PlaceholderImage 

2.10. 运行项目, 访问http://localhost:8080/#/ErrorImage 

2.11. 运行项目, 访问http://localhost:8080/#/LazyImage 

2.12. 运行项目, 访问http://localhost:8080/#/PreviewImage 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值