页面自动滚动脚本—autoScroll

这是我写的第一个脚本,实现起来很简单,有兴趣的话可以下载玩玩。添加链接描述
下面是它的源代码:



    // ==UserScript==
    // @name         autoScroll
    // @namespace    eyes
    // @version      0.2
    // @description  It allows the page to scroll on its own
    // @author       eyes
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
     
    (function() {
     
        'use strict';
        var speed = 0;
        // 获取滑动位置
        function getScrollTop() {
     
            var scrollTop = 0,
                bodyScrollTop = 0,
                documentScrollTop = 0;
            if (document.body) {
     
                bodyScrollTop = document.body.scrollTop;
            }
            if (document.documentElement) {
     
                documentScrollTop = document.documentElement.scrollTop;
            }
            scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
            return scrollTop;
        }

        //浏览器视口的高度
        function getWindowHeight() {
     
            var windowHeight = 0;
            if (document.compatMode == 'CSS1Compat') {
     
                windowHeight = document.documentElement.clientHeight;
            } else {
     
                windowHeight = document.body.clientHeight;
            }
            return windowHeight;
        }

        //文档的总高度
        function getScrollHeight() {
     
            var scrollHeight = 0,
                bodyScrollHeight = 0,
                documentScrollHeight = 0;
            if (document.body) {
     
                bodyScrollHeight = document.body.scrollHeight;
            }
            if (document.documentElement) {
     
                documentScrollHeight = document.documentElement.scrollHeight;
            }
            scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
            return scrollHeight;
        }

        // 滚动事件
        setInterval(() => {
     
            // 判断页面是否滑到底部
            let bottomFlag = (getScrollTop() + getWindowHeight() == getScrollHeight()) ? true : false;
            let topFlag = (getScrollTop() == 0) ? true : false;
            if (bottomFlag || topFlag) {
     
                speed = 0;
            } else {
     
                document.documentElement.scrollTop += speed;
            }
        }, 5)

        // 判断是否需要滚动
        document.onkeydown = (e) => {
     
            e = event || window.event;
            if (e && e.keyCode == 38 && e.altKey) {
      // 上键
                let bottomFlag = (getScrollTop() + getWindowHeight() == getScrollHeight()) ? true : false;
                if (bottomFlag) {
     
                    document.documentElement.scrollTop += -1;
                }
                speed -= 1.5;
            }
            if (e && e.keyCode == 40 && e.altKey) {
      // 下键
                let topFlag = (getScrollTop() == 0) ? true : false;
                if (topFlag) {
     
                    document.documentElement.scrollTop += 1;
                }
                speed += 1.5;
            }
        }

        // 单击页面停止滚动
        document.onclick = () => {
     
            speed = 0;
        }

        // 滑动滚轮页面停止滚动
        document.onmousewheel = () => {
     
            speed = 0;
        }
        document.addEventListener("DOMMouseScroll", () => {
     
            speed = 0;
        })
    })();


你可能感兴趣的:(脚本,javascript,es6)