1
- function searchSites ( ) {
2
- const query = document . getElementById ( 'searchInput' ) . value . toLowerCase ( ) ;
3
- const categories = document . querySelectorAll ( '.category' ) ;
4
-
5
- categories . forEach ( category => {
6
- const links = category . querySelectorAll ( '.link-item' ) ;
7
- let hasVisibleLinks = false ;
8
-
9
- links . forEach ( link => {
10
- const text = link . textContent . toLowerCase ( ) ;
11
- if ( text . includes ( query ) ) {
12
- link . style . display = 'block' ;
13
- hasVisibleLinks = true ;
14
- } else {
15
- link . style . display = query === '' ? 'block' : 'none' ;
16
- if ( query === '' ) hasVisibleLinks = true ;
17
- }
18
- } ) ;
19
-
20
- category . style . display = hasVisibleLinks ? 'block' : 'none' ;
21
- } ) ;
22
- }
23
1
24
2
function filterCategory ( category ) {
25
3
const categories = document . querySelectorAll ( '.category' ) ;
@@ -71,9 +49,10 @@ function updateCategoryCounts() {
71
49
}
72
50
}
73
51
74
- // 页面加载完成后更新计数
52
+ // 页面加载完成后更新计数和初始化扩展按钮
75
53
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
76
54
updateCategoryCounts ( ) ;
55
+ initExpandButtons ( ) ;
77
56
} ) ;
78
57
79
58
// 搜索框实时搜索
@@ -99,3 +78,85 @@ document.addEventListener('DOMContentLoaded', function () {
99
78
observer . observe ( category ) ;
100
79
} ) ;
101
80
} ) ;
81
+
82
+ // 初始化扩展按钮
83
+ function initExpandButtons ( ) {
84
+ const categories = document . querySelectorAll ( '.category' ) ;
85
+
86
+ categories . forEach ( category => {
87
+ const links = category . querySelector ( '.links' ) ;
88
+ const linkItems = links . querySelectorAll ( '.link-item' ) ;
89
+
90
+ // 如果链接数量超过阈值,添加扩展按钮
91
+ if ( linkItems . length > 8 ) {
92
+ const expandBtn = document . createElement ( 'button' ) ;
93
+ expandBtn . className = 'expand-btn' ;
94
+ expandBtn . textContent = `展开全部 (${ linkItems . length } )` ;
95
+ expandBtn . onclick = function ( ) {
96
+ toggleExpand ( category , expandBtn ) ;
97
+ } ;
98
+
99
+ const footer = document . createElement ( 'div' ) ;
100
+ footer . className = 'category-footer' ;
101
+ footer . appendChild ( expandBtn ) ;
102
+
103
+ category . appendChild ( footer ) ;
104
+ }
105
+ } ) ;
106
+ }
107
+
108
+ // 切换扩展/折叠状态
109
+ function toggleExpand ( category , button ) {
110
+ const links = category . querySelector ( '.links' ) ;
111
+ const isExpanded = links . classList . contains ( 'expanded' ) ;
112
+
113
+ if ( isExpanded ) {
114
+ links . classList . remove ( 'expanded' ) ;
115
+ button . textContent = `展开全部 (${ links . querySelectorAll ( '.link-item' ) . length } )` ;
116
+ button . classList . remove ( 'collapse' ) ;
117
+
118
+ // 平滑滚动到分类顶部
119
+ category . scrollIntoView ( { behavior : 'smooth' , block : 'nearest' } ) ;
120
+ } else {
121
+ links . classList . add ( 'expanded' ) ;
122
+ button . textContent = '收起' ;
123
+ button . classList . add ( 'collapse' ) ;
124
+ }
125
+ }
126
+
127
+ // 增强搜索功能,支持扩展状态
128
+ function searchSites ( ) {
129
+ const query = document . getElementById ( 'searchInput' ) . value . toLowerCase ( ) ;
130
+ const categories = document . querySelectorAll ( '.category' ) ;
131
+
132
+ categories . forEach ( category => {
133
+ const links = category . querySelectorAll ( '.link-item' ) ;
134
+ let hasVisibleLinks = false ;
135
+
136
+ links . forEach ( link => {
137
+ const text = link . textContent . toLowerCase ( ) ;
138
+ if ( text . includes ( query ) ) {
139
+ link . style . display = 'block' ;
140
+ hasVisibleLinks = true ;
141
+ } else {
142
+ link . style . display = query === '' ? 'block' : 'none' ;
143
+ if ( query === '' ) hasVisibleLinks = true ;
144
+ }
145
+ } ) ;
146
+
147
+ category . style . display = hasVisibleLinks ? 'block' : 'none' ;
148
+
149
+ // 搜索时自动展开分类
150
+ if ( hasVisibleLinks && query !== '' ) {
151
+ const linksContainer = category . querySelector ( '.links' ) ;
152
+ if ( linksContainer && ! linksContainer . classList . contains ( 'expanded' ) ) {
153
+ linksContainer . classList . add ( 'expanded' ) ;
154
+ const expandBtn = category . querySelector ( '.expand-btn' ) ;
155
+ if ( expandBtn ) {
156
+ expandBtn . textContent = '收起' ;
157
+ expandBtn . classList . add ( 'collapse' ) ;
158
+ }
159
+ }
160
+ }
161
+ } ) ;
162
+ }
0 commit comments