zoukankan      html  css  js  c++  java
  • js实用方法记录-指不定哪天就会用到的js方法

    js实用方法记录-指不定哪天就会用到的js方法

    常用或者不常用都有

    判断是否在微信浏览器中

    测试代码:isWeiXin()==false

    /**
     * 是否在微信中
     */
    function isWeixin() {
    
      return (
        navigator.userAgent
          .toLowerCase()
          .indexOf('micromessenger') > -1
      )
    }
    

    全角转半角

    测试代码:wholetoHalf('hello'')=='hello'

    /**
     * 转换全角字符串
     * @param {string} txt 含全角字符串
     */
    function wholetoHalf(txt){
        if (!txt) {
            return txt;
        }
        var tmp = "";
        for (var i = 0; i < txt.length; i++) {
            if (txt.charCodeAt(i) > 65280 && txt.charCodeAt(i) < 65375) {
                tmp += String.fromCharCode(txt.charCodeAt(i) - 65248);
            }
            else if (txt.charCodeAt(i) == 12288) {
                tmp += String.fromCharCode(32);
            }
            else {
                tmp += String.fromCharCode(txt.charCodeAt(i));
            }
        }
        return tmp;
    }
    
    

    生成Guid

    
    /**
     * 生成Guid
     */
    function genGuid() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0, 3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
    }
    

    获取滚动条距顶部距离

    /**
     * 获取滚动条距顶部距离
     */
    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 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;
    }
    

    通过判断滚动条位置操作元素

    if(getScrollHeight() > document.documentElement.clientHeight 
        && getScrollTop()>getScrollHeight()/4){//有滚动条且滚动条距离顶部在四分之外
        //显示回到顶部浮层什么的~~
    }
    
  • 相关阅读:
    Visual Studio中的键盘快捷键自动添加所需的使用声明
    Workflow异常
    理解Javascript_03_javascript全局观
    jquery插件开发方法
    JS在IE和FireFox之间的区别汇总
    IE与FireFox的js和css (杂记)
    CSS:IE与Firefox的CSS兼容大全
    CSS网页设计解决方案(Hacks & Issues)
    什么是标准站点(W3C标准)
    12个针对网页设计师的非常便利的CSS框架、模板和摘录网站
  • 原文地址:https://www.cnblogs.com/morang/p/web-js-other-func.html
Copyright © 2011-2022 走看看