前端小知识10点(2019.5.28)

一、火狐(firefox)的mouseenter问题算法

<MyIcon
      onMouseEnter={e => {
          this.mouseEnter(e,);
       }}
       onBlur={() => {}}
       onMouseLeave={e => {
          this.mouseOut(e,);
       }}
/>

onMouseEnter事件在火狐上会不断地触发mouseentermouseleave事件,因此须要先设置一个flag=false,在onMouseEnter时设为true,在onMouseLeave设为false,让不断触发的onMouseEnter事件只触发一次便可浏览器

this.state={
  flag:false
}

mouseEnter(){
  if(!this.state.flag){
    //...do something
    this.setState({
      flag:true,
    })
  }
}

mouseOut(){
  this.setState({
    flag:false,
  })
}

二、ESLint Unary operator '++' used
i++是不安全的,因此用i+=1安全

//bad
for(let i=0;i<a.length;i++)

//good
for(let i=0;i<a.length;i+=1)

三、兼容 ie11之 SVG 的transform旋转
从 0 度svg

//非IE能够这样写
svg.style('transform', `rotate(0deg)`)
//IE须要这么写
svg.attr('transform',`rotate(0,0 0)`)

转到 180 度wordpress

//非IE能够这样写
svg.style('transform', `rotate(180)`)
//IE须要这么写
svg.attr('transform',  `rotate(180,0 0)`)

详情请参考:https://www.zhangxinxu.com/wordpress/2015/10/understand-svg-transform/this

四、border-block-end
边界块结束url

border-block-end: 1px solid #d5d5d5;

第一次知道这个属性,好像是新边框属性,但兼容性不太好,IE11 不兼容,因此还得改回下面这样:spa

border-bottom: 1px solid #d5d5d5;

五、调整 svg 中<g>标签的位置
使用<g>标签自带的transform属性firefox

<g transform={'translate(0 30) scale(1.4)'}>
</g>

六、get请求中的参数有中文,ie11没法识别
使用encodeURI()方法来识别,也不影响其余浏览器code

encodeURI(
        url
    )

七、document.activeElement.tagName
返回文档中当前得到焦点的元素

console.log(document.activeElement.tagName)

(这个我之前记过,但发现工做中不多用到)

八、注意写法,在赋值的同时,判断条件

let a
 let b=1
 if ( ( a = b )!==2 ) {
   console.log(a,'a28') //1
 }

九、 网上常能见到的一段 JS 随机数生成算法以下,为何用 9301, 49297, 233280 这三个数字作基数?

function rnd( seed ){
    seed = ( seed * 9301 + 49297 ) % 233280; //为什么使用这三个数?
    return seed / ( 233280.0 );
};

function rand(number){
    today = new Date(); 
    seed = today.getTime();
    return Math.ceil( rnd( seed ) * number );
};

myNum=(rand(5));

简单说,是3点缘由:

(1)c与m互质
(2)a - 1能够被m的全部质因数整除
(3)若是m是4的倍数,a - 1也必须是4的倍数
以上三条被称为Hull-Dobell定理。
能够看到,a=9301, c = 49297, m = 233280这组参数,以上三条所有知足。

详情请参考:https://www.zhihu.com/question/22818104

十、浏览器类别判断 window.navigator.userAgent

console.log(window.navigator.userAgent,'userAgent67')

360安全浏览器:

const is360=window.navigator.userAgent.indexOf("WOW64")!==-1

Edge:

const isEdge = window.navigator.userAgent.indexOf('Edge') !== -1;

IE11:

const isMs = window.navigator.userAgent.indexOf('.NET') !== -1;

(完)

相关文章
相关标签/搜索