-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (71 loc) · 2.6 KB
/
script.js
File metadata and controls
75 lines (71 loc) · 2.6 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
// 移动端菜单切换
document.addEventListener('DOMContentLoaded', function() {
const menuToggle = document.querySelector('.menu-toggle');
const navMenu = document.querySelector('.nav-menu');
if (menuToggle) {
menuToggle.addEventListener('click', function() {
navMenu.classList.toggle('active');
});
}
// 点击菜单项后关闭移动端菜单
const navLinks = document.querySelectorAll('.nav-menu a');
navLinks.forEach(link => {
link.addEventListener('click', function() {
if (window.innerWidth <= 768) {
navMenu.classList.remove('active');
}
});
});
// 平滑滚动
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// 代码块复制功能
const codeBlocks = document.querySelectorAll('pre code');
codeBlocks.forEach(block => {
const pre = block.parentElement;
if (!pre.querySelector('.copy-btn')) {
const copyBtn = document.createElement('button');
copyBtn.className = 'copy-btn';
copyBtn.textContent = '复制';
copyBtn.style.cssText = `
position: absolute;
top: 10px;
right: 10px;
padding: 0.5rem 1rem;
background: var(--primary-color);
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 0.85rem;
transition: background 0.2s;
`;
copyBtn.addEventListener('mouseenter', () => {
copyBtn.style.background = 'var(--primary-dark)';
});
copyBtn.addEventListener('mouseleave', () => {
copyBtn.style.background = 'var(--primary-color)';
});
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(block.textContent).then(() => {
copyBtn.textContent = '已复制!';
setTimeout(() => {
copyBtn.textContent = '复制';
}, 2000);
});
});
pre.style.position = 'relative';
pre.appendChild(copyBtn);
}
});
});