多项卡片展示组件

青旬

前端开发时遇到一个比较特殊的需求,花了点时间写出来这组件,效果如下

该组件支持多张图片显示,可以根据图片数量自适应宽度。

如果传入的数据没有标题简介的话,则图片下侧没有阴影

  1. <template>
  2. <div class="container" :class="{ 'bg-transparent': bgFlag }">
  3. <div class="content">
  4. <div class="title">{{ moduleData.module.name }}</div>
  5. <div class="carousel">
  6. <div class="carousel-item"
  7. v-for="(item, index) in moduleData.children"
  8. :key="index"
  9. @mouseover="expandItem(index)"
  10. :style="{
  11. backgroundImage: `linear-gradient(180deg, rgba(0, 17, 128, 0.00) 0%, rgba(0, 13, 54, 0.60) 100%), url(${initImgPath() + item.module.imgUrl})`,
  12. width: activeIndex === index ? `calc(1200px - 160px * (${moduleData.children.length} - 1))` : '160px',
  13. }"
  14. >
  15. <div class="carousel-item-content">
  16. <transition name="fade">
  17. <div v-if="item.module.title"
  18. class="carousel-item-content-title"
  19. :key="activeIndex === index ? 'visible' : 'hidden'"
  20. :style="{
  21. width: activeIndex !== index ? '150px' : '100%',
  22. justifyContent: activeIndex !== index ? 'center' : 'left',
  23. }">
  24. {{ item.module.title }}
  25. </div>
  26. </transition>
  27. <transition name="fade">
  28. <div v-if="item.module.profile"
  29. v-show="activeIndex === index"
  30. class="carousel-item-content-profile">
  31. {{ item.module.profile }}
  32. </div>
  33. </transition>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import initImgPath from "@/myHooks/initImgPath.js";
  42. export default {
  43. props: {
  44. moduleData: {
  45. type: Object,
  46. required: true,
  47. },
  48. bgFlag: {
  49. type: Boolean,
  50. default: true,
  51. },
  52. },
  53. data() {
  54. return {
  55. activeIndex: 0, // 当前激活的页面索引
  56. };
  57. },
  58. methods: {
  59. initImgPath,
  60. expandItem(index) {
  61. this.activeIndex = index; // 记录当前展开的图片索引
  62. },
  63. },
  64. };
  65. </script>
  66. <style scoped>
  67. .container {
  68. position: relative;
  69. background: url("@/assets/images/9bed669d7b993dc75ead81d19db9fdaf.png") lightgray 50% / cover no-repeat;
  70. box-shadow: 0px 2px 35.686px 0px rgba(5, 42, 68, 0.04), 0px 4px 12.907px 0px rgba(5, 42, 68, 0.03);
  71. display: flex;
  72. flex-direction: column;
  73. padding: 20px 360px 100px 360px;
  74. justify-content: center;
  75. align-items: center;
  76. }
  77. .container::before {
  78. content: "";
  79. position: absolute;
  80. top: 0;
  81. left: 0;
  82. right: 0;
  83. bottom: 0;
  84. background: rgba(244, 247, 251, 0.95);
  85. z-index: 1;
  86. }
  87. .bg-transparent::before {
  88. background: rgba(255, 255, 255, 0.95); /* 半透明的白色背景 */
  89. }
  90. .content {
  91. position: relative;
  92. z-index: 2;
  93. width: 1200px;
  94. }
  95. .title {
  96. color: #222635;
  97. text-align: center;
  98. font-size: 32px;
  99. font-style: normal;
  100. font-weight: 700;
  101. line-height: normal;
  102. letter-spacing: 1.6px;
  103. padding: 60px;
  104. }
  105. .carousel {
  106. width: 1200px; /* 整体宽度 */
  107. height: 600px; /* 整体高度 */
  108. display: flex;
  109. justify-content: center;
  110. overflow: hidden;
  111. gap: 4px;
  112. }
  113. .carousel-item {
  114. display: inline-flex;
  115. width: 160px; /* 未展开状态宽度 */
  116. height: 600px;
  117. padding: 534px 38px 40px 38px;
  118. flex-direction: column;
  119. justify-content: flex-end;
  120. align-items: center;
  121. flex-shrink: 0;
  122. border-radius: 4px;
  123. transition: width 0.3s ease; /* 动画效果 */
  124. background-size: cover;
  125. background-position: center;
  126. }
  127. .carousel-item-content {
  128. width: 100%;
  129. position: relative;
  130. display: flex;
  131. justify-content: center;
  132. align-items: center;
  133. flex-direction: column;
  134. bottom: -20px;
  135. padding: 0;
  136. }
  137. .carousel-item-content-title {
  138. width: 100%;
  139. color: #FFF;
  140. display: flex;
  141. font-size: 20px;
  142. font-style: normal;
  143. font-weight: 600;
  144. line-height: normal;
  145. letter-spacing: 1px;
  146. margin-bottom: 10px;
  147. }
  148. .carousel-item-content-profile {
  149. width: 100%;
  150. color: #D9DDEB;
  151. font-size: 16px;
  152. font-style: normal;
  153. font-weight: 400;
  154. line-height: normal;
  155. letter-spacing: 0.8px;
  156. overflow: hidden; /* 隐藏超出部分 */
  157. display: -webkit-box; /* 使用 flexbox */
  158. -webkit-box-orient: vertical; /* 垂直排列 */
  159. -webkit-line-clamp: 2;
  160. text-align: left;
  161. opacity: 1;
  162. }
  163. .fade-enter-from {
  164. opacity: 0;
  165. }
  166. .fade-leave-from, .fade-leave-to {
  167. display: none;
  168. }
  169. .fade-enter-active, .fade-leave-active {
  170. transition: opacity 0.5s ease;
  171. }
  172. .fade-enter, .fade-enter-to /* .fade-enter-active below version <2.1.8 */ {
  173. opacity: 1;
  174. }
  175. @font-face {
  176. font-family: 'OPPOSans';
  177. src: url('@/assets/fonts/OPPOSans-R.ttf') format('truetype'); /* 确保路径正确 */
  178. font-style: normal; /* 如果有斜体,可以添加 */
  179. }
  180. </style>
