🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@ophiuchus/image

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ophiuchus/image

### 介绍

latest
npmnpm
Version
1.0.2
Version published
Weekly downloads
3
-82.35%
Maintainers
1
Weekly downloads
 
Created
Source

Image 图片

介绍

增强版的 img 标签,提供多种图片填充模式,支持图片懒加载、加载中提示、加载失败提示。

引入

import Vue from 'vue';
import Image from '@ophiuchus/image';

Vue.use(Image);

代码演示

基础用法

基础用法与原生 img 标签一致,可以设置 srcwidthheightalt 等原生属性。

<sf-image
  width="100"
  height="100"
  src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>

填充模式

通过 fit 属性可以设置图片填充模式,可选值见下方表格。

<sf-image
  width="10rem"
  height="10rem"
  fit="contain"
  src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>

圆形图片

通过 round 属性可以设置图片变圆,注意当图片宽高不相等且 fitcontainscale-down 时,将无法填充一个完整的圆形。

<sf-image
  round
  width="10rem"
  height="10rem"
  src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>

图片懒加载

设置 lazy-load 属性来开启图片懒加载,需要搭配 Lazyload 组件使用。

<sf-image
  width="100"
  height="100"
  lazy-load
  src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>
import Vue from 'vue';
import Lazyload from '@ophiuchus/lazyload';

Vue.use(Lazyload);

加载中提示

Image 组件提供了默认的加载中提示,支持通过 loading 插槽自定义内容。

<sf-image src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg">
  <template v-slot:loading>
    <sf-loading type="spinner" size="20" />
  </template>
</sf-image>

加载失败提示

Image 组件提供了默认的加载失败提示,支持通过 error 插槽自定义内容。

<sf-image src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg">
  <template v-slot:error>加载失败</template>
</sf-image>

API

Props

参数说明类型默认值
src图片链接string-
fit图片填充模式stringfill
alt替代文本string-
width宽度,默认单位为pxnumber | string-
height高度,默认单位为pxnumber | string-
radius圆角大小,默认单位为pxnumber | string0
round是否显示为圆形booleanfalse
lazy-load是否开启图片懒加载,须配合 Lazyload 组件使用booleanfalse
show-error是否展示图片加载失败提示booleantrue
show-loading是否展示图片加载中提示booleantrue
error-icon失败时提示的图标名称或图片链接stringphoto-fail
loading-icon加载时提示的图标名称或图片链接stringphoto
icon-prefix图标类名前缀,同 Icon 组件的 class-prefix 属性stringsf-icon

图片填充模式 

名称含义
contain保持宽高缩放图片,使图片的长边能完全显示出来
cover保持宽高缩放图片,使图片的短边能完全显示出来,裁剪长边
fill拉伸图片,使图片填满元素
none保持图片原有尺寸
scale-downnonecontain中较小的一个

Events

事件名说明回调参数
click点击图片时触发event: Event
load图片加载完毕时触发-
error图片加载失败时触发-

Slots

名称说明
default自定义图片下方的内容
loading自定义加载中的提示内容
error自定义加载失败时的提示内容

样式变量

组件提供了下列 Less 变量,可用于自定义样式,使用方法请参考主题定制

名称默认值描述
@image-placeholder-text-color@gray-6-
@image-placeholder-font-size@font-size-md-
@image-placeholder-background-color@background-color-
@image-loading-icon-size32px-
@image-loading-icon-color@gray-4-
@image-error-icon-size32px-
@image-error-icon-color@gray-4-

常见问题

如何引用本地图片?

在 .vue 文件中通过相对路径引用本地图片时,需要在图片的链接外包上一层 require(),将图片 URL 转换为 webpack 模块请求,并结合 file-loader 或者 url-loader 进行处理。

<!-- 错误写法 -->
<sf-image src="./image.png" />

<!-- 正确写法 -->
<sf-image :src="__require__('./image.png')" /> // 由于md插件解析的bug,实际使用请把 __require__ 替换成 require。

对此更详细的解释可以参考 vue-loader 的处理资源路径章节。

使用 image 标签无法渲染?

使用 Image 组件时,可能会遇到将 <image> 作为标签名时无法渲染的问题,比如下面的写法:

<template>
  <image src="xxx" />
</template>

<script>
import { Image } from '@ophiuchus/image';

export default {
  components: {
    Image,
  },
};
<script>

这是因为 <image> 标签是原生的 SVG 标签,Vue 不允许将原生标签名注册为组件名,使用 <sf-image> 即可规避这个问题。

Keywords

ui

FAQs

Package last updated on 26 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts