当前位置:   article > 正文

vuejs 组件_VueJS 2的头像组件

vue2 头像组件

vuejs 组件

Vue头像 (VueAvatar)

Avatar component for VueJS 2

VueJS 2的头像组件

  • Use username to create letter avatar (up to 3 chars)

    使用用户名创建字母头像(最多3个字符)

  • If an image src is provided, will try to use it as avatar

    如果提供了图片src,将尝试将其用作头像

  • Images are lazy loaded. If it is not loaded successfully, it will not be used (letter avatar as fallback)

    图像是延迟加载的。 如果未成功加载,则将不使用它(字母头像作为后备)

安装 (Installation)

npm install --save @lossendae/vue-avatar

用法 (Usage)

ES6 (ES6)

  1. import Vue from 'vue'
  2. import VueAvatar from '@lossendae/vue-avatar'
  3. // If not installed globally
  4. export default {
  5. ...
  6. components: {
  7. VueAvatar
  8. },
  9. ...
  10. }

CommonJS

普通JS

  1. const Vue = require('vue')
  2. const VueAvatar = require('@lossendae/vue-avatar')
  3. // In your component if not installed globally
  4. Vue.extend({
  5. ...
  6. components: {
  7. 'vue-avatar': VueAvatar.VueAvatar
  8. },
  9. ...
  10. })

在全球范围内提供 (Make available globally)

Vue.component('vue-avatar', VueAvatar)

用法 (Usage)

<vue-avatar :username="'Franck Zappatta'" :src="'/path/to/img'"></vue-avatar>

道具 (Props)

NameTypeRequiredDefaultDescription
usernameStringtrue-String used for the letter avatar (up to three characters)
sizeNumberfalse50Size in pixels of the avatar
srcStringfalse-Optional image source path to use for the avatar
colorsArrayfalseDEFAULT_COLORS[] (see below)Pool of colors used for avatar background image (expect array of hsl values)
borderRadiusNumberfalse50Value of the border radius for the avatar
customStylesObjectfalse{}Custom style object to merge with the default style
delayNumberfalse0* See below
名称 类型 需要 默认 描述
用户名 true -- 用于字母头像的字符串(最多三个字符)
尺寸 false 50 化身的大小(以像素为单位)
src false -- 用于化身的可选图像源路径
颜色 数组 false DEFAULT_COLORS[] (如下所示) 用于化身背景图片的颜色池(预期hsl值数组)
borderRadius false 50 化身的边框半径值
customStyles 目的 false {} 自定义样式对象以与默认样式合并
延迟 false 0 * 见下文

delay道具 (delay prop)

Specify the delay in milliseconds to wait before attempting to load the image src.

指定尝试加载图像src之前要等待的延迟(以毫秒为单位)。

This allows to show the letter avatar for the given time and then eventually load the image.

这样可以显示给定时间的字母头像,然后最终加载图像。

Vue-avatar uses vuejs built-in transitions to let you switch smoothly between the letters and the image.

Vue-avatar使用vuejs内置的过渡功能 ,可让您在字母和图像之间平稳切换。

By default, the transition will not do anything, but with a little bit of css you can for example flip the avatar to the image once loaded :

默认情况下,过渡不会执行任何操作,但是只需一点点CSS,您就可以将头像转换为加载后的图像:

  1. /* Add this to your css file or into your component style to flip the avatar into the image if the image is loaded successfully */
  2. /* The animation should also be triggered when the prop src changes */
  3. .vue-avatar-enter-active {
  4. animation: vue-avatar-in .8s;
  5. }
  6. .vue-avatar-leave-active {
  7. animation: vue-avatar-in reverse;
  8. }
  9. @keyframes vue-avatar-in {
  10. from {
  11. transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  12. animation-timing-function: ease-in;
  13. opacity: 0;
  14. }
  15. 40% {
  16. transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
  17. animation-timing-function: ease-in;
  18. }
  19. 60% {
  20. transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
  21. opacity: 1;
  22. }
  23. 80% {
  24. transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  25. }
  26. to {
  27. transform: perspective(400px);
  28. }
  29. }
  30. @keyframes vue-avatar-out {
  31. from {
  32. transform: perspective(400px);
  33. }
  34. 30% {
  35. transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
  36. opacity: 1;
  37. }
  38. to {
  39. transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  40. opacity: 0;
  41. }
  42. }

The above transition was made using animate.css

上面的转换是使用animate.css进行的

默认颜色 (Default colors)

VueAvatar

In you want to override those colors, use the colors props by passing an array of hsl colors

在您要覆盖这些颜色的情况下,通过传递hsl颜色数组来使用颜色道具

See below the values used for the above example:

请参见下面用于以上示例的值:

  1. const COLORS_HSL = [
  2. [6, 71, 60],
  3. [340, 85, 66],
  4. [291, 49, 60],
  5. [263, 49, 63],
  6. [232, 46, 64],
  7. [218, 93, 67],
  8. [187, 73, 70],
  9. [187, 73, 58],
  10. [175, 43, 50],
  11. [151, 44, 53],
  12. [88, 53, 59],
  13. [66, 73, 59],
  14. [51, 95, 53],
  15. [47, 100, 49],
  16. [40, 100, 50],
  17. [16, 100, 69],
  18. [0, 0, 76],
  19. [201, 17, 62],
  20. [17, 16, 56],
  21. [0, 0, 64],
  22. [233, 47, 79],
  23. [262, 49, 74],
  24. [0, 0, 76],
  25. [187, 73, 70],
  26. [15, 15, 69],
  27. [88, 52, 67],
  28. ]

发展历程 (Development)

  1. # Install dependencies
  2. yarn install
  3. # run the unit tests
  4. yarn test
  5. # Build
  6. yarn build

翻译自: https://vuejsexamples.com/avatar-component-for-vuejs-2/

vuejs 组件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/120938?site
推荐阅读
相关标签
  

闽ICP备14008679号