滚动到顶/底

在网页右下添加一个的按钮,通过上下滑动可以切换按钮形态,快速回到页面顶部或底部。

  1. // ==UserScript==
  2. // @name 滚动到顶/底
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.2.2
  5. // @description 在网页右下添加一个的按钮,通过上下滑动可以切换按钮形态,快速回到页面顶部或底部。
  6. // @author techwb
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10.  
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 创建按钮元素
  17. var scrollButton = document.createElement('button');
  18. scrollButton.textContent = '▲';
  19. scrollButton.id = 'scroll-top-button';
  20.  
  21. // 添加按钮样式
  22. scrollButton.style.border = 'none'; // 去掉边框
  23. scrollButton.style.color = 'black'; // 文字颜色为黑色
  24. scrollButton.style.fontFamily = 'Arial'; // 字体
  25. scrollButton.style.fontWeigh = 'normal'; // 设定按钮的内边距
  26. scrollButton.style.width = '35px'; // 设定按钮宽度
  27. scrollButton.style.height = '35px'; // 设定按钮高度
  28. scrollButton.style.fontSize = '13px'; // 设定字体大小
  29. scrollButton.style.textAlign = 'center'; // 文字水平居中
  30. scrollButton.style.verticalAlign = 'center'; // 文字垂直居中
  31. scrollButton.style.textDecoration = 'none'; // 去掉下划线
  32. scrollButton.style.padding = '0'; // 去掉内边距
  33. scrollButton.style.margin = '0'; // 去掉外边距
  34. scrollButton.style.display = 'none'; // 默认不显示
  35. scrollButton.style.border = '0'; // 去掉边框
  36. scrollButton.style.borderRadius = '10px'; // 设定圆角
  37. scrollButton.style.boxShadow = '2px 2px 3px rgba(0, 0, 0, 0.3)'; // 添加阴影效果
  38. scrollButton.style.position = 'fixed'; // 设定固定定位
  39. scrollButton.style.bottom = '15%'; // 设定距离底部的距离
  40. scrollButton.style.cursor = 'none';
  41. scrollButton.style.right = '15px'; // 设定距离右侧的距离
  42. scrollButton.style.zIndex = '999999'; // 设定 z-index
  43.  
  44. // 添加鼠标悬停效果
  45. scrollButton.addEventListener('mouseenter', function() {
  46. scrollButton.style.backgroundColor = 'hsla(221, 41%, 98%, 0.8)'; // 鼠标悬停时的背景颜色
  47. });
  48.  
  49. scrollButton.addEventListener('mouseleave', function() {
  50. if (scrollButton.textContent === '▲') {
  51. scrollButton.style.backgroundColor = 'hsla(221, 41%, 98%, 0.8)'; // 鼠标离开时的背景颜色
  52. } else {
  53. scrollButton.style.backgroundColor = 'hsla(221, 41%, 98%, 0.8)'; // 鼠标离开时的背景颜色
  54. }
  55. });
  56.  
  57. // 添加按钮到页面
  58. document.body.appendChild(scrollButton);
  59.  
  60. // 当用户滚动页面时,如果已经滚动了一定距离,就显示按钮
  61. var lastScrollPosition = window.pageYOffset; // 上一次滚动的位置
  62. var hideButtonTimeout;
  63.  
  64. window.addEventListener('scroll', function() {
  65. var currentScrollPosition = window.pageYOffset;
  66. if (currentScrollPosition > lastScrollPosition) { // 向下滚动
  67. scrollButton.textContent = '▼';
  68. scrollButton.style.background = 'hsla(221, 41%, 98%, 0.8)';
  69. } else { // 向上滚动
  70. scrollButton.textContent = '▲';
  71. scrollButton.style.background = 'hsla(221, 41%, 98%, 0.8)';
  72. }
  73. lastScrollPosition = currentScrollPosition;
  74. if (window.pageYOffset > 100) { // 滚动距离超过 100px 时
  75. scrollButton.style.display = 'block'; // 显示按钮
  76. } else {
  77. scrollButton.style.display = 'none'; // 否则隐藏按钮
  78. }
  79.  
  80. // 清除之前的计时器
  81. clearTimeout(hideButtonTimeout);
  82. // 设置新的计时器,在 2000 毫秒(2 秒)后隐藏按钮
  83. hideButtonTimeout = setTimeout(function() {
  84. scrollButton.style.display = 'none';
  85. }, 2000);
  86. });
  87.  
  88. // 点击按钮时,回到页面顶部或底部
  89. scrollButton.addEventListener('click', function() {
  90. if (scrollButton.textContent === '▲') {
  91. window.scrollTo(0, 0); // 将页面滚动到顶部
  92. } else {
  93. window.scrollTo(0, document.documentElement.scrollHeight - window.innerHeight); // 将页面滚动到底部
  94. }
  95. });
  96. })();

QingJ © 2025

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