GitHub top bar enhancement

Fix and Auto Hide GitHub Top Bar w/ Ocototree caused headerbar displacement fix

当前为 2017-12-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         GitHub top bar enhancement
// @namespace    https://ccoooss.com/
// @version      0.1.11
// @description  Fix and Auto Hide GitHub Top Bar w/ Ocototree caused headerbar displacement fix
// @author       Yukino Song
// @match        https://github.com
// @match        https://github.com/*/*
// @match        https://gist.github.com/*
// @grant        none
// ==/UserScript==

const style = `
body {
	padding-top: 54px;
}
.Header {
	position: fixed;
	width: 100%;
	min-width: 1020px;
	top: 0;
	left: 0;
	z-index: 50;
	transform: translate3d(0, 0, 0);
	transition: transform .3s;
}

.Header.hidden {
	transform: translate3d(0, -54px, 0);
}

@media screen and (max-width: 1480px) {
    .octotree-show .Header {
        min-width: 1246px;
    	padding-left: 226px;
    }

    .octotree-show .Header .container-lg {
        margin-left: 0;
    }
}
`;

function getScrollHeight() {
    return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
}

(function() {
    'use strict';
    // Your code here...
    // Inject style
    var topBar = document.querySelector('.Header');
    if (!topBar) return;

    document.querySelector('head').insertAdjacentHTML('beforeend', `<style>${style}</style>`);

    var previousHeight = getScrollHeight();

    function updateScroll() {
        const prev = previousHeight;
        const height = getScrollHeight();
        previousHeight = height;

        // Always show on top
        if (height < 54) {
            topBar.classList.remove('hidden');
            return;
        }

        if (height > prev) topBar.classList.add('hidden');
        else topBar.classList.remove('hidden');
    }

    function smoothScrollTop() {
        // Use native smooth scrolling method (Chrome >= 61, Firefox >= 36)
        window.scroll({
            top: 0,
            behavior: 'smooth'
        });
    }

    window.addEventListener('scroll', updateScroll);
    topBar.addEventListener('dblclick', smoothScrollTop);
})();