简体   繁体   English

如何测量 Div 上的查看时间?

[英]How Can I measure view Time on a Div?

I am looking for to calculate how much time a visitor read my post using JavaScript.我正在寻找计算访问者使用 JavaScript 阅读我的帖子的时间。 Like I have就像我有

3 div. 3 格each div contains information when visitor read any div I will calculate the time.每个 div 都包含访问者读取任何 div 时的信息,我将计算时间。 it will not work when somebody scroll down not staying on the div.当有人向下滚动而不停留在 div 上时,它将不起作用。 User must stay on the div.用户必须留在 div 上。 Each particular div will generate its own reading time.每个特定的 div 都会产生自己的阅读时间。 Actually I did not implement any code.其实我没有实现任何代码。 Looking for a better solution.寻找更好的解决方案。 Thank you谢谢

You can use some web analytics services like google analytics or hotjar that provide a good services.您可以使用一些 web 分析服务,如提供良好服务的google analyticshotjar

If you want to do something custom that match exactly you use case如果你想做一些完全匹配你用例的自定义

I'd suggest using IntersectionObserver API我建议使用IntersectionObserver API

const onIntersection = (entries) => {
  for (const entry of entries) {
    if (entry.isIntersecting) {
      console.log(entry);
    }
  }
};

const observer = new IntersectionObserver(onIntersection);
observer.observe(document.querySelector('#div1'));

This will tell you when the viewport is on the selected element #div1 in this case (When the user is seeing the div).在这种情况下,这将告诉您视口何时位于所选元素#div1上(当用户看到 div 时)。 So you can run a timer and check how long the user has been viewing the element.因此,您可以运行计时器并检查用户查看元素的时间。

Useful links: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API/Timing_element_visibility https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API有用的链接: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API/Timing_element_visibility https://developer.mozilla.org/en-US/docs/Web/API/Intersection_API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM