ScrollLocker

Lock and unlock page scrolling

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/589163/1889704/ScrollLocker.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         ScrollLocker
// @name:zh-CN   ScrollLocker
// @namespace    https://greasyfork.org/zh-CN/users/1570630
// @version      1.2
// @description  Lock and unlock page scrolling
// @description:zh-CN 锁定与恢复页面滚动
// @author       ryxel
// @license      MIT
// ==/UserScript==

var scrollLocker = (function() {
  'use strict';

  if (!document.__scrollLockerData) {
    document.__scrollLockerData = {
      lockCount: 0,
      savedStyle: null,
      isMobile: null
    };
  }

  const data = document.__scrollLockerData;

  const locker = {
    isLocked: false,

    lock() {
      if (locker.isLocked) return;
      locker.isLocked = true;

      if (++data.lockCount > 1) return;

      if (data.isMobile === null) data.isMobile = /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent);

      data.savedStyle = {
        bodyOverflow: document.body.style.overflow,
        bodyPaddingRight: document.body.style.paddingRight,
        htmlOverflow: document.documentElement.style.overflow
      };

      if (data.isMobile) {
        document.body.style.overflow = 'hidden';
        document.documentElement.style.overflow = 'hidden';
      } else {
        const oldWidth = document.body.clientWidth;
        const oldPaddingRight = parseFloat(window.getComputedStyle(document.body).paddingRight) || 0;

        document.body.style.overflow = 'hidden';
        document.documentElement.style.overflow = 'hidden';

        const diff = document.body.clientWidth - oldWidth;
        if (diff > 0) document.body.style.paddingRight = `${oldPaddingRight + diff}px`;
      }
    },

    unlock() {
      if (!locker.isLocked) return;
      locker.isLocked = false;

      if (data.lockCount <= 0) return;

      if (--data.lockCount > 0) return;

      document.body.style.overflow = data.savedStyle.bodyOverflow;
      document.body.style.paddingRight = data.savedStyle.bodyPaddingRight;
      document.documentElement.style.overflow = data.savedStyle.htmlOverflow;

      data.savedStyle = null;
    }
  };

  return locker;
})();