滚动条

为网页添加滚动条。找不到作者。年久失修,修修补补再上岗,顺便改了一些不合理的地方。修复了一些问题,产生了新的问题

当前为 2023-04-04 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        滚动条
// @namespace  https://greasyfork.org/zh-CN/users/954189
// @version      1.1
// @description  为网页添加滚动条。找不到作者。年久失修,修修补补再上岗,顺便改了一些不合理的地方。修复了一些问题,产生了新的问题
// @author       ???
// @run-at       document-end
// @license MIT
// @match       *
// ==/UserScript==


function createScrollBar() {
  var isScrollBar,startY,endY,whereScroll,clearScrollBar;
  var clientHeight = document.documentElement.scrollHeight;
  var scrollTop = document.documentElement.scrollTop;
  isScrollBar = document.getElementById('theScrollBar');

  if (isScrollBar) {
    isScrollBar.parentNode.removeChild(isScrollBar);
  };

  var theScrollBar = document.createElement("div");
  theScrollBar.id = "theScrollBar";
  theScrollBar.addEventListener("touchstart",function (e) {
    e.stopPropagation();
    e.preventDefault();
    if ( clearScrollBar ) {
      clearTimeout( clearScrollBar );
    };
    return startY = e.changedTouches[0].clientY - parseFloat(this.style.top);
  },true);

  theScrollBar.addEventListener("touchmove",function (e) {
    e.stopPropagation();
    e.preventDefault();
    if ( clearScrollBar ) {
      clearTimeout( clearScrollBar );
    };
    endY = e.changedTouches[0].clientY;
    var theTop = endY - startY;
    this.style.top = theTop + "px";
    thePageTop = theTop / document.documentElement.clientHeight * document.documentElement.scrollHeight;
    window.scrollTo(0,thePageTop);
  },true);

  theScrollBar.addEventListener("touchend",function (e) {
    e.stopPropagation();
    e.preventDefault();
    clearScrollBar = setTimeout(function () {
      if ( whereScroll ) {
        clearInterval(whereScroll);
      };
      that.style.display = "none";
    },3000);
    return clearScrollBar;
  },true);

  theScrollBar.innerHTML = "▲<br>▼";
  theScrollBar.setAttribute("style","font-size:4vw ;width:8vw ;line-height:8vw ;text-align:center ;background-color:rgba(255,255,255,0.7) ;box-shadow:0px 1px 5px rgba(0,0,0,0.2) ;color:#000 ;position:fixed ;top:" + scrollTop + "px;right:1vw ;z-index:999999 ;transform: translateZ(0);border-radius:1vw ");
  theScrollBar.style.display = 'none'
  document.body.appendChild(theScrollBar);

  document.ontouchmove = function () {
    if ( clearScrollBar ) {
      clearTimeout( clearScrollBar );
    };
    var scrollBar = document.getElementById('theScrollBar');
    scrollBar.style.display = "block";
    if ( !whereScroll ) {
      whereScroll = requestAnimationFrame(updateScrollBarPosition);
    };
  };



  document.ontouchstart = function () {
   if ( whereScroll ) {
     cancelAnimationFrame(whereScroll);
   };
   whereScroll = requestAnimationFrame(updateScrollBarPosition);
  };

  document.ontouchend = function () {
   var scrollBar = document.getElementById('theScrollBar');
   clearScrollBar = setTimeout(function () {
     scrollBar.style.display = "none";
     if ( whereScroll ) {
       cancelAnimationFrame(whereScroll);
     };
   },3000);
   return clearScrollBar;
  };
  
  
  
  function updateScrollBarPosition() {
    var scrollBar = document.getElementById('theScrollBar');
    var nowScrollTop = document.documentElement.scrollTop;
    var scrollBarHeight = scrollBar.offsetHeight;
var maxTop = document.documentElement.clientHeight - scrollBarHeight;
var theTop = ( nowScrollTop / (document.documentElement.scrollHeight - document.documentElement.clientHeight) ) * maxTop;
    scrollBar.style.top = theTop + "px";
    whereScroll = requestAnimationFrame(updateScrollBarPosition);
  }
  
};
createScrollBar();