美化移动设备下界面的样式,并优化操作逻辑
This commit is contained in:
1 parent
a734192bfd
commit
4f607e63fb
8 files changed
+82
-43
No files matched your search
+3
-2
@@ -4,12 +4,13 @@ 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 = '-50px'; // 将搜索框移出视口
|
||||
searchBox.style.top = isMobile ? '14px' : '-50px'; // 将搜索框移出视口
|
||||
fullscreenButton.style.top = '-50px'; // 让全屏按钮同步隐藏
|
||||
} else {
|
||||
searchBox.style.top = '10px'; // 恢复到初始位置
|
||||
searchBox.style.top = isMobile ? '72px' : '10px'; // 移动设备恢复到72px,桌面端恢复到10px
|
||||
fullscreenButton.style.top = '0px'; // 让全屏按钮同步恢复
|
||||
}
|
||||
});
|
||||
|
||||
+19
-6
@@ -2,6 +2,9 @@
|
||||
const menuButton = document.querySelector('.toggle-menu');
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
|
||||
const menuButtons = document.querySelectorAll('.tag-btn');
|
||||
|
||||
|
||||
// 侧边栏切换功能
|
||||
menuButton.addEventListener('click', function(event) {
|
||||
event.stopPropagation(); // 阻止事件冒泡,避免触发文档点击事件
|
||||
@@ -9,14 +12,24 @@ menuButton.addEventListener('click', function(event) {
|
||||
toggleRotation(); // 调用旋转按钮函数
|
||||
});
|
||||
|
||||
menuButtons.forEach(button => {
|
||||
button.addEventListener('click', function(event) {
|
||||
event.stopPropagation(); // 阻止事件冒泡
|
||||
toggleSidebar(); // 关闭侧边栏
|
||||
toggleRotation(); // 旋转按钮
|
||||
});
|
||||
});
|
||||
|
||||
// 侧边栏元素和按钮
|
||||
function toggleSidebar() {
|
||||
sidebar.classList.toggle('open'); // 切换侧边栏的显示/隐藏
|
||||
if (sidebar.classList.contains('open')) {
|
||||
document.body.classList.add('no-scroll'); // 禁用滚动
|
||||
} else {
|
||||
document.body.classList.remove('no-scroll'); // 恢复滚动
|
||||
}
|
||||
if (window.innerWidth <= 768) { // 小屏幕显示菜单按钮
|
||||
sidebar.classList.toggle('open'); // 切换侧边栏的显示/隐藏
|
||||
if (sidebar.classList.contains('open')) {
|
||||
document.body.classList.add('no-scroll'); // 禁用滚动
|
||||
} else {
|
||||
document.body.classList.remove('no-scroll'); // 恢复滚动
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查侧边栏的状态,更新按钮显示/隐藏
|
||||
|
||||
Reference in new issue
Block a user