-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (112 loc) · 3.7 KB
/
Copy pathscript.js
File metadata and controls
128 lines (112 loc) · 3.7 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
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
// Original JavaScript
// Add a hover effect to table of contents items
document.addEventListener('DOMContentLoaded', function () {
const tocItems = document.querySelectorAll('.toc a');
tocItems.forEach(item => {
item.addEventListener('mouseenter', () => {
item.style.backgroundColor = '#f0f0f0';
});
item.addEventListener('mouseleave', () => {
item.style.backgroundColor = '#fff';
});
});
});
// Additional JavaScript
// 合并 DOMContentLoaded 事件监听器
document.addEventListener('DOMContentLoaded', function () {
// 目录项悬停效果
const tocItems = document.querySelectorAll('.toc a, .dropdown-title');
tocItems.forEach(item => {
item.addEventListener('mouseenter', () => item.style.backgroundColor = '#3498db');
item.addEventListener('mouseleave', () => item.style.backgroundColor = '');
});
// 下拉菜单切换
const dropdowns = document.querySelectorAll('.dropdown');
dropdowns.forEach(dropdown => {
const title = dropdown.querySelector('.dropdown-title');
const submenu = dropdown.querySelector('.submenu');
title.addEventListener('click', () => {
submenu.style.display = submenu.style.display === 'block' ? 'none' : 'block';
});
});
});
// 平滑滚动
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth'
});
}
});
});
// 左侧子菜单切换
const submenuBtn = document.querySelector('.submenu-btn-left');
const submenuContent = document.querySelector('.submenu-content-left');
if (submenuBtn && submenuContent) {
submenuBtn.addEventListener('click', function(e) {
e.stopPropagation();
submenuContent.style.display = submenuContent.style.display === 'block' ? 'none' : 'block';
});
}
// 图片放大效果
const datasheetImages = document.querySelectorAll('.datasheet-img');
datasheetImages.forEach(img => {
img.addEventListener('click', () => {
img.classList.toggle('enlarged');
});
});
// 返回顶部按钮
const backToTopButton = document.createElement('button');
backToTopButton.textContent = '返回顶部';
backToTopButton.classList.add('back-to-top');
document.body.appendChild(backToTopButton);
backToTopButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
window.addEventListener('scroll', () => {
if (window.pageYOffset > 300) {
backToTopButton.style.display = 'block';
} else {
backToTopButton.style.display = 'none';
}
});
// 使用模块化代码
import { toggleMenu } from './menu.js';
import { smoothScroll } from './scroll.js';
document.addEventListener('DOMContentLoaded', () => {
toggleMenu();
smoothScroll();
});
// menu.js
export function toggleMenu() {
const dropdowns = document.querySelectorAll('.dropdown');
dropdowns.forEach(dropdown => {
const title = dropdown.querySelector('.dropdown-title');
const submenu = dropdown.querySelector('.submenu');
title.addEventListener('click', () => {
submenu.style.display = submenu.style.display === 'block' ? 'none' : 'block';
});
});
}
// scroll.js
export function smoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth'
});
}
});
});
}