日记

4/13/2020

# code-snippets

/**
 * 此文件为所有页面皆可公用的js
 */
function isPc() {
    let userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
        "SymbianOS", "Windows Phone",
        "iPad", "iPod"
    ];
    var flag = true;
    for (var i = 0; i < Agents.length; i++) {
        if (userAgentInfo.indexOf(Agents[i]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    // 防抖
    function debounce (fn, wait) {
        let timeout = null;
        return function () {
            if (timeout !== null) clearTimeout(timeout);
            timeout = setTimeout(fn, wait);
        }
    }


        // 防抖
    debounce = (fn, wait) => {
        let timeout = null;
        return function () {
            if (timeout !== null) clearTimeout(timeout);
            timeout = setTimeout(fn, wait);
        }
    }

    //获取滚动条当前的位置
    getScrollTop = () => {
        if (document.documentElement && document.documentElement.scrollTop) {
            return document.documentElement.scrollTop;
        } else if (document.body) {
            return document.body.scrollTop;
        }
    }
    //获取当前可视范围的高度
    getClientHeight = () => {
        if (document.body.clientHeight && document.documentElement.clientHeight) {
            return Math.min(document.body.clientHeight, document.documentElement.clientHeight);
        } else {
            return Math.max(document.body.clientHeight, document.documentElement.clientHeight);
        }
    }

    //获取文档完整的高度
    getScrollHeight = () => {
        return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
    }

    return this;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Last Updated: 10/23/2021, 4:31:30 PM