scroll_to_top

scroll to top for PC and mobile

Versione datata 03/02/2022. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         scroll_to_top
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  scroll to top for PC and mobile
// @author       amormaid
// @run-at       document-end
// @match        http(s)?://*/*
// @include      http://*
// @include      https://*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const sttClassName = 'scroll-to-top';
    let timeoutID;

    function scroll_or_touchmove_handler() {
        clearTimeout(timeoutID);
        timeoutID = setTimeout(() => {
            const btn = (document.getElementsByClassName(sttClassName) || [])[0];
            if (btn) {
                btn.style.cssText = 'opacity: 1;';
                setTimeout(() => {
                    btn.style.cssText = 'opacity: .1;';
                }, 5000)
            }
            timeoutID = null;
        }, 500);
    }

    function addCSSStyle() {
        var cssStyle = document.createElement('style');
        cssStyle.type = 'text/css';
        cssStyle.textContent = [
            '.' + sttClassName + ' {',
            '	width: 8vw;',
            '	height: 8vw;',
            '	line-height: 8vw;',
            '	text-align: center;',
            '	background: whiteSmoke;',
            '	font-weight: bold;',
            '	font-size: 6vw;',
            '	color: #444;',
            '	text-decoration: none;',
            '	position: fixed;',
            '	bottom: 0;',
            '	left: 0;',
            '	display: block;',
            '	border: 1px solid grey;',
            '	border-top-right-radius: 4vw;',
            '	box-shadow: 0 0 3px grey;',
            '	transition: opacity 250ms ease-out;',
            '	opacity: .1;',
            '	z-index: 1000;',
            '	cursor: pointer;',
            '}'
        ].join('\n');
        if(document.head) {
            document.head.appendChild(cssStyle);
        }
    }

    (() => {
        if ('ontouchstart' in window) {
            window.ontouchmove = scroll_or_touchmove_handler;
        } else {
            window.onscroll = scroll_or_touchmove_handler;
            window.onwheel = function() {
                if(typeof timeoutID != 'number') {
                    return;
                }
                clearTimeout(timeoutID);
                timeoutID = null;
            };

        }
        addCSSStyle();
        var div = document.createElement('div');
        div.className = sttClassName;
        div.textContent = '⇧';
        div.onclick = () => window.scrollTo(0, 0);
        document.body.appendChild(div);


    })();

})();