Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
How to make function starts at a targeted div

This is my first post here, sorry if I make rookie mistakes... I started web development 6months ago and I haven't learn JavaScript yet, although I need to use it for a personal project. I want to make a scale model of solar system, like this https://joshworth.com/dev/pixelspace/pixelspace_solarsystem.html (I didn't know this website existed when I got the idea but now I'm inspired by it).
So what I want to do is the km counter for the length of #divB, I've found a beginning of code (thank you @Przemek for this https://stackoverflow.com/a/35923228), that I tried to arrange with my own research, this is just an example of the idea (I don't want you guys to do all the job for me !):

My problem is that I can't find a way to make the counter starts at #divB and stops when the div ends.
My second problem is I don't know how to make the script works (might be stupid request but I tried the original @Przemek's code (that perfectly works on JSFiddle) between element in the html file, or with an external source linked to a js file but it's not working for me)

var counter = document.getElementById('count');

function updateCounter() {
  'use strict';
  var clientHeight = document.getElementById('divB').clientHeight - window.innerHeight;
  counter.textContent = Math.round(document.documentElement.scrollTop * 3.4735);
}
//multiplied by 3.4735 for the km scale

document.addEventListener('scroll', updateCounter);
window.addEventListener('resize', updateCounter);
#divA {
  height: 1000px;
  width: 100%
}

#divB {
  height: 1296000px;
  width: 100%
}

#divC {
  height: 1000px;
  width: 100%
}

#count {
  position: fixed;
}
<span id="count">0</span>
<div id="divA"></div> <!-- intro -->
<div id="divB"></div> <!-- planets scale -->
<div id="divC"></div> <!-- footer -->

JSFiddle here

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!