Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda