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- ...
随机推荐
- 微信小程序官方文档中的加密算法
用Nodejs来算一下:
- Tensorflow 学习笔记 -----gradient
Tensorflow 的求梯度函数: [db, dW, dx] = tf.gradient(C, [b, w, x]) 在调试时用处较大. 实例: import tensorflow as tf im ...
- CSUOJ 1551 Longest Increasing Subsequence Again
1551: Longest Increasing Subsequence Again Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 75 Solved ...
- DataGirdView 常用操作
1.将数据源的某列添加到已有DataGirdView的列 例如:将文件夹下所有文件名添加到DataGirdView 的文件名一列,图片如下: 首先在datagridview把文件名列的DATAPROP ...
- 深入浅出WPF 第一部分(3)
3.2.3 属性元素 <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> ...
- 【推荐】适合本科生的网络公开课(MOOC为主),不断更新……
题记:身在海大(湛江),是幸运还是不幸,每一个人有自己的定义.人生不能再来一次,唯有把握当下.提高自己,才可能在不能拼爹的年代靠自身实力前行.或许,我们做不了富二代.但我们每一个人.都有机会成为富二代 ...
- eclipse部署maven web项目到tomcat服务器时,没有将lib、web.xml复制过去的解决办法
我这几天在写项目的时候发现自己以前的项目能够访问,隔一段时间写的这个项目却不能够访问,没有发现代码的逻辑错,但是就是访问不了jsp页面,项目一发布就是出现404错误,后来发现原来是发布到tomcat上 ...
- zoj 1119 / poj 1523 SPF (典型例题 求割点 Tarjan 算法)
poj : http://poj.org/problem?id=1523 如果无向图中一个点 u 为割点 则u 或者是具有两个及以上子女的深度优先生成树的根,或者虽然不是一个根,但是它有一个子女 w, ...
- 认识Linux瘦客户机
(本文完整版见http://os.51cto.com/art/201001/181448.htm) 随着Linux的发展,以及网络计算技术的发展和逐步深入的云计算,基于Li ...
- Linux编译ffmpeg
Linux编译ffmpeg并转换MP3到AMR AMR格式是智能手机上的常用音频文件格式,比如MP3格式的压缩比大,但是文件比MP3小,所以在移动互联项目中应用比较广泛.去年年底协助联想研究院开发一款 ...