card组件在我们日常开发中经常使用,如果能够有一个较好的封装,将会省去很多的重复代码,提高开发效率。

card 组件需要实现哪些功能呢
必须分为标题和内容两部分,并且都支持自定义
要与cardGroup相结合能够实现水平排列和垂直排列,或者瀑布流排列
能够自定义背景色
提供一种或多种hover特效
card提供点击事件用以交互

<template><div@click="cardClick"class="dx-card-warpper":class="hover":style="background ? { background } : {}"><div class="dx-card-title" v-if="showTitle"><slot name="title" /></div><div class="dx-card-title dx-card-title-default" v-else-if="title"><span class="dx-card-title-default-text">{{ title }}</span></div><div class="dx-card-content"><slot /></div></div>
</template><script lang="ts">
import { ComponentInternalInstance, getCurrentInstance, ref, PropType } from 'vue'export default {props: {title: {required: false,default: '',type: String},hover: {required: false,default: '',type: String as PropType<'boxShadow' | 'floatUp' | 'enlarge'>},background: {required: false,default: '',type: String}},setup() {const currentInstance: ComponentInternalInstance | null = getCurrentInstance()const cardClick = function (e: Event) {currentInstance?.emit('click', e)}// 自定义card头部const showTitle = ref(false)const title = currentInstance?.slots.titleif (title) {showTitle.value = true}return {cardClick,showTitle}}
}
</script><style lang="scss" scoped>
@import '@/scss/layout.scss';.dx-card-warpper {border-radius: 4px;border: $border;cursor: pointer;width: 100%;.dx-card-title {border-bottom: $border;padding: 24px;}.dx-card-title-default {font-size: 16px;font-weight: 600;display: flex;}.dx-card-content {padding: 24px;}
}.dx-card-warpper.boxShadow:hover {box-shadow: 4px 4px 10px rgb(0 0 0 / 29%);
}.dx-card-warpper.floatUp:hover {transform: translateY(-10%);
}.dx-card-warpper.enlarge:hover {transform: scale(1.1);
}
</style>

cardgroup组件

<template><div class="dx-card-group-warpper" :class="ClassName" ref="cardGroup"><slot /></div>
</template><script lang="ts">
import { ref, onMounted } from 'vue'
import { Data } from '../dialog/types'export default {props: {// columnNumber表示每行有多少列columnNumber: {required: false,default: 1,type: Number}},setup(propsData: Data) {// 自定义card头部const cardGroup: any = ref(null)const ClassName = `count-number-${propsData.columnNumber}`const flowClassName = onMounted(() => {// ref只能在onMounted里面才能获取const hasNotCardComponents = [...cardGroup.value.children].some((item: any) => {return item.className !== 'dx-card-warpper'})if (hasNotCardComponents) {// 算是给开发者的题型,在CardGroup里只能使用card组件console.log(Error('Only Card components can be used in CardGroup'))}})return {cardGroup,ClassName}}
}
</script><style lang="scss" scoped>
@import '@/scss/layout.scss';
.dx-card-group-warpper {width: 100%;display: flex;flex-wrap: wrap;align-items: stretch;::v-deep .dx-card-warpper {margin-bottom: 12px;}
}@for $i from 2 through 99 {.count-number-#{$i} {// gap: calc(4% / #{$i - 1});::v-deep .dx-card-warpper {width: calc(96% / #{$i});margin-right: calc(4% / #{$i - 1});}::v-deep .dx-card-warpper:nth-child(#{$i}n + #{$i}) {margin-right: 0 !important;}}
}
</style>

与cardgroup组件一起使用

 <CardGroup :columnNumber="3"><Card>Hover</Card><Card>Hover</Card><Card :title="3">Hover</Card><Card :title="3">Hover</Card></CardGroup>

提供了线上的体验网站,欢迎大家前来,并提出你的建议
http://www.dxyx-together.cn/#/home/card

vue3.0 组件篇 Card相关推荐

  1. vue3.0 组件篇 Tooltip

    tooltip组件构建 定位问题,很重要,top topLeft topRight bottom bottomLeft bottomRight left right,tooltip可以出现在不同的位置 ...

  2. vue3.0 组件使用 引用 传值 父传子 子传父

    vue3.0 组件使用 引用 传值 父传子 子传父 组件的创建 组件引入 在html中使用 组件传值 父传子 子接收参数 子传父 父接收参数 组件的创建 1.将所需功能封装在components目录下 ...

  3. vue3.0 组件传值

    vue3.0组件传值 子组件 代码如下(示例): <template><p @click="handle">{{ test }}</p> < ...

  4. single-spa 集成 vue3.0 生产篇

    上一篇实现的是开发模式下的 微服务集成vue3.0, 接下来讲讲如何实现生产的集成, 废话不多说, 直接上代码 子服务 vite.js 做如下改造 build: {manifest: true,out ...

  5. vue3.0组件之父传子,子传父,父传孙

    父组件<template> <!-- <div>{{name}}</div> {{computedsx}} {{sum}}<button @click= ...

  6. VUE3.0组件兄弟之间传值

    用vue3基础语法操作 引入vue  <script src="https://unpkg.com/vue@next"></script> 页面渲染代码 & ...

  7. VUE3.0——组件传值

    子组件关键代码: 子组件: <template><view class="header"><view class="header-top&q ...

  8. vue3.0父子组件的通信

    vue3.0父子组件的通信 Vue3.0组件通信 Vue3.0组件通信 vue3.0脚手架setup内部的组件通信 1.父到子通过props 父组件 <template><div & ...

  9. 前端Vue3.0搭建后台管理系统

    视频   第1节 - 了解项目开发流程         第1学时 了解产品从0到1的开发流程,产品经理.UI设计师.研发部.测试工程师.mp4            第2节 - 构建vue项目.代码仓 ...

  10. vue3.0实现地图功能

    目前官方没有vue3.0组件,是JavaScript API v3以script标签引入 vue3.0实现地图功能 1.创建秘钥ak 引入script 1.打开index.html 2.插入scrip ...

最新文章

  1. magicui系统会不会升级鸿蒙,华为EMUI 11和Magic UI 4.0同步开启内测,均升级为鸿蒙系统...
  2. 郑州军办计算机学校,郑州市国防科技学校2019级新生开启军训模式
  3. Python类的实例属性详解
  4. [Abp 源码分析]自动审计记录
  5. 基于matlab的数字下变频器的设计与仿真应用,基于MATLAB的数字下变频器的设计与仿真应用.pdf...
  6. 手机连接投影机的步骤_投影机安装过程详解
  7. 打开git界面_使用 Gitea 快速搭建私有 Git 版本控制服务
  8. 【Python系列】python GUI界面
  9. 软件设计师19-系统开发和运行02
  10. 华为机试HJ30:字符串合并处理
  11. wget下载命令使用说明
  12. caffe目录结构 及 caffe源码文件说明
  13. C语言 标准库stdio.h
  14. Java夜未眠(蔡学镛)
  15. 【Python学习】最新版pyecharts之绘制Map地图
  16. sql compare mysql版本_SQL Compare 13免费版
  17. 1602液晶显示屏工作原理引脚电路图51单片机
  18. SRM 613 div1 500pt
  19. 多路PT100转RS485模块
  20. tcp 阻塞与非阻塞

热门文章

  1. 如何在图片上加水印?图片添加、去除水印方法
  2. 小明左右手分别拿两张纸牌,黑桃10和红心8,现在交换手中的牌。编写并输出互换后的结果,输出结果如图。
  3. zk集群和clickhouse集群搭建
  4. 浏览器播放RTSP视频流几种解决方案
  5. matplotlib色彩(colors)之图表数据系列默认配色(默认色彩循环)
  6. Oracle查询排列组合,Oracle SQL排列组合之组合问题
  7. 小米平板1刷los神盾系统
  8. html 中各种鼠标手势
  9. 计算机启动黑屏一段时间,电脑开机为什么黑屏一段时间就好了
  10. 建立数学模型论文写作