I have a blog post page, which have a component named PostContent that contains the text of the post. What I want to achieve is to dynamically add some content among the paragraphs of this PostContent depending on its length. I will do that by introducing the mentioned content with a distance of 1.5x viewport's height.
The problem I have is I cannot calculate the distance. I am using https://www.npmjs.com/package/html-react-parser but I cannot access to the offsetHeight property of the DOM Elements with that, which I want to know at what distance the element is from the top of the container element.
Is there a better/alternative way to achieve what I want?
Thanks!
Let's assume the viewport height is 100px and the post content 2000px, so I want to add an element every 200px (2x viewport's height).
<article>
<p>This is the first example paragraph of 10px</p>
<p>This is the second example paragraph of 10px</p>
// ...
<p>This is the twentieth example paragraph of 10px</p>
// ...
</article>
To do so, I'd need to iterate over all the paragraphs of the article in order to access to the offsetHeight of each one. That way, I'd know how far I am from the container. Hence, I'd add the wanted element always that offsetHeight is a multiple of 200px, right?
<article>
<p>This is the first example paragraph of 10px</p>
<p>This is the second example paragraph of 10px</p>
// ...
<p>This is the twentieth example paragraph of 10px</p>
<!-- Here I would insert the first element,
as it would be at 200px from the container (article).
-->
// .... same applies with the rest of the article ....
</article>