Files
youbai/src/components/layout/Header.vue

307 lines
10 KiB
Vue
Raw Normal View History

2025-06-09 16:51:58 +08:00
<template>
<header
class="fixed top-0 w-full z-50 transition-all duration-300"
:class="{
2025-06-10 12:47:00 +08:00
'bg-white/90 backdrop-blur-md shadow-sm py-3': isScrolled || isMenuOpen,
'bg-transparent py-5': !isScrolled && !isMenuOpen
}"
2025-06-09 16:51:58 +08:00
>
2025-06-10 12:47:00 +08:00
<!-- 关键修复移动端布局优化 -->
<div class="px-3 sm:px-4">
2025-06-09 16:51:58 +08:00
<div class="flex justify-between items-center">
2025-06-10 12:47:00 +08:00
<router-link to="/" class="flex items-center shrink-0">
<img src="@/assets/images/logo.png" alt="Logo" class="w-8 h-8 md:w-10 md:h-10" />
<span class="ml-2 text-base sm:text-xl md:text-2xl font-bold text-primary whitespace-nowrap">
{{ currentLang === 'zh' ? '酉佰' : 'YouBaiBai' }}
</span>
2025-06-09 16:51:58 +08:00
</router-link>
<nav class="hidden md:flex space-x-8">
<router-link
2025-06-10 12:47:00 +08:00
v-for="item in menuItems"
:key="item.path"
:to="item.path"
2025-06-09 16:51:58 +08:00
class="font-medium transition-colors hover:text-primary"
active-class="text-primary"
2025-06-10 12:47:00 +08:00
:exact="item.exact"
2025-06-09 16:51:58 +08:00
>
2025-06-10 12:47:00 +08:00
{{ item.name }}
</router-link>
2025-06-09 16:51:58 +08:00
</nav>
<div class="hidden md:flex items-center space-x-4">
2025-06-10 12:47:00 +08:00
<!-- 语言切换按钮 -->
2025-06-09 16:51:58 +08:00
<button
2025-06-10 12:47:00 +08:00
@click="toggleLanguage"
class="flex items-center justify-center w-10 h-10 rounded-full bg-gray-100 hover:bg-primary hover:text-white transition-colors"
:title="currentLang === 'zh' ? '切换为英文' : 'Switch to Chinese'"
>
{{ currentLang === 'zh' ? 'EN' : '中' }}
</button>
<button
class="bg-gradient-to-r from-blue-500 to-indigo-600 hover:from-blue-600 hover:to-indigo-700 text-white font-medium py-2 px-4 md:py-3 md:px-6 rounded-lg flex items-center justify-center transition-all duration-300 transform hover:-translate-y-1 shadow-lg hover:shadow-xl active:translate-y-0 active:shadow-md"
2025-06-09 22:19:09 +08:00
@click="openModal"
2025-06-09 16:51:58 +08:00
>
2025-06-10 12:47:00 +08:00
{{ currentLang === 'zh' ? '联系我们' : 'Contact' }}
2025-06-09 16:51:58 +08:00
</button>
</div>
2025-06-10 12:47:00 +08:00
<!-- 移动端菜单按钮 - 关键修复区域 -->
<div class="flex items-center space-x-2 md:hidden">
<!-- 移动端语言切换按钮 - 尺寸优化 -->
<button
@click="toggleLanguage"
class="shrink-0 flex items-center justify-center w-8 h-8 rounded-full bg-gray-100 hover:bg-primary hover:text-white transition-colors"
2025-06-09 16:51:58 +08:00
>
2025-06-10 12:47:00 +08:00
{{ currentLang === 'zh' ? 'EN' : '中' }}
</button>
<button @click="toggleMenu" class="shrink-0 text-gray-700">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M4 6h16M4 12h16m-7 6h7"
/>
</svg>
</button>
</div>
2025-06-09 16:51:58 +08:00
</div>
</div>
2025-06-10 12:47:00 +08:00
<!-- 移动端菜单 - 修复宽度问题 -->
2025-06-09 16:51:58 +08:00
<div
v-show="isMenuOpen"
2025-06-10 12:47:00 +08:00
class="w-full md:hidden fixed inset-x-0 z-50 bg-white/95 backdrop-blur-lg py-5 overflow-hidden"
:style="{ top: headerHeight + 'px', height: `calc(100vh - ${headerHeight}px)` }"
@click.self="closeMenu"
2025-06-09 16:51:58 +08:00
>
2025-06-10 12:47:00 +08:00
<div class="px-4 h-full flex flex-col">
<nav class="flex flex-col space-y-4 py-4 overflow-y-auto">
2025-06-09 16:51:58 +08:00
<router-link
2025-06-10 12:47:00 +08:00
v-for="item in menuItems"
:key="item.path"
:to="item.path"
2025-06-09 16:51:58 +08:00
class="font-medium text-lg py-2 transition-colors hover:text-primary"
active-class="text-primary"
@click="closeMenu"
>
2025-06-10 12:47:00 +08:00
{{ item.name }}
</router-link>
2025-06-09 16:51:58 +08:00
</nav>
2025-06-10 12:47:00 +08:00
<div class="pt-4 pb-8 mt-auto">
2025-06-09 16:51:58 +08:00
<button
class="w-full bg-primary hover:bg-blue-700 text-white font-semibold py-3 rounded-lg transition-colors"
2025-06-10 12:47:00 +08:00
@click="openModal(); closeMenu();"
2025-06-09 16:51:58 +08:00
>
2025-06-10 12:47:00 +08:00
{{ currentLang === 'zh' ? '联系我们' : 'Contact' }}
2025-06-09 16:51:58 +08:00
</button>
</div>
</div>
</div>
</header>
2025-06-10 12:47:00 +08:00
2025-06-09 22:19:09 +08:00
<!-- 模态框组件 -->
2025-06-10 12:47:00 +08:00
<!-- 模态框组件 - 移动端优化 -->
2025-06-09 22:19:09 +08:00
<transition name="modal-fade">
2025-06-10 12:47:00 +08:00
<div v-if="showModal" @click="closeModal" class="fixed inset-0 z-50 overflow-auto bg-black/50 flex items-start justify-center p-4 backdrop-blur-lg">
<div @click.stop class="bg-white rounded-2xl shadow-xl w-full max-w-md sm:max-w-2xl overflow-hidden transform transition-all sm:mt-20">
2025-06-09 22:19:09 +08:00
<!-- 模态框头部 -->
2025-06-10 12:47:00 +08:00
<div class="bg-gradient-to-r from-indigo-600 to-blue-700 p-4 sm:p-6 text-white">
2025-06-09 22:19:09 +08:00
<div class="flex justify-between items-center">
2025-06-10 12:47:00 +08:00
<h2 class="text-xl sm:text-2xl font-bold">{{ currentLang === 'zh' ? '留下您的联系方式' : 'Leave Your Contact' }}</h2>
2025-06-09 22:19:09 +08:00
<button @click="closeModal" class="text-white hover:text-gray-200">
2025-06-10 12:47:00 +08:00
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
2025-06-09 22:19:09 +08:00
</button>
</div>
</div>
2025-06-10 12:47:00 +08:00
<!-- 模态框内容 - 移动端优化 -->
<div class="p-4 sm:p-6 max-h-[70vh] overflow-y-auto bg-gradient-to-r from-indigo-600 to-blue-700">
<ContactForm :lang="currentLang" />
2025-06-09 22:19:09 +08:00
</div>
2025-06-10 12:47:00 +08:00
<!-- 模态框底部 - 移动端优化 -->
<div class="bg-gray-50 px-4 sm:px-6 py-4 flex flex-col sm:flex-row sm:flex-wrap justify-end gap-3">
2025-06-09 22:19:09 +08:00
<button
@click="closeModal"
2025-06-10 12:47:00 +08:00
class="w-full sm:w-auto px-5 py-2 text-gray-700 rounded-lg font-medium border border-gray-300 hover:bg-gray-100"
2025-06-09 22:19:09 +08:00
>
2025-06-10 12:47:00 +08:00
{{ currentLang === 'zh' ? '关闭' : 'Close' }}
2025-06-09 22:19:09 +08:00
</button>
<button
2025-06-10 12:47:00 +08:00
class="w-full sm:w-auto px-5 py-2 bg-indigo-600 text-white rounded-lg font-medium hover:bg-indigo-700"
2025-06-09 22:19:09 +08:00
>
2025-06-10 12:47:00 +08:00
{{ currentLang === 'zh' ? '立即申请' : 'Submit' }}
2025-06-09 22:19:09 +08:00
</button>
</div>
</div>
</div>
</transition>
2025-06-09 16:51:58 +08:00
</template>
2025-06-09 22:19:09 +08:00
<script setup>
2025-06-10 12:47:00 +08:00
import { ref, computed, onMounted, onBeforeUnmount } from 'vue';
2025-06-09 22:19:09 +08:00
import ContactForm from "@/components/ui/ContactForm.vue";
2025-06-10 12:47:00 +08:00
const isScrolled = ref(false);
const isMenuOpen = ref(false);
const showModal = ref(false);
const headerHeight = ref(0);
const currentLang = ref('zh'); // 默认中文
2025-06-09 22:19:09 +08:00
2025-06-10 12:47:00 +08:00
// 新增:视口和导航宽度变量
const viewportWidth = ref(0);
const navWidth = ref(0);
// 菜单项根据语言动态变化
const menuItems = computed(() => {
return [
{ path: '/', name: currentLang.value === 'zh' ? '首页' : 'Home', exact: true },
{ path: '/about', name: currentLang.value === 'zh' ? '公司介绍' : 'About' },
{ path: '/team', name: currentLang.value === 'zh' ? '团队介绍' : 'Team' },
{ path: '/features', name: currentLang.value === 'zh' ? '服务特色' : 'Features' },
{ path: '/careers', name: currentLang.value === 'zh' ? '加入我们' : 'Careers' }
];
});
// 新增:输出视口和导航宽度
const logDimensions = () => {
// 获取视口宽度
viewportWidth.value = window.innerWidth;
// 输出到控制台
console.log('视口宽度:', `${viewportWidth.value}px`);
console.log('----------------------');
// 获取导航元素宽度
const navElement = document.querySelector('header');
if (navElement) {
navWidth.value = navElement.offsetWidth;
2025-06-09 16:51:58 +08:00
}
2025-06-10 12:47:00 +08:00
console.log('导航宽度:', `${navWidth.value}px`);
console.log('----------------------');
// 修改header标签的宽度
navElement.style.width = `${navWidth.value}px`;
// 修改body的宽度
document.body.style.width = `${viewportWidth.value}px`;
};
const toggleLanguage = () => {
currentLang.value = currentLang.value === 'zh' ? 'en' : 'zh';
};
const openModal = () => {
showModal.value = true;
};
const closeModal = () => {
showModal.value = false;
};
2025-06-09 22:19:09 +08:00
const toggleMenu = () => {
2025-06-10 12:47:00 +08:00
isMenuOpen.value = !isMenuOpen.value;
if (isMenuOpen.value) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = '';
}
};
2025-06-09 22:19:09 +08:00
const closeMenu = () => {
2025-06-10 12:47:00 +08:00
isMenuOpen.value = false;
document.body.style.overflow = '';
};
const handleScroll = () => {
isScrolled.value = window.scrollY > 20;
};
// 计算头部高度用于移动菜单定位
const calculateHeaderHeight = () => {
const header = document.querySelector('header');
if (header) {
headerHeight.value = header.offsetHeight;
}
};
2025-06-09 22:19:09 +08:00
onMounted(() => {
2025-06-10 12:47:00 +08:00
window.addEventListener('scroll', handleScroll);
calculateHeaderHeight();
window.addEventListener('resize', calculateHeaderHeight);
// 新增:添加尺寸输出功能
logDimensions(); // 初始输出
window.addEventListener('resize', logDimensions); // 窗口大小变化时输出
});
2025-06-09 22:19:09 +08:00
onBeforeUnmount(() => {
2025-06-10 12:47:00 +08:00
window.removeEventListener('scroll', handleScroll);
window.removeEventListener('resize', calculateHeaderHeight);
// 新增:移除尺寸输出监听器
window.removeEventListener('resize', logDimensions);
document.body.style.overflow = ''; // 确保卸载时恢复滚动
});
2025-06-09 16:51:58 +08:00
</script>
<style scoped>
.router-link-exact-active {
position: relative;
}
.router-link-exact-active::after {
content: '';
position: absolute;
bottom: -3px;
left: 0;
width: 100%;
height: 2px;
background-color: #2563eb;
}
2025-06-10 12:47:00 +08:00
/* 模态框动画 */
.modal-fade-enter-active,
.modal-fade-leave-active {
transition: opacity 0.3s ease;
}
.modal-fade-enter-from,
.modal-fade-leave-to {
opacity: 0;
}
/* 移动端菜单宽度修复 */
@media (max-width: 767px) {
.container {
padding-left: 1rem;
padding-right: 1rem;
}
/* 确保移动菜单不会超出视口 */
.overflow-hidden {
overflow: hidden;
}
/* 防止路由链接换行 */
.router-link {
white-space: nowrap;
}
}
/* 增强模糊效果 */
.backdrop-blur-lg {
backdrop-filter: blur(12px);
}
</style>