O'Reilly Helper

🎴 Enhance your O'Reilly learning experience: smoothly hide header and footer on scroll down, and show them (with cute transparency!) on scroll up.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        O'Reilly Helper
// @namespace   https://github.com/mefengl
// @version     0.2.0
// @description 🎴 Enhance your O'Reilly learning experience: smoothly hide header and footer on scroll down, and show them (with cute transparency!) on scroll up.
// @author      mefengl
// @match       https://learning.oreilly.com/library/view/*
// @icon        https://www.google.com/s2/favicons?sz=64&domain=oreilly.com
// @license     MIT
// @grant       GM_addStyle
// ==/UserScript==

(function () {
  'use strict';

  GM_addStyle(`
      header, #content-navigation {
          transition: opacity 0.5s linear;
      }
  `);

  let lastScrollTop = window.pageYOffset || document.documentElement.scrollTop;
  let isScrollingDown = false;

  let header = document.querySelector('header');
  let footer = document.querySelector('#content-navigation');

  if (header) {
    header.style.opacity = '0.6';
    header.style.transition = 'opacity 0.5s';
  }

  if (footer) {
    footer.style.opacity = '0';
    footer.style.transition = 'opacity 0.5s';
  }

  window.addEventListener('scroll', function () {
    let scrollTop = window.pageYOffset || document.documentElement.scrollTop;

    if (scrollTop > lastScrollTop && !isScrollingDown) {
      isScrollingDown = true;
      // On scroll down, hide header and footer
      if (header) header.style.opacity = '0';
      if (footer) footer.style.opacity = '0';
    } else if (scrollTop < lastScrollTop && isScrollingDown) {
      isScrollingDown = false;
      // On scroll up, show header and footer
      if (header) header.style.opacity = '0.6';
      if (footer) footer.style.opacity = '0.6';
    }

    lastScrollTop = scrollTop <= 0 ? 0 : scrollTop; // For mobile
  }, false);

})();