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- ...
随机推荐
- mybatis+springmvc+sqlite一个累心的问题:不在纠结
1 java.sql.SQLException: NYI 2 org.sqlite.RS.getColumnClassName(RS.java:269) 在配置mybatis+springmvc+sq ...
- 基于SIFT的点云关键点提取
这篇博文主要介绍SIFT算法在提取点云图像关键点时的具体用法. 尺度不变特征转换(Scale-invariant feature transform,SIFT)是David Lowe在1999年发表, ...
- Vue中对data的操作
1. {{a}} var vm = new Vue({ el: '#app', data: { a: { a: 1, b: 2 } } }) vm.a.c = 'sadoisad' // 按理说是 ...
- 【Henu ACM Round#15 B】A and B and Compilation Errors
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 开3个map, 存在map里面: 然后迭代第一个和第二个map; 分别与第二个和第三个map比较就可以了 [代码] #include ...
- 【Henu ACM Round #13 B】Spider Man
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现每个环可以操作的次数是固定的. (环的大小-1 也就是说一旦环确定了.其实结果就已经确定了. 则直接看操作总数的奇偶性就可以了 ...
- [Python] Problem with Default Arguments
Default arguments are a helpful feature, but there is one situation where they can be surprisingly u ...
- Maven实战(八)---模块划分
为了防止传递依赖,我们各个模块之间尽量用直接依赖的方式.本篇文章介绍多模块化开发.我们做过Maven项目的都知道.我们的项目一般都是分模块的,每一个模块都会相应着一个POM.xml文件,它们之间通过继 ...
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disa
转自:https://blog.csdn.net/ouyangtianhan/article/details/6797999 Unable to find required classes (java ...
- Java证书通信
一.概念介绍: 加密是将数据资料加密,使得非法用户即使取得加密过的资料,也无法获取正确的资料内容,所以数据加密可以保护数据,防止监听攻击.其重点在于数据的安全性.身份认证是用来判断某个身份的真实性 ...
- kali之Nmap (Network Mapper(网络映射器)
Nmap是主机扫描工具,他的图形化界面是Zenmap,分布式框架为Dnamp. Nmap可以完成以下任务: 主机探测 端口扫描 版本检测 系统检测 支持探测脚本的编写 Nmap在实际中应用场合如下: ...