Files
monkey-ai-web/src/docs/script.js
T
2026-10-15 16:33:46 +08:00

568 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function () {
// 初始化代码高亮
hljs.highlightAll();
// 初始化侧边栏导航
initSidebarNavigation();
// 初始化操作系统选择器
initOSSelector();
// 初始化主题切换
initThemeToggle();
// 添加滚动动画
initScrollAnimations();
console.log(
'%c🤖 AI CLI 工具指南',
'font-size: 20px; font-weight: bold; color: #2563eb;',
);
console.log(
'%c欢迎访问!采用全新布局设计',
'font-size: 14px; color: #64748b;',
);
});
// 侧边栏导航功能
function initSidebarNavigation() {
const navItems = document.querySelectorAll('.nav-item');
const tabContents = document.querySelectorAll('.tab-content');
navItems.forEach((item) => {
item.addEventListener('click', function () {
const targetTab = this.getAttribute('data-tab');
// 移除所有活动状态
navItems.forEach((nav) => nav.classList.remove('active'));
tabContents.forEach((content) => content.classList.remove('active'));
// 添加当前活动状态
this.classList.add('active');
const targetContent = document.getElementById(targetTab);
if (targetContent) {
targetContent.classList.add('active');
// 滚动到顶部
document.querySelector('.content-area').scrollTo({
top: 0,
behavior: 'smooth',
});
}
});
});
}
// 操作系统选择器功能
function initOSSelector() {
const osButtons = document.querySelectorAll('.os-btn');
const osContents = document.querySelectorAll('.os-content');
osButtons.forEach((button) => {
button.addEventListener('click', function () {
const targetOS = this.getAttribute('data-os');
// 移除所有活动状态
osButtons.forEach((btn) => btn.classList.remove('active'));
osContents.forEach((content) => content.classList.remove('active'));
// 添加当前活动状态
this.classList.add('active');
const targetContent = document.getElementById(targetOS + '-deploy');
if (targetContent) {
targetContent.classList.add('active');
// 平滑滚动到内容
setTimeout(() => {
targetContent.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
}, 100);
}
});
});
}
// 代码复制功能
function copyCode(button) {
const codeWrapper = button.closest('.code-wrapper');
const codeElement = codeWrapper.querySelector('code');
const code = codeElement.textContent;
// 使用现代 Clipboard API
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard
.writeText(code)
.then(() => {
showCopySuccess(button);
})
.catch((err) => {
console.error('复制失败:', err);
fallbackCopyText(code, button);
});
} else {
fallbackCopyText(code, button);
}
}
// 备用复制方法
function fallbackCopyText(text, button) {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
textarea.setSelectionRange(0, 99999);
try {
document.execCommand('copy');
showCopySuccess(button);
} catch (err) {
console.error('复制失败:', err);
button.innerHTML = '<i class="fas fa-times"></i> 复制失败';
setTimeout(() => {
button.innerHTML = '<i class="fas fa-copy"></i> 复制';
}, 2000);
}
document.body.removeChild(textarea);
}
// 显示复制成功
function showCopySuccess(button) {
const originalHTML = button.innerHTML;
button.innerHTML = '<i class="fas fa-check"></i> 已复制!';
button.style.background = '#10b981';
setTimeout(() => {
button.innerHTML = originalHTML;
button.style.background = '';
}, 2000);
}
// 主题切换功能
function initThemeToggle() {
const themeToggle = document.getElementById('themeToggle');
if (!themeToggle) return;
themeToggle.addEventListener('click', function () {
// 这里可以添加深色模式切换逻辑
const icon = this.querySelector('i');
if (icon.classList.contains('fa-moon')) {
icon.classList.remove('fa-moon');
icon.classList.add('fa-sun');
// 可以添加深色主题样式
console.log('切换到深色模式(功能待实现)');
} else {
icon.classList.remove('fa-sun');
icon.classList.add('fa-moon');
console.log('切换到浅色模式');
}
});
}
// 滚动动画
function initScrollAnimations() {
if (!('IntersectionObserver' in window)) return;
const animatedElements = document.querySelectorAll(
'.feature-card, .info-box, .step-item, .practice-item, .use-case-card',
);
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.style.opacity = '0';
entry.target.style.transform = 'translateY(20px)';
entry.target.style.transition =
'opacity 0.6s ease, transform 0.6s ease';
setTimeout(() => {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}, 100);
observer.unobserve(entry.target);
}
});
},
{
threshold: 0.1,
rootMargin: '0px 0px -50px 0px',
},
);
animatedElements.forEach((el) => observer.observe(el));
}
// 移动端侧边栏切换
function initMobileSidebar() {
const sidebar = document.querySelector('.sidebar');
const overlay = document.createElement('div');
overlay.className = 'sidebar-overlay';
overlay.style.cssText = `
position: fixed;
top: 60px;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: none;
z-index: 998;
`;
document.body.appendChild(overlay);
// 创建移动端菜单按钮
const mobileMenuBtn = document.createElement('button');
mobileMenuBtn.innerHTML = '<i class="fas fa-bars"></i>';
mobileMenuBtn.className = 'mobile-menu-btn';
mobileMenuBtn.style.cssText = `
position: fixed;
top: 10px;
left: 10px;
width: 40px;
height: 40px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.2);
border: none;
color: white;
font-size: 1.25rem;
cursor: pointer;
display: none;
align-items: center;
justify-content: center;
z-index: 1001;
`;
// 检查是否为移动设备
if (window.innerWidth <= 768) {
mobileMenuBtn.style.display = 'flex';
document.body.appendChild(mobileMenuBtn);
}
// 移动端菜单按钮点击事件
mobileMenuBtn.addEventListener('click', function () {
sidebar.classList.toggle('active');
overlay.style.display = sidebar.classList.contains('active')
? 'block'
: 'none';
});
// 点击遮罩关闭侧边栏
overlay.addEventListener('click', function () {
sidebar.classList.remove('active');
overlay.style.display = 'none';
});
// 响应窗口大小变化
window.addEventListener('resize', function () {
if (window.innerWidth <= 768) {
mobileMenuBtn.style.display = 'flex';
} else {
mobileMenuBtn.style.display = 'none';
sidebar.classList.remove('active');
overlay.style.display = 'none';
}
});
}
// 初始化移动端侧边栏
initMobileSidebar();
// 返回顶部按钮
function addBackToTopButton() {
const button = document.createElement('button');
button.innerHTML = '<i class="fas fa-arrow-up"></i>';
button.className = 'back-to-top';
button.style.cssText = `
position: fixed;
bottom: 2rem;
right: 2rem;
width: 50px;
height: 50px;
border-radius: 50%;
background: linear-gradient(135deg, #2563eb, #0ea5e9);
color: white;
border: none;
font-size: 1.25rem;
cursor: pointer;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
z-index: 999;
display: flex;
align-items: center;
justify-content: center;
`;
document.body.appendChild(button);
// 监听内容区域滚动
const contentArea = document.querySelector('.content-area');
if (contentArea) {
contentArea.addEventListener('scroll', function () {
if (contentArea.scrollTop > 300) {
button.style.opacity = '1';
button.style.visibility = 'visible';
} else {
button.style.opacity = '0';
button.style.visibility = 'hidden';
}
});
}
// 点击返回顶部
button.addEventListener('click', function () {
contentArea.scrollTo({
top: 0,
behavior: 'smooth',
});
});
// 悬停效果
button.addEventListener('mouseenter', function () {
this.style.transform = 'translateY(-4px) scale(1.05)';
this.style.boxShadow = '0 10px 15px -3px rgba(0, 0, 0, 0.1)';
});
button.addEventListener('mouseleave', function () {
this.style.transform = 'translateY(0) scale(1)';
this.style.boxShadow = '0 4px 6px -1px rgba(0, 0, 0, 0.1)';
});
}
// 初始化返回顶部按钮
addBackToTopButton();
// 键盘快捷键支持
document.addEventListener('keydown', function (e) {
// ESC 键关闭移动端侧边栏
if (e.key === 'Escape') {
const sidebar = document.querySelector('.sidebar');
const overlay = document.querySelector('.sidebar-overlay');
if (sidebar && sidebar.classList.contains('active')) {
sidebar.classList.remove('active');
if (overlay) overlay.style.display = 'none';
}
}
// 数字键快速导航(1-7)
if (e.key >= '1' && e.key <= '7' && !e.ctrlKey && !e.altKey && !e.shiftKey) {
const navItems = document.querySelectorAll('.nav-item');
const index = parseInt(e.key) - 1;
if (navItems[index]) {
navItems[index].click();
}
}
});
// 链接平滑滚动
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener('click', function (e) {
const href = this.getAttribute('href');
if (href === '#') {
e.preventDefault();
return;
}
const targetId = href.substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
e.preventDefault();
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
}
});
});
// 性能优化:延迟加载图片
function lazyLoadImages() {
if (!('IntersectionObserver' in window)) return;
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.removeAttribute('data-src');
imageObserver.unobserve(img);
}
});
});
images.forEach((img) => imageObserver.observe(img));
}
lazyLoadImages();
// 打印友好处理
window.addEventListener('beforeprint', function () {
document.querySelectorAll('.copy-btn').forEach((btn) => {
btn.style.display = 'none';
});
});
window.addEventListener('afterprint', function () {
document.querySelectorAll('.copy-btn').forEach((btn) => {
btn.style.display = 'flex';
});
});
// 页面可见性API - 优化性能
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
console.log('页面隐藏,暂停动画');
// 可以在这里暂停动画或其他操作
} else {
console.log('页面可见,恢复动画');
// 恢复动画
}
});
// 添加页面加载完成动画
window.addEventListener('load', function () {
const contentArea = document.querySelector('.content-area');
if (contentArea) {
contentArea.style.opacity = '0';
contentArea.style.transition = 'opacity 0.5s ease';
setTimeout(() => {
contentArea.style.opacity = '1';
}, 100);
}
});
// 工具提示功能(可选)
function initTooltips() {
const tooltipElements = document.querySelectorAll('[data-tooltip]');
tooltipElements.forEach((element) => {
element.addEventListener('mouseenter', function () {
const tooltipText = this.getAttribute('data-tooltip');
const tooltip = document.createElement('div');
tooltip.className = 'custom-tooltip';
tooltip.textContent = tooltipText;
tooltip.style.cssText = `
position: absolute;
background: #1e293b;
color: white;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
font-size: 0.875rem;
white-space: nowrap;
z-index: 10000;
pointer-events: none;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
`;
document.body.appendChild(tooltip);
const rect = this.getBoundingClientRect();
tooltip.style.top = rect.top - tooltip.offsetHeight - 10 + 'px';
tooltip.style.left =
rect.left + (rect.width - tooltip.offsetWidth) / 2 + 'px';
this._tooltip = tooltip;
});
element.addEventListener('mouseleave', function () {
if (this._tooltip) {
document.body.removeChild(this._tooltip);
delete this._tooltip;
}
});
});
}
initTooltips();
// 导出全局函数供HTML使用
window.copyCode = copyCode;
// 联系弹窗功能
function showContactInfo() {
const modal = document.getElementById('contactModal');
const overlay = document.getElementById('modalOverlay');
if (modal && overlay) {
modal.classList.add('active');
overlay.classList.add('active');
document.body.style.overflow = 'hidden';
}
}
function closeContactModal() {
const modal = document.getElementById('contactModal');
const overlay = document.getElementById('modalOverlay');
if (modal && overlay) {
modal.classList.remove('active');
overlay.classList.remove('active');
document.body.style.overflow = '';
}
}
// 工具选择器功能
function initToolSelector() {
const toolButtons = document.querySelectorAll('.tool-btn');
const toolDeployments = document.querySelectorAll('.tool-deployment');
toolButtons.forEach((button) => {
button.addEventListener('click', function () {
const tool = this.getAttribute('data-tool');
// 移除所有活动状态
toolButtons.forEach((btn) => btn.classList.remove('active'));
toolDeployments.forEach((deployment) =>
deployment.classList.remove('active'),
);
// 添加当前活动状态
this.classList.add('active');
const targetDeployment = document.getElementById(tool + '-deploy');
if (targetDeployment) {
targetDeployment.classList.add('active');
}
});
});
}
// 页面导航函数
function navigateToTab(tabName) {
const navItem = document.querySelector(`[data-tab="${tabName}"]`);
if (navItem) {
navItem.click();
}
}
// 在DOMContentLoaded中添加初始化
document.addEventListener('DOMContentLoaded', function () {
// 之前的初始化代码...
// 初始化工具选择器
initToolSelector();
// ESC键关闭弹窗
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') {
closeContactModal();
}
});
});
// 导出全局函数
window.showContactInfo = showContactInfo;
window.closeContactModal = closeContactModal;
window.navigateToTab = navigateToTab;