//获取页面元素的位置functiongetElementTopLeft(obj){vartop=0;varleft=0;while(obj){top+=obj.offsetTop;left+=obj.offsetLeft;obj=obj.offsetParent;}return{top:top,left:left};}vartestImg=document.getElementById(testImg);vartestImgImp=0;window.onscroll=function(){varviewHeight=document.documentElement.clientHeight;//窗口可视区高度varscrollY=document.documentElement.scrollTopdocument.body.scrollTop;//滚动条距离顶部高度vartestImgH=testImg.offsetHeight;if(getElementTopLeft(testImg).top+testImgH/2
viewHeight+scrollY){//进入可视区testImg对象1/2高度以内if(testImgImp==0){//防止重复加载apImpression(监测代码testImgImp=1;}}}
推荐: