-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (63 loc) · 2.24 KB
/
script.js
File metadata and controls
78 lines (63 loc) · 2.24 KB
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
let models = [];
const searchInput = document.getElementById('searchInput');
const modelGallery = document.getElementById('modelGallery');
window.onload = async function () {
const response = await fetch('models.json');
models = await response.json();
const urlParams = new URLSearchParams(window.location.search);
const initialQuery = urlParams.get('q') || '';
if (initialQuery) {
searchInput.value = initialQuery;
}
executeFilter(initialQuery);
searchInput.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
filterCards();
}
});
};
function renderGallery(data) {
modelGallery.innerHTML = '';
if (data.length === 0) {
modelGallery.innerHTML = '<p style="color: #6c757d; font-size: 1.2rem; margin-top: 50px;">抱歉,没有找到匹配的模型。</p>';
return;
}
data.forEach(model => {
const card = document.createElement('div');
card.className = 'card';
card.innerHTML = `
<a href="look.html?model=${encodeURIComponent(model.glb)}" style="text-decoration: none; color: inherit;">
<img src="${model.image}" alt="${model.name}" />
<h3>${model.name}</h3>
</a>
`;
modelGallery.appendChild(card);
});
}
function executeFilter(query) {
if (query === '') {
renderGallery(models);
return;
}
try {
const regex = new RegExp(query, 'i');
const filtered = models.filter(model => regex.test(model.name));
renderGallery(filtered);
} catch (e) {
console.error("搜索关键字无效:", e);
renderGallery(models);
}
}
function filterCards() {
const input = searchInput.value.trim();
const baseUrl = window.location.origin + window.location.pathname;
const urlParams = new URLSearchParams(window.location.search);
if (input) {
urlParams.set('q', input);
} else {
urlParams.delete('q');
}
const newUrl = baseUrl + (urlParams.toString() ? '?' + urlParams.toString() : '');
window.history.pushState({ path: newUrl }, '', newUrl);
executeFilter(input);
}