77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
// 基础JavaScript功能
|
|
|
|
// 返回顶部功能
|
|
window.addEventListener('scroll', function() {
|
|
const backToTop = document.getElementById('backToTop');
|
|
if (window.pageYOffset > 300) {
|
|
backToTop.style.display = 'block';
|
|
} else {
|
|
backToTop.style.display = 'none';
|
|
}
|
|
});
|
|
|
|
function scrollToTop() {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth'
|
|
});
|
|
}
|
|
|
|
// 购物车数量更新
|
|
function updateCartBadge(count) {
|
|
const badge = document.getElementById('cartBadge');
|
|
if (count > 0) {
|
|
badge.textContent = count;
|
|
badge.style.display = 'inline-block';
|
|
} else {
|
|
badge.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
// 页面加载完成后的初始化
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// 当前页面高亮
|
|
const currentPath = window.location.pathname;
|
|
const navLinks = document.querySelectorAll('.navbar-nav .nav-link');
|
|
|
|
navLinks.forEach(link => {
|
|
if (link.getAttribute('href') === currentPath) {
|
|
link.classList.add('active');
|
|
}
|
|
});
|
|
|
|
// 初始化购物车数量
|
|
// TODO: 实现购物车数量获取
|
|
});
|
|
|
|
// 通用AJAX错误处理
|
|
function handleAjaxError(xhr) {
|
|
if (xhr.status === 401) {
|
|
alert('请先登录');
|
|
window.location.href = '/auth/login';
|
|
} else if (xhr.status === 403) {
|
|
alert('没有权限执行此操作');
|
|
} else {
|
|
alert('操作失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
// 通用成功提示
|
|
function showSuccessMessage(message) {
|
|
// 创建临时提示框
|
|
const alertDiv = document.createElement('div');
|
|
alertDiv.className = 'alert alert-success alert-dismissible fade show position-fixed success-toast';
|
|
alertDiv.innerHTML = `
|
|
${message}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
`;
|
|
document.body.appendChild(alertDiv);
|
|
|
|
// 3秒后自动消失
|
|
setTimeout(() => {
|
|
if (alertDiv.parentNode) {
|
|
alertDiv.parentNode.removeChild(alertDiv);
|
|
}
|
|
}, 3000);
|
|
}
|