Scroll to Bottom and Top Button

Add buttons to scroll to the bottom and top of the website

  1. // ==UserScript==
  2. // @name Scroll to Bottom and Top Button
  3. // @name:zh 底部和顶部按钮
  4. // @namespace https://gf.qytechs.cn/
  5. // @version 1.0
  6. // @description Add buttons to scroll to the bottom and top of the website
  7. // @description:zh
  8. // @author ghzxs
  9. // @match *://*/*
  10. // @license MIT
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. if (window !== window.top) {
  17. return;
  18. }
  19.  
  20. // Base64-encoded SVG data for bottom icon
  21. const base64BottomIcon = 'PHN2ZyBzdHJva2U9ImN1cnJlbnRDb2xvciIgZmlsbD0ibm9uZSIgc3Ryb2tlLXdpZHRoPSIyIiB2aWV3Qm94PSIwIDAgMjQgMjQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgY2xhc3M9Imljb24tc20gbS0xIiBoZWlnaHQ9IjFlbSIgd2lkdGg9IjFlbSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48bGluZSB4MT0iMTIiIHkxPSI1IiB4Mj0iMTIiIHkyPSIxOSI+PC9saW5lPjxwb2x5bGluZSBwb2ludHM9IjE5IDEyIDEyIDE5IDUgMTIiPjwvcG9seWxpbmU+PC9zdmc+';
  22.  
  23. // Base64-encoded SVG data for top icon
  24. const base64TopIcon = 'PHN2ZyBzdHJva2U9ImN1cnJlbnRDb2xvciIgZmlsbD0ibm9uZSIgc3Ryb2tlLXdpZHRoPSIyIiB2aWV3Qm94PSIwIDAgMjQgMjQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgY2xhc3M9Imljb24tc20gbS0xIiBoZWlnaHQ9IjFlbSIgd2lkdGg9IjFlbSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48bGluZSB4MT0iMTIiIHkxPSIxOSIgeDI9IjEyIiB5Mj0iNSI+PC9saW5lPjxwb2x5bGluZSBwb2ludHM9IjUgMTIgMTIgNSAxOSAxMiI+PC9wb2x5bGluZT48L3N2Zz4=';
  25.  
  26. // Common styles for both buttons
  27. const buttonStyles = {
  28. position: 'fixed',
  29. zIndex: '9999',
  30. backgroundColor: 'none',
  31. border: '0.5px solid transparent',
  32. borderRadius: '50%',
  33. padding: '4px',
  34. cursor: 'pointer',
  35. display: 'flex',
  36. alignItems: 'center',
  37. justifyContent: 'center'
  38. };
  39.  
  40. // Create the bottom button
  41. const bottomButton = document.createElement('button');
  42. const bottomImg = document.createElement('img');
  43. bottomImg.src = `data:image/svg+xml;base64,${base64BottomIcon}`;
  44. bottomImg.alt = 'Scroll to Bottom';
  45. bottomImg.style.width = '16px';
  46. bottomImg.style.height = '16px';
  47. bottomImg.style.display = 'block';
  48. bottomButton.appendChild(bottomImg);
  49.  
  50. // Apply styles to the bottom button
  51. Object.assign(bottomButton.style, buttonStyles);
  52. bottomButton.style.bottom = '14px';
  53. bottomButton.style.right = '14px';
  54.  
  55. // Add click event listener to scroll to the bottom
  56. bottomButton.addEventListener('click', function() {
  57. window.scrollTo({
  58. top: document.body.scrollHeight,
  59. behavior: 'smooth'
  60. });
  61. });
  62.  
  63. // Append the bottom button to the body
  64. document.body.appendChild(bottomButton);
  65.  
  66. // Create the top button
  67. const topButton = document.createElement('button');
  68. const topImg = document.createElement('img');
  69. topImg.src = `data:image/svg+xml;base64,${base64TopIcon}`;
  70. topImg.alt = 'Scroll to Top';
  71. topImg.style.width = '16px';
  72. topImg.style.height = '16px';
  73. topImg.style.display = 'block';
  74. topButton.appendChild(topImg);
  75.  
  76. // Apply styles to the top button
  77. Object.assign(topButton.style, buttonStyles);
  78. topButton.style.bottom = '50px';
  79. topButton.style.right = '14px';
  80.  
  81. // Add click event listener to scroll to the top
  82. topButton.addEventListener('click', function() {
  83. window.scrollTo({
  84. top: 0,
  85. behavior: 'smooth'
  86. });
  87. });
  88.  
  89. // Append the top button to the body
  90. document.body.appendChild(topButton);
  91.  
  92. // Function to toggle the visibility of the top button
  93. function toggleTopButton() {
  94. if (window.scrollY === 0) {
  95. topButton.style.display = 'none';
  96. } else {
  97. topButton.style.display = 'flex';
  98. }
  99. }
  100.  
  101. // Initial check
  102. toggleTopButton();
  103.  
  104. // Add scroll event listener to toggle the top button
  105. window.addEventListener('scroll', toggleTopButton);
  106. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址