JS 冷门知识

监听网络状态

navigator.onLine 方法

代码如下:

/* 监听网络状态 */
mounted(){
    let that = this;
    window.addEventListener('online',  that.update, true);
    window.addEventListener('offline', that.update, true);
},
/* 监听方法 */
update (){
    let that = this;
    that.onLineInfo = navigator.onLine ? "online" : "offline";
},

页面可编辑:contentEditable

可以让浏览器变成编辑器

代码如下:

/* 在浏览器地址栏输入 */
data:text/html,<html contenteditable>

还可以让你编辑当前页面的文字(增 / 删)

/* 在 console 控制台输入 */
document.body.contentEditable=true

页面中某个元素相对浏览器视窗上下左右的距离

使用 getBoundingClientRect() 方法 :用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。不包含文档卷起来的部分。该函数返回一个 object 对象,有8个属性:top,right,buttom,left,width,height,x,y

代码如下:

/* 监听头部固定 */
<div class="pride_tab_fixed" ref="pride_tab_fixed">	
    <div class="pride_tab" :class="titleFixed == true ? 'isFixed' :''">	
        //header
    </div>	
</div>	
export default {	
    data(){	
      return{	
        titleFixed: false	
      }	
    },	
    activated(){	
      this.titleFixed = false;	
      window.addEventListener('scroll', this.handleScroll);	
    },	
    methods: {	
      handleScroll: function () {	
        let offsetTop = this.$refs.pride_tab_fixed.getBoundingClientRect().top;	
        this.titleFixed = offsetTop < 0;	
      }	
    }	
  }

监听元素是否进入了设备的可视区域之内

使用 IntersectionObserver 可以用来监听元素是否进入了设备的可视区域之内,而不需要频繁的计算来做这个判断。

所以我们可以用这个特性来处理曝光埋点,而不是用 getBoundingClientRect().top 这种更加损耗性能的方式来处理。

IntersectionObserverFun: function() {	
    let self = this;	
    let ele = self.$refs.pride_tab_fixed;	
    if( !!IntersectionObserver ){	
        let observer = new IntersectionObserver(function(){	
            let offsetTop = ele.getBoundingClientRect().top;	
            self.titleFixed = offsetTop &lt; 0;	
        }, {	
            threshold: [1]	
        });	
        observer.observe(document.getElementsByClassName('title_box')[0]);	
    } else {	
        window.addEventListener('scroll', _.throttle(function(){	
            let offsetTop = ele.getBoundingClientRect().top;	
            self.titleFixed = offsetTop &lt; 0;	
        }, 50));	
    }	
}, 

喜欢博主的可以点赞关注一下

---------------------------------------------------------------   END   ------------------------------------------------------------------

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值