vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)
自己在写这个组件的时候主要遇到的问题就是在动态传入背景图片或者背景色的时候没能立马顺利写出来,不过现在实现了这个简单组件就和大家分享一下
<template>
<div class="card" :style="bg != undefined ? setBg() : {backgroundColor: '#fff'}">
<div class="cardTop">
<span class="left">
<span>
<avatar class="leftImg" :src="cardImg" alt=""></avatar>
</span>
<span class="content">
<span class="cardName">{{cardName != undefined ? cardName : defaultCardName}}</span>
<span class="cardType">{{cardType != undefined ? cardType : defaultCardType}}</span>
</span>
</span>
<span class="right">
<avatar :src="QRCode" alt=""></avatar>
</span>
</div>
<div class="cardBottom">
<span class="cardNum">卡号:{{cardNum != undefined ? cardNum : defaultCardNum}}</span>
</div>
</div>
</template>
<script>
import Avatar from '@/components/avatar/Avatar.vue';
export default {
components: {
Avatar,
},
props: {
cardImg: {},
QRCode: {},
bg: {},
cardName: {
type: String,
},
cardType: {
type: String,
},
cardNum: {
type: String,
},
},
data() {
return {
backGround: '#fff',
defaultCardName: '阿里云',
defaultCardType: '会员卡',
defaultCardNum: '8888 8888 8888'
}
},
computed: {},
methods: {
setBg() {
let cur = this.bg.lastIndexOf('.'); let img = this.bg.substr(cur + 1); if (/(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(img)) {
return { backgroundImage: 'url(' + this.bg + ')' }
} else {
return { backgroundColor: this.bg }
}
}
}
}
</script>
<style scoped lang="less"> .card {
height: 180px;
max-width: 350px;
display: flex;
flex-direction: column;
border-radius: 10px;
padding: 10px;
justify-content: space-between;
border: 1px solid #ccc;
.cardTop {
display: flex;
justify-content: space-between;
.left {
display: flex;
flex-direction: row;
.leftImg {
height: 70px;
width: 70px;
}
.content {
margin-left: 10px;
min-height: 70px;
display: flex;
flex-direction: column;
justify-content: space-around;
.cardName, .cardType {
font-size: 18px;
}
}
}
.right {
img {
height: 50px;
width: 50px;
}
}
}
.cardBottom { }
} </style>
图片组件如下:
<template>
<span :class="avatarCls">
<img :src="src" v-if="src">
<i v-else-if="icon" :class="['iconfont', `icon-${icon}`]"></i>
<span v-else :class="`${prefixCls}-string`" :style="style" ref="children">
<slot></slot>
</span>
</span>
</template>
<script>
export default {
name: "Avatar",
data() {
return {
prefixCls: "ei-avatar",
scale: 1,
isSlotShow: false,
style: {}
};
},
props: {
size: {
type: String,
default: "large"
},
src: String,
shape: {
type: String,
default: "square"
},
icon: String
},
computed: {
avatarCls() {
const size = { large: "lg", small: "sm" }[this.size];
return [
this.prefixCls,
`${this.prefixCls}-${this.shape}`,
{
[`${this.prefixCls}-${size}`]: !!size,
[`${this.prefixCls}-icon`]: !!this.icon,
[`${this.prefixCls}-image`]: !!this.src
}
];
},
},
methods: {
setScale() {
this.isSlotShow = !this.src && !this.icon;
if (this.$refs.children) {
const childrenWidth = this.$refs.children.offsetWidth;
const avatarWidth = this.$el.getBoundingClientRect().width;
if (avatarWidth - 8 < childrenWidth) {
this.scale = (avatarWidth - 8) / childrenWidth;
} else {
this.scale = 1;
}
}
}
},
mounted() {
this.setScale();
},
updated() {
this.setScale();
}
};
</script>
<style lang="stylus">
@import '../../assets/stylus/variable.styl';
@import '../../assets/stylus/mixin.styl';
.ei-avatar {
display: inline-block;
flex-center(start, center, center)
text-align: center;
background: $avatar-bg;
color: $avatar-color;
white-space: nowrap;
position: relative;
overflow: hidden;
avatar-size($avatar-size-base, $avatar-font-size-base);
&-lg {
avatar-size($avatar-size-lg, $avatar-font-size-lg);
}
&-sm {
avatar-size($avatar-size-sm, $avatar-font-size-sm);
}
&-square {
border-radius: $avatar-border-radius;
}
& > img {
width: 100%;
height: 100%;
display: block;
}
}
</style>
vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)的更多相关文章
- django在style的样式image url添加静态图片路径和django如何动态传入图片链接?
#django在style的样式image url添加静态图片路径 style=" background:url({% static "agribusiness/images/lo ...
- 如何在小程序自定义组件和动态传入数据小demo
在开发过程中,我们会将页面常用功能抽象为一个组件,这样既方便又可以避免代码冗余.小程序中也提供了自定义组件,了解Vue的伙伴们会发现其实和Vue的组件化很相似.这里用返回顶部功能来说说如何自定义组件, ...
- AngularJS下拉列表select在option动态变化之后多出了一个错误项的问题
场景: Select初始化之后,选中select的某个选项 通过AngularJS更新select的选项 错误写法: HTML(使用ng-repeat) <div ng-app="Te ...
- VUE 单选下拉框Select中动态加载 默认选中第一个
<lable>分类情况</lable> <select v-model="content.tid"> <option v-for=&quo ...
- Vue2.0组件实现动态搜索引擎(一)
原文链接:https://blog.csdn.net/qwezxc24680/article/details/74550556 从github上看到一个不错的开源项目:https://github.c ...
- Knockout.js组件系统的详解之(一) - 组件的定义和注册
(Knockout版本:3.4.1 ) KO的组件主要从以下四个部分进行详细介绍: 1.组件的定义和注册 2.组件绑定 3.使用自定义元素 4.自定义组件加载器(高级) 目录结构 1.通过" ...
- Vue加载组件、动态加载组件的几种方式
https://cn.vuejs.org/v2/guide/components.html https://cn.vuejs.org/v2/guide/components-dynamic-async ...
- Vue.js教程--基础(实例 模版语法template computed, watch v-if, v-show v-for, 一个组件的v-for.)
官网:https://cn.vuejs.org/v2/guide/index.html Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统. 视频教程:https: ...
- [Vue]组件——实现动态组件:keep-alive的使用
1.在app.vue中用一个 <keep-alive> 元素将其动态组件包裹起来: keepAlive为true时,第一次被创建的时候缓存下来,为false时,不会缓存 <keep- ...
随机推荐
- XML结构,写到TreeView树上
http://blog.csdn.net/ztzi321/article/details/44077563
- SQL函数_Floor和Celling
1 floor()函数用于获得小于或者等于数值表达式的最大整数,也就是向下取整;celling()函数用于获得大于或者等于数值表达式的最小整数,也就是向上取整: 2 floor()函数和round函数 ...
- vsphere平台windows虚拟机克隆的小插曲(无法登陆系统)
问题: 1.克隆完windows虚拟化后输入法乱码. 2.开启远程的情况下远程登录输入正确的密码也无法登录. 解决: 1.更改管理员用户密码(不输入原win7密码更改win7密码). 2.重新启用管理 ...
- oracle之dbms_output
oracle的dbms_output oracle的dbms_output的两个小问题 .如果dbms_output.put_line的内容不能显示,需要在命令行中先敲入set serveroutpu ...
- tensorflow学习之路---简单的代码
import numpyimport tensorflow as tf #自己创建的数据x_data = numpy.random.rand(100).astype(numpy.float32)#创建 ...
- zip-tar
1.zip 制作压缩文件 (1)格式:zip 压缩文件名 文件1 文件2... zip文件不能用cat查看 (2)选项: -r:用来压缩目录 2.unzip 解压缩文件 (1)格式:unzip 压缩文 ...
- UESTC 360 Another LCIS
Another LCIS Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on UESTC. Original ...
- hdu4565---So Easy!(矩阵)
Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce ...
- [Python] Working with file
Python allows you to open a file, do operations on it, and automatically close it afterwards using w ...
- Maven实战(八)---模块划分
为了防止传递依赖,我们各个模块之间尽量用直接依赖的方式.本篇文章介绍多模块化开发.我们做过Maven项目的都知道.我们的项目一般都是分模块的,每一个模块都会相应着一个POM.xml文件,它们之间通过继 ...