Are you an LLM? You can read better optimized documentation at /50projects50days-vue3/01-expanding-cards.md for this page in Markdown format
展开卡片
自写重构 demo
loading
官方演示 demo
文章教程
B 站视频教程
源码
详情
vue
<script lang="ts" setup>
import { ref, computed, watch, onMounted } from "vue";
import { v4 as uuidv4 } from "uuid";
import { ElImage, ElLoading } from "element-plus";
import axios from "axios";
/** @see https://www.dmoe.cc/ */
type GetImageResponse = {
code: string;
imgurl: string;
width: string;
height: string;
};
/** 遍历的图片项 */
type Item = {
image: string;
id: string;
};
/**
* 随机图片接口 数据集
* @description
*
* @see https://www.laoluowl.cn/archives/35
* @see https://blog.csdn.net/SectSnow/article/details/115835711
* @see https://www.dmoe.cc/
*/
const imageApiList = [
// 樱花随机二次元图片API
// "https://www.dmoe.cc/random.php",
"https://www.dmoe.cc/random.php?return=json",
// 随机二次元图片API接口
"https://api.vvhan.com/api/acgimg",
];
const imageApi = imageApiList[0];
const isLoading = ref(false);
const service = axios.create({
withCredentials: false,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
},
});
async function getImage() {
return await service.get<GetImageResponse>(imageApi).then((response) => {
return response;
});
}
const items = ref<Item[]>([]);
const itemNumber = 5;
/** `<el-image>` 的 preview-src-list 配置 */
const previewSrcList = computed(() => items.value.map((elm) => elm.image));
async function initItems() {
const asyncList = new Array(itemNumber).fill(1).map((elm) => {
return getImage();
});
isLoading.value = true;
await Promise.all(asyncList)
.then((response) => {
items.value = response.map((res) => {
return {
image: res.data.imgurl,
id: uuidv4(),
};
});
})
.finally(() => {
isLoading.value = false;
});
}
/** 被点击图片项的id值 */
const itemIdClicked = ref<Item["id"]>("");
function initItemIdClicked() {
itemIdClicked.value = items.value?.[0]?.id ?? "";
}
function handleClick(params: Item) {
itemIdClicked.value = params.id;
}
onMounted(async () => {
// https://api.oick.cn/random/api.php?type=pc
// https://www.dmoe.cc/random.php?return=json
fetch("https://api.oick.cn/random/api.php?type=pc", {
mode: "no-cors",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => response.json())
.then((data) => {});
});
</script>
<template>
<section class="ExpandingCards-root">
<!-- v-loading="isLoading"
无法使用 局部导入时 导致了识别错误
-->
<section class="container" v-loading="isLoading">
<ElImage
class="item"
fit="cover"
loading="lazy"
v-for="item in 5"
src="https://api.oick.cn/random/api.php?type=pc"
>
<template #error> </template>
</ElImage>
</section>
</section>
</template>
<style lang="scss" scoped>
.ExpandingCards-root {
// 默认占据一半可视高度
height: 50vh;
width: 100%;
.container {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
gap: 20px;
.item {
flex: 0.5 0;
height: 100%;
cursor: pointer;
transition: all 0.5s ease-in;
&.isClicked {
flex: 4 0;
}
// 反选择器 只要没选择的 就应用 灰色滤镜 和 模糊
&:not(.isClicked) {
filter: grayscale(100%) blur(3px);
}
// 该写法无效
// &:deep(.el-image) {
// opacity: 0.4;
// }
/**
参考资料 https://blog.csdn.net/m0_47097190/article/details/131306821
使用背景图片时 样式设置才有效
*/
// // 保持原有比例
// background-size: cover;
// // 不重复
// background-repeat: no-repeat;
// // 图片居中
// background-position: center;
}
}
}
// 该深度作用选择器写法才是有效的
// .ExpandingCards-root {
// :deep(.el-image) {
// opacity: 0.4;
// }
// }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186