Scroll to Bottom and Top Button

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

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

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