23 lines
1.1 KiB
JavaScript
23 lines
1.1 KiB
JavaScript
const searchBox = document.getElementById('search-input'); // 使用不同的名称避免重复声明
|
||
const fullButton = document.getElementById('fullscreen-button');
|
||
|
||
// 监听滚动事件
|
||
window.addEventListener('scroll', function() {
|
||
const scrollTop = window.scrollY || document.documentElement.scrollTop; // 获取滚动的距离
|
||
const isMobile = window.innerWidth <= 767; // 判断是否为移动设备
|
||
|
||
if (scrollTop > 50) { // 当页面滚动超过50px时
|
||
searchBox.style.top = isMobile ? '14px' : '-50px'; // 将搜索框移出视口
|
||
fullscreenButton.style.top = '-50px'; // 让全屏按钮同步隐藏
|
||
} else {
|
||
searchBox.style.top = isMobile ? '72px' : '10px'; // 移动设备恢复到72px,桌面端恢复到10px
|
||
fullscreenButton.style.top = '0px'; // 让全屏按钮同步恢复
|
||
}
|
||
});
|
||
|
||
// 页面加载完成后滚动到顶部
|
||
window.addEventListener('load', function() {
|
||
setTimeout(function() {
|
||
window.scrollTo(0, 0); // 将页面滚动到顶部
|
||
}, 100); // 延迟 100ms 执行,确保页面已完全加载
|
||
}); |