Smart Header for Zhihu

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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();
    }
  });
});