Skip to content

Commit bc646d8

Browse files
committed
更新 [向下翻时自动隐藏顶栏] 适配知乎改动
1 parent e834db6 commit bc646d8

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

Zhihu-Beautification.user.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 知乎美化
3-
// @version 1.5.17
3+
// @version 1.5.18
44
// @author X.I.U
55
// @description 宽屏显示、暗黑模式(4种)、暗黑模式跟随浏览器、屏蔽首页活动广告、隐藏文章开头大图、调整图片最大高度、向下翻时自动隐藏顶栏
66
// @match *://www.zhihu.com/*
@@ -44,6 +44,8 @@
4444
}
4545
registerMenuCommand();
4646
addStyle();
47+
// 向下翻时自动隐藏顶栏
48+
if (menu_value('menu_hideTitle')) setTimeout(hideTitle, 2000);
4749

4850
// 注册脚本菜单
4951
function registerMenuCommand() {
@@ -253,9 +255,9 @@ html[data-theme=light] .AppHeader-notifications:not([aria-label=通知])>div:fir
253255
style_2 = `/* 隐藏在各列表中查看文章时开头显示的大图,不影响文章、专栏页面 */
254256
.RichContent img.ArticleItem-image {display: none !important;}
255257
`,
256-
style_3 = `/* 向下翻时自动隐藏顶栏*/
257-
header.is-hidden {display: none;}
258-
`,
258+
// style_3 = `/* 向下翻时自动隐藏顶栏*/
259+
//header.is-hidden {display: none;}
260+
//`,
259261
style_4 = `/* 调整图片最大高度 */
260262
.ztext .content_image, .ztext .origin_image, .GifPlayer img {max-height: 500px !important;width: auto !important;}
261263
`,
@@ -481,8 +483,6 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();}
481483
if (menu_value('menu_picHeight')) style += style_4;
482484
// 隐藏文章开头大图
483485
if (menu_value('menu_postimg')) style += style_2;
484-
// 向下翻时自动隐藏顶栏
485-
if (menu_value('menu_hideTitle')) style += style_3;
486486

487487
if (document.lastChild) {
488488
document.lastChild.appendChild(style_Add).textContent = style;
@@ -496,6 +496,27 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();}
496496
}
497497
}
498498

499+
function hideTitle() {
500+
// 获取需要控制的元素
501+
const floatingElement = document.getElementsByTagName('header')[0];
502+
let beforeScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
503+
scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop,
504+
scrollHeight = window.innerHeight || document.documentElement.clientHeight
505+
506+
window.addEventListener('scroll', function (e) {
507+
var afterScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
508+
delta = afterScrollTop - beforeScrollTop;
509+
//console.log(delta,floatingElement)
510+
if (delta == 0) return false;
511+
if (delta > 0) {// 向下滚动 隐藏
512+
floatingElement.hidden = true;
513+
} else {
514+
// 向上滚动 显示
515+
floatingElement.hidden = false;
516+
}
517+
beforeScrollTop = afterScrollTop;
518+
}, false);
519+
}
499520

500521
// 获取知乎 Cookie 中的主题类型
501522
function getTheme() {

0 commit comments

Comments
 (0)