2019前端笔记

  1. 采用局部滚动布局,不要给body和html添加overflow:hidden,因为android系统下,focus input框时,会出现键盘弹起遮住input框的bug。 此条有误,只有全局滚动才可以避免此类问题发生

  2. iOS下采用-webkit-overflow-scrolling开启局部弹性滚动布局,尽量不要在局部滚动内部采用:active伪类样式,当:active伪类和iOS特有的惯性滚动一起触发时,页面会闪烁

  3. 无法在iOS12微信浏览器内,通过historyAPI修改即将复制出去的链接,但安卓可以。

  4. iOS8 支持css3 animtion动画,但不支持animation回调

  5. android下监听键盘弹起收回事件,支持屏幕旋转

    let h = window.innerHeight;
    let orientation = window.orientation;
    if (os.isAndroid) {
        window.addEventListener('resize', () => {
            let height = window.innerHeight;
            if (orientation === window.orientation && height < h) {
                window.dispatchEvent(new CustomEvent('keyboardshow'));
                return;
            }
            if (orientation === window.orientation && height === h) {
                window.dispatchEvent(new CustomEvent('keyboardhide'));
                return;
            }
            if (orientation !== window.orientation) {
                h = height;
                orientation = window.orientation;
                window.dispatchEvent(new CustomEvent('keyboardhide'));
            }
        });
    }
    
  6. css针对iOS或andorid采用不同样式:

    /*android 4.44以下、iOS环境*/
    div{border:0.5px solid #fff}
    @supports not (-webkit-overflow-scrolling:touch){
          /*android 4.44以上的环境*/
         div{border:none;box-shadow:0 0 0 0.5px #fff}
    }
    

    supports支持程度为iOS 8+,android 4.44+,可根据需求灵活调整语法。

  7. 移动端可以使用vh和vw这两个单位,根据mdn文档,vh等于1%初始视口高度,vw为1%初始视口宽度,但在safari中,使用这个“初始视口高度”包含了顶部的地址栏和底部的导航栏。因此不能在safari中使用100vh当成全屏高度使用。

  8. 解决安卓line-height设置后文字不居中的办法(android 7.0+):

    • 第一步,给html设置语言,指定中文:<html lang="zh_cn">
    • 第二步,使用中文字体进行渲染:html{font-family:sans-serif}
  9. 一个数字转成对应的rgba颜色值:

    function n2rgba(n){
         if(i>0xffffffff)return [0xff,0xff,0xff,1];
         const str = ('00000000'+n.toString(16)).slice(-8);
         return [
             parseInt(`0x${str.slice(0,2)}`),
             parseInt(`0x${str.slice(2,4)}`),
             parseInt(`0x${str.slice(4,6)}`),
             parseInt(`0x${str.slice(6,8)}`)/0xff,
         ]
    }
    
    //采用位运算
    function n2rgba(n){
        return [
            (n & 0xff000000)>>>24,
            (n & 0xff0000)>>>16,
            (n & 0xff00)>>>8,
            (n & 0xff)/0xff    
        ]
    }
    
  10. 一个rgba颜色值转成数字(a最大值为255)

    function rgba2n(r,g,b,a){
        const toHex=(n)=>('00'+n.toString(16)).slice(-2)
        return parseInt(
            '0x' +toHex(r) + toHex(g)+ toHex(b) + toHex(a)
        );
    }
    
    //采用运算
    function rgba2n(r,g,b,a){
       return [r,g,b,a].reduce((n,item,i)=>{
           item = Math.abs(item);
           if(item>=0xff){
               item = 0xff
           }
           n += item*Math.pow(16,(3-i)*2)
           return n
       },0)
    }
    
  11. 在canvas中绘制iconfont图标:

    var ctx = canvas.getContext('2d');
    
    //可以随意设置颜色
    ctx.fillStyle='#f00'
    
    //iconfont对应导出的字体名称
    ctx.font='24px iconfont'
    
    //'\ue508'对应相应图标的字体编码
    ctx.fillText('\ue508',0,0);
    
  12. webpack4+less+MiniCssExtractPlugin打包后公共less代码提取不出来?试试使用@import (reference) '<你的公共less或者主题less文件>';代替@import '<你的公共less或者主题less文件>';

  13. 莫名其妙,ios safari里,视口外的css3 animation动画初始化时不执行,除非你给animation-delay加个延时,而且这个延时还不能太小,另外,通过使用setTimeout给他加延时的话,也是可以的

  14. chrome里面,想要获取输入光标的包围盒直接调用range.getBoundingClientRect()就可以了,而在safari ios里面,如果当前的光标rangecollapsed状态为true,则获取到的值都是0。但可以通过range.getClientRects()获取到光标包围盒:

    function getRangeBoundingClientRect(){
        let selection = window.getSelection();
        let defaultValue = {top:0,left:0/*...and so on*/}//默认返回       
        if(!selection.rangeCount){
            return defaultValue
        }
        let range = selection.getRangeAt(0);
        if(!range){
            return defaultValue
        }
        let rangeBcrList = range.getClientRects();
        if(!rangeBcrList.length){
            return defaultValue
        }
        return rangeBcrList[0]
    }
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容