Smart Header for Zhihu

根据页面滚动,自动隐藏及显示知乎顶栏。

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        Smart Header for Zhihu
// @namespace   org.sorz.lab.zhihu
// @include     http://www.zhihu.com/*
// @version     0.2
// @grant       none
// @description 根据页面滚动,自动隐藏及显示知乎顶栏。
// ==/UserScript==

$(document).ready(function() {
  var $header = $('.zu-top');
  var headerHeight = $header.outerHeight() + 3;
  
  var refPos = $(document).scrollTop();
  var headerPos = 0;
  
  function isHidden() {
    return headerPos <= -headerHeight;
  }
  
  function isShown() {
    return headerPos >= 0;
  }
  
  $(window).scroll(function() {
    var scroll = $(document).scrollTop() - refPos;
    if (Math.abs(scroll) > headerHeight) {
      var oldHeaderPos = headerPos;
      headerPos = scroll > 0 ? -headerHeight : 0;
      if (headerPos != oldHeaderPos)
        $header.animate({top: headerPos + 'px'}, 200);
      refPos = $(document).scrollTop();
      return;
    }
    
    if ((scroll > 0 && !isHidden()) || (scroll < 0 && !isShown())) {
      headerPos = scroll > 0 ? -scroll : -headerHeight - scroll;
      $header.css('top', headerPos + 'px');
    } else {
      refPos = $(document).scrollTop();
    }
  });
});