You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
7.5 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. <template>
  2. <!-- 卡片 -->
  3. <div class="card-container">
  4. <div v-for="(live, index) in liveList" :key="index" class="card">
  5. <div class="cover-image" :class="{ 'no-overlay': !live.status == 1 }" style="position:relative;">
  6. <img :src=live.cover alt="节目1">
  7. <div v-if="live.status == 1" class="overlay">即将开播</div>
  8. <div v-if="live.status == 1" class="start-time">{{ getDateDay(live.startTime) }} {{ live.startTime.slice(11, 16) }}开播</div>
  9. </div>
  10. <div class="card-content">
  11. <div class="card-title">{{ live.liveName }}</div>
  12. <div class="card-actions">
  13. <!-- 用户头像 -->
  14. <img :src=live.user.avatar alt="节目1" class="user-avatar">
  15. <!-- 用户昵称与头像同行显示 -->
  16. <div class="card-info">{{ live.user.userName }}</div>
  17. <!-- 预约按钮,如果预约过了按钮变灰色文字变灰色按钮文字为已预约 -->
  18. <a v-if="live.reservation == 0" href="#" class="card-button"
  19. @click="booking(live.id, live.user.userId)">预约</a>
  20. <a v-else href="#" class="card-button" style="background-color: #ccc;">已预约</a>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script setup>
  27. import { ref } from 'vue';
  28. /*
  29. 模拟后端数据
  30. */
  31. const liveList = ref([]);
  32. //获取直播列表
  33. function getLive() {
  34. liveList.value = [
  35. {
  36. id: 1,
  37. cover: "src/assets/live.jpg",
  38. user: {
  39. userId: 1,
  40. userName: "冷辉老师",
  41. avatar: "src/assets/live.jpg"
  42. },
  43. liveName: "猎庄之顶级波段",
  44. startTime: "2024-12-01 09:55",
  45. reservation: 1, //0表示未预约,1表示已预约
  46. status: 1 //0表示未开播,1表示已开播
  47. },
  48. {
  49. id: 2,
  50. cover: "src/assets/live.jpg",
  51. user: {
  52. userId: 2,
  53. userName: "冷辉老师",
  54. avatar: "src/assets/live.jpg"
  55. },
  56. liveName: "猎庄之顶级波段",
  57. startTime: "2024-12-01 09:55",
  58. reservation: 1, //0表示未预约,1表示已预约
  59. status: 1
  60. },
  61. {
  62. id: 3,
  63. cover: "src/assets/live.jpg",
  64. user: {
  65. userId: 3,
  66. userName: "冷辉老师",
  67. avatar: "src/assets/live.jpg"
  68. },
  69. liveName: "猎庄之顶级波段",
  70. startTime: "2024-11-30 12:55",
  71. reservation: 1, //0表示未预约,1表示已预约
  72. status: 1
  73. },
  74. {
  75. id: 4,
  76. cover: "src/assets/live.jpg",
  77. user: {
  78. userId: 4,
  79. userName: "冷辉老师",
  80. avatar: "src/assets/live.jpg"
  81. },
  82. liveName: "猎庄之顶级波段",
  83. startTime: "2024-12-30 12:55",
  84. reservation: 0, //0表示未预约,1表示已预约
  85. status: 1
  86. },
  87. {
  88. id: 5,
  89. cover: "src/assets/live.jpg",
  90. user: {
  91. userId: 5,
  92. userName: "冷辉老师",
  93. avatar: "src/assets/live.jpg"
  94. },
  95. liveName: "猎庄之顶级波段",
  96. startTime: "2024-12-30 12:55",
  97. reservation: 0, //0表示未预约,1表示已预约
  98. status: 1,
  99. }
  100. ]
  101. }
  102. getLive();
  103. // 判断开播日期与当前时间的关系(今天、明天、其他)
  104. function getDateDay(startTime) {
  105. const now = new Date();
  106. const start = new Date(startTime);
  107. const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
  108. if (now.toDateString() == start.toDateString()) {
  109. return "今天";
  110. } else if (now.getTime() + oneDay >= start.getTime()) {
  111. return "明天";
  112. } else {
  113. return startTime.slice(5, 10);
  114. }
  115. }
  116. // //获取用户信息
  117. // const userInfo = ref([]);
  118. // function getUserInfo() {
  119. // userInfo.value = {
  120. // userId: 1,
  121. // userName: "冷辉老师",
  122. // avatar: "src/assets/live.jpg",
  123. // //预约信息
  124. // reservationList: [1,2,3]
  125. // }
  126. // }
  127. // getUserInfo();
  128. // console.log(userInfo.value);
  129. // /* 判断预约状态 */
  130. // function isBooking(liveId) {
  131. // for (let i = 0; i < userInfo.value.reservationList.length; i++) {
  132. // if (userInfo.value.reservationList[i] == liveId) {
  133. // return true;
  134. // }
  135. // }
  136. // return false;
  137. // }
  138. /*点击预约按钮以后显示预约成功的提示,并将按钮变为不可点击状态,且将按钮的文字改为“已预约”,并将按钮的背景颜色改为灰色*/
  139. function booking(liveId, userId) {
  140. alert("预约成功!");
  141. }
  142. </script>
  143. <style scoped>
  144. .card-container {
  145. /*居中显示*/
  146. margin-left: auto;
  147. margin-right: auto;
  148. /*宽度*/
  149. width: 916px;
  150. font-family: Arial, sans-serif;
  151. padding: 0;
  152. flex-wrap: wrap;
  153. /*超出自动换行*/
  154. background-color: #f4f4f4;
  155. display: flex;
  156. align-items: center;
  157. }
  158. .card {
  159. background-color: #fff;
  160. border-radius: 8px;
  161. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  162. margin: 10px;
  163. overflow: hidden;
  164. border: #666;
  165. height: 190px;
  166. width: 209px;
  167. }
  168. .card .cover-image img {
  169. width: 100%;
  170. height: 100%;
  171. /*图片占满整个卡片*/
  172. object-fit: cover;
  173. border-radius: 8px;
  174. }
  175. .cover-image {
  176. position: relative;
  177. height: 120px;
  178. width: 208px;
  179. border-radius: 8px;
  180. }
  181. /*即将开播图层*/
  182. .overlay {
  183. position: absolute;
  184. z-index: 2;
  185. text-align: center;
  186. top: 30%;
  187. left: 50%;
  188. transform: translate(-50%, -50%);
  189. color: #fff;
  190. font-size: 18px;
  191. }
  192. /*开播时间*/
  193. .start-time {
  194. position: absolute;
  195. z-index: 2;
  196. text-align: center;
  197. top: 60%;
  198. left: 50%;
  199. transform: translate(-50%, -50%);
  200. color: #ffffffaf;
  201. font-size: 15px;
  202. width: 120px;
  203. }
  204. .cover-image::before {
  205. content: " ";
  206. position: absolute;
  207. top: 0;
  208. left: 0;
  209. width: 207px;
  210. height: 120px;
  211. /*最后一个参数是半透明度,可以透过调整0-1的数值,调整到满意的透明度*/
  212. background-color: rgba(0, 0, 0, 0.3);
  213. border-radius: 8px;
  214. }
  215. /*用于隐藏样式*/
  216. .cover-image.no-overlay::before {
  217. display: none;
  218. }
  219. .card-content {
  220. padding: 8px;
  221. }
  222. .card-title {
  223. font-size: 13px;
  224. font-weight: bold;
  225. margin-bottom: 5px;
  226. white-space: nowrap;
  227. overflow: hidden;
  228. text-overflow: ellipsis;
  229. }
  230. .card-info {
  231. font-size: 14px;
  232. color: #666;
  233. }
  234. .card-button {
  235. display: inline-block;
  236. padding: 5px 5px;
  237. background-color: #eb8b31;
  238. color: #fff;
  239. text-decoration: none;
  240. border-radius: 20px;
  241. font-size: small;
  242. width: 50px;
  243. text-align: center;
  244. margin-left: auto;
  245. }
  246. .card-actions {
  247. display: flex;
  248. align-items: center;
  249. /* 垂直居中对齐 */
  250. justify-content: space-between;
  251. /* 按钮右对齐 */
  252. }
  253. .user-avatar {
  254. border-radius: 50%;
  255. width: 20px;
  256. height: 20px;
  257. }
  258. .card-info {
  259. margin-left: 5px;
  260. /* 昵称与头像之间的间距 */
  261. margin-top: 0;
  262. /* 移除顶部外边距 */
  263. /*超长以后将内容省略显示*/
  264. white-space: nowrap;
  265. overflow: hidden;
  266. text-overflow: ellipsis;
  267. width: 90px;
  268. font-size: 12px;
  269. }
  270. </style>