美文网首页
前端代码收集

前端代码收集

作者: 大房子里的小乌龟 | 来源:发表于2018-12-05 11:51 被阅读0次
    //禁用微信右上角分享功能js代码
    function onBridgeReady() {
      WeixinJSBridge.call('hideOptionMenu');
    }
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } else {
        onBridgeReady();
    }
    
    vue 禁用微信分享
    mounted(){
            this.wxShare();
    },
    methods: {
        wxShare(){
            let _this = this;
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady',
                    _this.onBridgeReady, false);
                } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady',
                    _this.onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady',
                    _this.onBridgeReady);
                }
            } else {
                _this.onBridgeReady();
            }
        },
        onBridgeReady:function(){
            WeixinJSBridge.call('hideOptionMenu');
        },
    }
    

    IOS 系统 中文参数 encode解码

    //IOS 不支持中文参数 Android 支持解码中文参数
    that.encriptStr = '?openId=' + that.openId + '&mobile=' + that.mobile + '&agentCode=' + that.agentCode + '&userName=' + encodeURI(that.userName);
    
    //移动端键盘弹起导致屏幕下方定位的按钮被顶起
    var win_h = $(window).height();
    window.addEventListener('resize', function () {
        if($(window).height() < win_h){
            $('.bottom_nav').hide();
        }else{
            $('.bottom_nav').show();
        }
    });
    
    //是否微信浏览器
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
      console.log('微信浏览器');
    } else {
      console.log('非微信浏览器');
    }
    
    采用正则表达式获取地址栏参数
    function getUrlName(name) {
        var url = window.location.search;
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var result = url.substr(1).match(reg);
        return result ? decodeURIComponent(result[2]) : null;
    }
     
    // 调用方法
    alert(getUrlName("参数名1"));
    alert(getUrlName("参数名2"));
    alert(getUrlName("参数名3"));
    
    <!-------- rem布局算法 设计稿750px -------->
    (function(doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function() {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                if(clientWidth >= 750) clientWidth = 750;
                docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
                /*docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';*/
            };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
    <!-------- rem布局算法 设计稿750px -------->
    
    /* 兼容华为原生浏览器flex布局 */
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    
    -webkit-box-pack: justify;
    -moz-justify-content: space-between;
    -webkit-justify-content: space-between;
    
    -webkit-box-align: center;
    -moz-align-items: center;
    -webkit-align-items: center;
    
    -webkit-box-direction: normal;
    -webkit-box-orient: horizontal;
    -moz-flex-direction: row;
    -webkit-flex-direction: row;
    
    -webkit-box-flex: 1;
    -ms-flex: 1;
    
    //css tips
    //CheckBox, radio 点击后面的文字 也可勾选
    <input type="radio" id="male" name="sex" checked="checked" /><label for="male">男</label>  
    给 label标签添加for属性,值为input的id值
    
    //伪类 设置placeholder样式
    #input-test::-webkit-input-placeholder{
            color: #ADD8E6;
    }
    
    //fixed定位超出父级宽度
    .header{
      position: fixed;
      top:0;
      width: 100%;
      left: 50%;
      transform: translateX(-50%);
      max-width: 750px;
      height: 2rem;
      background:#EB2926;
    }
    
    //超出部分变为省略号
    width: 300px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    
    //超出2行部分 变为省略号
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2; //这儿的数字代表的就是你所需要实现效果的第N行
    -webkit-box-orient: vertical;
    

    二维码 兼容小米华为 解决长摁不识别二维码bug 原因在于生成的img标签 没有src属性

    creatQrCode(){
        let _this = this 
        var qrcode = new QRCode(this.$refs.qrCodeUrl, {
            text: "https://"+window.location.host+"/#/recommend/register" + _this.encriptStr,
            width: 200,
            height: 200,
            colorDark: '#000000',
            colorLight: '#ffffff',
            correctLevel: QRCode.CorrectLevel.H
        });
        // qrcode.makeCode(url);
        var canvas=document.getElementsByTagName('canvas')[0];
        var img = convertCanvasToImage(canvas);
        document.getElementById('qrcode').append(img);// 添加DOM
        //从 canvas 提取图片 image
        function convertCanvasToImage(canvas) {
            //新建Image对象
            var image = new Image();
            // canvas.toDataURL 返回的是一串Base64编码的URL
            image.src = canvas.toDataURL("image/png");  
            return image; 
        }
    },
    

    相关文章

      网友评论

          本文标题:前端代码收集

          本文链接:https://www.haomeiwen.com/subject/bhpicqtx.html