html

这个组件的动画比较特殊,首先给carousel-item设置一个0.3秒的width变化动画,因为需要做鼠标光标悬停在当前图片的展开动画。

然后这里并没有使用:hover做鼠标悬停的样式,因为涉及到图片自适应,需要根据当前图片数量来计算出展开后的大小。(未展开默认宽度为160px)

width: activeIndex === index ? `calc(1200px - 160px * (${图片数量} - 1))` : '160px',
html

这样图片的样式就算写好了,接下来开始写标题和简介的样式

图片组件内的标题组件不管是展开还是未展开的状态都是一定会显示的(当传入的数据里面有title的情况),简介组件只有在图片展开的情况才会展示。所以要添加 v-show="activeIndex === index"  这个条件。

然后根据展开和未展开的情况给标题组件添加不同的样式

  1. :style="{
  2. width: activeIndex !== index ? '150px' : '100%',
  3. justifyContent: activeIndex !== index ? 'center' : 'left',
  4. }"
html

因为title没有v-show,所以在使用<transition>前要添加 :key="activeIndex === index ? 'visible' : 'hidden'"  来强制 Vue 重新渲染该组件

接着就是动画的处理了,我设计的是在每次展开图片时,将标题简介设置为完全透明,再通过动画将其渐变为可见状态。

  1. .fade-enter-from {
  2. opacity: 0; // 设置组件在进入动画的始状态为透明
  3. }
  4. .fade-leave-from, .fade-leave-to {
  5. display: none; // 设置组件在离开动画的始末状态为不可见
  6. }
  7. .fade-enter-active, .fade-leave-active {
  8. transition: opacity 0.5s ease; // 透明度渐变的设置
  9. }
  10. .fade-enter, .fade-enter-to /* .fade-enter-active below version <2.1.8 */ {
  11. opacity: 1; // 设置组件在动画进入动画、进入动画的末状态时可见
  12. }
html

最后在最外层元素carousel组件上添加样式

  1. display: flex;
  2. justify-content: center;
html

一个非常简单的多项卡片展示组件就算做好了