Go to Top -- Google

Add a 'go to top' button for Google

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

  1. // ==UserScript==
  2. // @name Go to Top -- Google
  3. // @namespace feifeihang.info
  4. // @description Add a 'go to top' button for Google
  5. // @include https://www.google.*
  6. // @include http://www.google.*
  7. // @version 3
  8. // @grant none
  9. // ==/UserScript==
  10. (function (window, document, undefined) {
  11. // create the goto-top button.
  12. var btn = document.createElement('div');
  13. btn.id = 'goto-top-btn';
  14. btn.innerHTML = 'TOP';
  15. btn.onclick = gotoTop;
  16. // set CSS style.
  17. btn.style = 'display: none; color: #fff; position: fixed; bottom: 10px;' +
  18. 'right: 10px; line-height: 40px; text-align: center;' +
  19. 'width: 40px; height: 40px; background: #4285F4;' +
  20. 'cursor: pointer; font-weight: bolder; opacity: 0.8;';
  21. // append the go-to-top to search form to successfully attach to the UI.
  22. var form = document.querySelector('#searchform');
  23. form.appendChild(btn);
  24. window.onload = function () {
  25. var doc = document.documentElement;
  26. var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  27. if (top !== 0) {
  28. btn.style.display = 'block';
  29. }
  30. }
  31. // bind button hiden/show event.
  32. window.onscroll = function() {
  33. var doc = document.documentElement;
  34. var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  35. if (top === 0) {
  36. btn.style.display = 'none';
  37. } else {
  38. btn.style.display = 'block';
  39. }
  40. }
  41. function gotoTop() {
  42. goto(Math.floor(window.pageYOffset / 5));
  43. }
  44. function goto(step) {
  45. setTimeout(function () {
  46. window.scrollTo(0, window.pageYOffset - step);
  47. if (window.pageYOffset <= 0) return;
  48. goto(step);
  49. }, 100);
  50. }
  51. })(window, document, undefined);

QingJ © 2025

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