Execute Code/Pixel On Page Scroll Using JSCRIPT?

superaff1984

Senior Member
Premium Member
Joined
Jun 16, 2010
Messages
992
Reaction score
131
Hi, I have this code below. I plan on putting in a conversion pixel where it says "execute your code here..." and I am wanting that code to execute when a visitor scroll 70% of the page.

Is this correct below? and where should I place this code at bottom or in headers?

JavaScript:
window.addEventListener('scroll', function() {
  const scrollPosition = window.scrollY; // Current vertical scroll position.
  const totalHeight = document.documentElement.scrollHeight - window.innerHeight; // Total scrollable height.
  const seventyPercent = 0.7 * totalHeight;

  if (scrollPosition >= seventyPercent) {
    // Execute your code here when the user scrolls 70% of the page.
    console.log("Scrolled 70% of the page!");
    // Example: You could load more content or display a special element here.
  }
});
 
Back
Top