Skip to content

Commit 16546e1

Browse files
authored
Update pub.js
1 parent 81745f4 commit 16546e1

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

cssjs/pub.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
.year-filter {
2-
margin-bottom: 20px;
3-
}
4-
.year-filter button {
5-
margin: 5px;
6-
padding: 10px;
7-
cursor: pointer;
8-
border: 1px solid #ccc;
9-
background-color: #f9f9f9;
10-
}
11-
.year-filter button.active {
12-
background-color: #007bff;
13-
color: white;
14-
}
15-
.publication li {
16-
display: none; /* 默认隐藏所有文章 */
17-
}
18-
.publication li.show {
19-
display: block; /* 显示符合条件的文章 */
1+
function filterPublications(year) {
2+
const publications = document.querySelectorAll('.publication li');
3+
const buttons = document.querySelectorAll('.year-filter button');
4+
5+
// 移除所有按钮的 active 类
6+
buttons.forEach(button => button.classList.remove('active'));
7+
8+
// 为当前点击的按钮添加 active 类
9+
const activeButton = document.querySelector(`.year-filter button[onclick="filterPublications('${year}')"]`);
10+
activeButton.classList.add('active');
11+
12+
// 遍历所有文章,根据年份显示或隐藏
13+
publications.forEach(publication => {
14+
const publicationYear = publication.getAttribute('data-year');
15+
if (year === 'all' || publicationYear === year) {
16+
publication.classList.add('show');
17+
} else {
18+
publication.classList.remove('show');
19+
}
20+
});
2021
}
22+
23+
// 默认显示所有文章
24+
filterPublications('all');

0 commit comments

Comments
 (0)