更改所有页面中英文切换样式及JS功能代码 所有页面logo图片不切换
This commit is contained in:
@@ -61,60 +61,158 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
};
|
||||
|
||||
let currentLang = localStorage.getItem('siteLang') || "zh";
|
||||
|
||||
// 语言显示更新函数
|
||||
function updateLanguageDisplay(lang) {
|
||||
const currentFlag = document.getElementById('current-flag');
|
||||
const currentLangText = document.getElementById('current-lang');
|
||||
|
||||
if (currentFlag && currentLangText) {
|
||||
if (lang === 'en') {
|
||||
currentFlag.src = '../imgs/flags/en.svg';
|
||||
currentLangText.textContent = 'English';
|
||||
} else {
|
||||
currentFlag.src = '../imgs/flags/zh.svg';
|
||||
currentLangText.textContent = '简体中文';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyLang(lang) {
|
||||
const data = langData[lang];
|
||||
|
||||
// logo
|
||||
document.getElementById("footerLogo").src = data.logo;
|
||||
document.querySelector('.footerBoxRightTitle').textContent = data.title;
|
||||
// logo
|
||||
// const footerLogo = document.getElementById("footerLogo");
|
||||
// if (footerLogo) footerLogo.src = data.logo;
|
||||
|
||||
const footerBoxRightTitle = document.querySelector('.footerBoxRightTitle');
|
||||
if (footerBoxRightTitle) footerBoxRightTitle.textContent = data.title;
|
||||
|
||||
// 页面标题
|
||||
document.title = data.title;
|
||||
|
||||
// 顶部标题和描述
|
||||
document.querySelector('.topBannerLeftTitle p:nth-child(1)').textContent = data.topBannerTitle;
|
||||
document.querySelector('.topBannerLeftTitle p:nth-child(2)').textContent = data.topBannerDesc;
|
||||
const topBannerTitle = document.querySelector('.topBannerLeftTitle p:nth-child(1)');
|
||||
if (topBannerTitle) topBannerTitle.textContent = data.topBannerTitle;
|
||||
|
||||
const topBannerDesc = document.querySelector('.topBannerLeftTitle p:nth-child(2)');
|
||||
if (topBannerDesc) topBannerDesc.textContent = data.topBannerDesc;
|
||||
|
||||
// 三大服务
|
||||
const serviceBoxes = document.querySelectorAll('.bannerBox > .bannerLeft, .bannerBox > .bannerRight');
|
||||
data.service.forEach((item, idx) => {
|
||||
if (serviceBoxes[idx]) {
|
||||
serviceBoxes[idx].querySelector('.title').textContent = item.title;
|
||||
serviceBoxes[idx].querySelector('.problem').textContent = item.desc;
|
||||
serviceBoxes[idx].querySelector('.report').childNodes[0].nodeValue = item.btn + " ";
|
||||
const titleElement = serviceBoxes[idx].querySelector('.title');
|
||||
if (titleElement) titleElement.textContent = item.title;
|
||||
|
||||
const descElement = serviceBoxes[idx].querySelector('.problem');
|
||||
if (descElement) descElement.textContent = item.desc;
|
||||
|
||||
const reportElement = serviceBoxes[idx].querySelector('.report');
|
||||
if (reportElement && reportElement.childNodes[0]) {
|
||||
reportElement.childNodes[0].nodeValue = item.btn + " ";
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 邮箱区
|
||||
document.querySelector(".emailLeftTitle").textContent = data.emailLeftTitle;
|
||||
document.querySelector(".emailLeftText").textContent = data.emailLeftText;
|
||||
const emailLeftTitle = document.querySelector(".emailLeftTitle");
|
||||
if (emailLeftTitle) emailLeftTitle.textContent = data.emailLeftTitle;
|
||||
|
||||
const emailLeftText = document.querySelector(".emailLeftText");
|
||||
if (emailLeftText) emailLeftText.textContent = data.emailLeftText;
|
||||
|
||||
// 按钮文本
|
||||
document.getElementById("lang-toggle").textContent = data.btn;
|
||||
// 背景图
|
||||
document.querySelector('.topBanner').style.backgroundImage = `url(${data.topBannerBG})`;
|
||||
// 英文样式调整(如有需要可扩展)
|
||||
const topBanner = document.querySelector('.topBanner');
|
||||
if (topBanner) topBanner.style.backgroundImage = `url(${data.topBannerBG})`;
|
||||
|
||||
// 英文样式调整
|
||||
if (lang === 'en') {
|
||||
document.querySelector('.topBannerLeftTitle p:nth-child(1)').classList.add('en');
|
||||
document.querySelector('.topBannerLeftTitle p:nth-child(2)').classList.add('en');
|
||||
if (topBannerTitle) topBannerTitle.classList.add('en');
|
||||
if (topBannerDesc) topBannerDesc.classList.add('en');
|
||||
} else {
|
||||
document.querySelector('.topBannerLeftTitle p:nth-child(1)').classList.remove('en');
|
||||
document.querySelector('.topBannerLeftTitle p:nth-child(2)').classList.remove('en');
|
||||
if (topBannerTitle) topBannerTitle.classList.remove('en');
|
||||
if (topBannerDesc) topBannerDesc.classList.remove('en');
|
||||
}
|
||||
|
||||
// 更新语言显示
|
||||
updateLanguageDisplay(lang);
|
||||
}
|
||||
|
||||
function handleLangToggle() {
|
||||
currentLang = currentLang === "zh" ? "en" : "zh";
|
||||
|
||||
// 修正的handleLangToggle函数,正确接收lang参数
|
||||
function handleLangToggle(lang) {
|
||||
currentLang = lang;
|
||||
localStorage.setItem('siteLang', currentLang);
|
||||
applyLang(currentLang);
|
||||
updateLanguageDisplay(currentLang);
|
||||
}
|
||||
|
||||
|
||||
// 初始化
|
||||
applyLang(currentLang);
|
||||
updateLanguageDisplay(currentLang);
|
||||
|
||||
// 事件绑定
|
||||
document.getElementById("lang-toggle").addEventListener("click", handleLangToggle);
|
||||
document.getElementById("lang-toggle").addEventListener("keydown", e => {
|
||||
if (e.key === "Enter" || e.key === " ") handleLangToggle();
|
||||
// 获取语言切换相关的DOM元素
|
||||
const langBtn = document.getElementById('lang-btn');
|
||||
const langDropdown = document.getElementById('lang-dropdown');
|
||||
const langItems = document.querySelectorAll('.lang-item');
|
||||
|
||||
// 切换下拉菜单显示/隐藏
|
||||
if (langBtn) {
|
||||
langBtn.addEventListener('click', function(e) {
|
||||
console.log(`点击切换语言`);
|
||||
e.stopPropagation(); // 阻止事件冒泡
|
||||
|
||||
// 直接修改样式,而不是依赖 CSS 类
|
||||
if (langDropdown.style.display === 'block') {
|
||||
langDropdown.style.display = 'none';
|
||||
} else {
|
||||
langDropdown.style.display = 'block';
|
||||
// 确保绝对定位和 z-index 正确设置
|
||||
langDropdown.style.position = 'absolute';
|
||||
langDropdown.style.top = '100%';
|
||||
langDropdown.style.right = '0';
|
||||
langDropdown.style.zIndex = '1000';
|
||||
}
|
||||
|
||||
// 旋转箭头
|
||||
const arrow = langBtn.querySelector('.arrow');
|
||||
if (arrow) {
|
||||
arrow.style.transform = langDropdown.style.display === 'block' ? 'rotate(180deg)' : 'rotate(0)';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 点击其他区域关闭下拉菜单
|
||||
document.addEventListener('click', function(event) {
|
||||
if (!event.target.closest('.lang-dropdown') && langDropdown) {
|
||||
langDropdown.style.display = 'none';
|
||||
const arrow = langBtn?.querySelector('.arrow');
|
||||
if (arrow) {
|
||||
arrow.style.transform = 'rotate(0)';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 语言选择事件
|
||||
if (langItems) {
|
||||
langItems.forEach(item => {
|
||||
item.addEventListener('click', function() {
|
||||
const lang = this.getAttribute('data-lang');
|
||||
if (lang !== currentLang) {
|
||||
handleLangToggle(lang);
|
||||
}
|
||||
if (langDropdown) langDropdown.style.display = 'none';
|
||||
const arrow = langBtn?.querySelector('.arrow');
|
||||
if (arrow) {
|
||||
arrow.style.transform = 'rotate(0)';
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 注释掉旧的事件绑定代码
|
||||
// document.getElementById("lang-toggle").addEventListener("click", handleLangToggle);
|
||||
// document.getElementById("lang-toggle").addEventListener("keydown", e => {
|
||||
// if (e.key === "Enter" || e.key === " ") handleLangToggle();
|
||||
// });
|
||||
});
|
||||
Reference in New Issue
Block a user