Scroll Top Button

Fast and lightweight scrolltop button

当前为 2017-12-19 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name Scroll Top Button
// @description Fast and lightweight scrolltop button
// @author NeoCortex
// @license MIT
// @version 0.1
// @include *://*
// @namespace https://greasyfork.org/users/12790
// ==/UserScript==
(function (window, undefined) {
    var w;
    if (typeof unsafeWindow !== undefined) {
        w = unsafeWindow;
    } else {
        w = window;
    }

    if (w.self != w.top) {
        return;
    }
	  var buttonMargins = '15px';
	  var buttonEdges = '4px';
	  var buttonSize = '40px';
	  var buttonOpacity = '0.3';
	  var buttonHoverOpacity = '0.7';
	  var fontSize = '35px';
	  var buttonColor = 'black';
	  var transitionDelay = '.3s';
	  var transitionType = 'ease-in-out';

	  var btnClass = Math.random().toString(36).replace(/[^a-z]+/g, '');
	  var styles = '.' + btnClass + '{';
	  styles += 'position: fixed;';
	  styles += 'cursor: pointer;';
	  styles += 'bottom: ' + buttonMargins + ';';
	  styles += 'right: ' + buttonMargins + ';';
	  styles += 'border-radius: ' + buttonEdges + ';';
	  styles += 'width: ' + buttonSize + ';';
	  styles += 'height: ' + buttonSize + ';';
	  styles += 'opacity: ' + buttonOpacity + ';';
	  styles +='z-index: 99;';
	  styles += 'transition: all ' + transitionDelay + ' ' + transitionType + ';';
	  styles += 'background: ' + buttonColor + ';}';
	  styles += '.' + btnClass + ':hover{';
	  styles += 'opacity: ' + buttonHoverOpacity + ';';
      styles += 'transform:scale(1.1);}';
	  styles += '.' + btnClass + '>span{';
	  styles += 'color: white;opacity: 1;font-size:' + fontSize + ';';
	  styles += 'width:100%;height:100%;margin:0 auto;display:block;';
	  styles += 'line-height:' + buttonSize + ';}';
	  styles += '.' + btnClass + '.' + btnClass + '_hidden{';
	  styles += 'opacity: 0;transition: all ' + transitionDelay +' ' + transitionType + '}';
	  var btn = document.createElement('div');
	  var style = document.createElement('style');
	  style.innerHTML = styles;
	  document.body.appendChild(style);
	  btn.className = btnClass + (w.scrollY === 0 ? ' ' + btnClass + '_hidden' : '');
	  btn.innerHTML = '<span>&#129145;</span>';
	  btn.addEventListener('click', function scrollToTop() {
	  var scrollDuration = 700;
	  const   scrollHeight = w.scrollY,
			  scrollStep = Math.PI / ( scrollDuration / 15 ),
			  cosParameter = scrollHeight / 2;
	  var     scrollCount = 0,
			  scrollMargin,
			  scrollInterval = setInterval( function() {
				  if ( w.scrollY !== 0 ) {
					  scrollCount = scrollCount + 1;
					  scrollMargin = cosParameter - cosParameter * Math.cos( scrollCount * scrollStep );
					  w.scrollTo( 0, ( scrollHeight - scrollMargin ) );
				  }
				  else clearInterval(scrollInterval);
			  }, 15 );
	  }, false);
	  document.addEventListener('scroll', function (event) {
		  var btn = document.querySelector('.' + btnClass);
		  if(w.scrollY === 0) {
			btn.className = btnClass + ' ' + btnClass + '_hidden';
			setTimeout(function(){
			  btn.style.right = '-9999px';
			}, 300);
		  } else {
			btn.style.right = buttonMargins;
			btn.className = btnClass;
		  }
	  });
    w.onload = function() {
        document.body.appendChild(btn);
    };
})(window);