埋点之眼球曝光

埋点曝光需求要求在页面在用户的视野中就发送一个曝光日志,一个页面的一个模块最多发送一次,也就是首次曝光才发送,之后在进入视野中就不用在发曝光日志了

一、当看到这个需求的时候我们第一点肯定想到要监听滚动事件

window.addEventListener('scroll', function(){
    console.log('监听滚动')
});

二、然后优化下滚动事件,加上throttle。

window.addEventListener('scroll',newScroll(exposure,500));
const newScroll = function(realFunc,time){
         let timeout = null;
         return function (){
              clearTimeout(timeout);
                   timeout = setTimeout(realFunc,time)
         }
 };
function exposure() {
                console.log('监听滚动增加throttle')
}

三、然后优化下滚动事件,加上throttle。
注意scroll监听的匿名事件是不可以取消监听事件的,我想要的是页面中所有需要曝光的地方曝光结束后,取消监听事件

//需要曝光的点
const exposureList = [
                {
                    selector: 'aaaa',
                    aid: '6790'
                },
            ];
window.addEventListener('scroll',newScroll);
const newScroll = function(realFunc,time){
         let timeout = null;
         return function (){
              clearTimeout(timeout);
                   timeout = setTimeout(realFunc,time)
         }
 }(exposure,500);
function exposure() {
   if(exposureList.length == 0){
         window.removeEventListener("scroll", newScroll);
  }
 exposureList.forEach(function(value,index) {
                    const currentDom = document.getElementById(value.selector);
                    if(currentDom){
                        const currentClientRect = currentDom.getBoundingClientRect();
                        const bottomToTopView = currentClientRect.top >0 && window.innerHeight/3*2>currentClientRect.top;
                        const topToBottomView  = currentClientRect.top <0 && currentClientRect.top > - currentDom.offsetHeight/2;
                        if(bottomToTopView ||topToBottomView){
                            //发送曝光事件
                            exposureList.splice(index,1);
                        }
                    }
}

tips: 这里用到了getBoundingClientRect文章出处

getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。getBoundingClientRect是DOM元素到浏览器可视范围的距离(不包含文档卷起的部分)。

该函数返回一个Object对象,该对象有6个属性:top,lef,right,bottom,width,height;

1.兼容性
getBoundingClientRect()最先是IE的私有属性,现在已经是一个W3C标准。所以你不用当心浏览器兼容问题,不过还是有区别的:IE只返回top,lef,right,bottom四个值,不过可以通过以下方法来获取width,height的值

document.getElementById(value.selector).offsetHeight();
document.getElementById(value.selector). clientHeight();
var ro = object.getBoundingClientRect();
var Width = ro.right - ro.left;
var Height = ro.bottom - ro.top;

//兼容所有浏览器写法:

var ro = object.getBoundingClientRect();
var Top = ro.top;
var Bottom = ro.bottom;
var Left = ro.left;
var Right = ro.right;
var Width = ro.width||Right - Left;
var Height = ro.height||Bottom - Top;
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

相关阅读更多精彩内容

  • 滑向未来(现代 JavaScript 与 CSS 滚动实现指南) 原文链接: evilmartians.com译文...
    DiligentLeo阅读 1,276评论 0赞 4
  • 前端开发面试题 面试题目: 根据你的等级和职位的变化,入门级到专家级,广度和深度都会有所增加。 题目类型: 理论知...
    怡宝丶阅读 2,727评论 0赞 7
  • 一:在制作一个Web应用或Web站点的过程中,你是如何考虑他的UI、安全性、高性能、SEO、可维护性以及技术因素的...
    Arno_z阅读 1,435评论 0赞 1
  • 本节介绍各种常见的浏览器事件。 鼠标事件 鼠标事件指与鼠标相关的事件,主要有以下一些。 click 事件,dblc...
    许先生__阅读 2,981评论 0赞 4
  • 乒乓球亚洲杯刚刚开打,就爆出了惊天大冷门,排名第二的樊振东在首轮比赛中就输给了排名和自己差了一大截的陈建安,冷门还...
    百合花白阅读 179评论 0赞 2

友情链接更多精彩内容