Go to Top -- Google

Add a 'go to top' button for Google

Fra og med 04.09.2015. Se den nyeste version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Go to Top -- Google
// @namespace   feifeihang.info
// @description Add a 'go to top' button for Google
// @include     https://www.google.*
// @include     http://www.google.*
// @version     3
// @grant       none
// ==/UserScript==
(function (window, document, undefined) {
  // create the goto-top button.
  var btn = document.createElement('div');
  btn.id = 'goto-top-btn';
  btn.innerHTML = 'TOP';
  btn.onclick = gotoTop;
  
  // set CSS style.
  btn.style = 'display: none; color: #fff; position: fixed; bottom: 10px;' +
              'right: 10px; line-height: 40px; text-align: center;' +
              'width: 40px; height: 40px; background: #4285F4;' +
              'cursor: pointer; font-weight: bolder; opacity: 0.8;';
  
  // append the go-to-top to search form to successfully attach to the UI.
  var form = document.querySelector('#searchform');
  form.appendChild(btn);
  
  window.onload = function () {
    var doc = document.documentElement;
    var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
    if (top !== 0) {
      btn.style.display = 'block';
    }
  }
  
  // bind button hiden/show event.
  window.onscroll = function() {
    var doc = document.documentElement;
    var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
    if (top === 0) {
      btn.style.display = 'none';
    } else {
      btn.style.display = 'block';
    }
  }
  
  function gotoTop() {
    goto(Math.floor(window.pageYOffset / 5));
  }
  
  function goto(step) {
    setTimeout(function () {
      window.scrollTo(0, window.pageYOffset - step);
      if (window.pageYOffset <= 0) return;
      goto(step);
    }, 100);
 
  }
  
  
})(window, document, undefined);