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

166
Visualizações
window.scrollTop is undfined

why would window.scrollTop be undefined?

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop

The Element.scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically.

An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0.

jQuery(window).on('scroll', function() {
        if(window.scrollTop > 0) {
            console.log('of the top')
            console.log('scroll top value is: ' + window.scrollTop)
        } else {
            console.log('the top')
            console.log('scroll top value is: ' + window.scrollTop)
        }
    })

All I'm getting in my console log is:

scroll top value is: undefined

and I'm always getting an evaluation of in the if else:

of the top

even if I'm scrolled to the top of the page.

Why would window.scrollTop be undefined?

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop does window not count as an element?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to use the jQuery $ identifier as well as the parenthesis () for .scrollTop() in order to get the position

Here's the executable code:

<script>
jQuery(window).on('scroll', function() {
        if($(window).scrollTop() > 0) {
            console.log('of the top')
            console.log('scroll top value is: ' + $(window).scrollTop())
        } else {
            console.log('the top')
            console.log('scroll top value is: ' + $(window).scrollTop())
        }
    })
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

<script>
jQuery(window).on('scroll', function() {
        if($(window).scrollTop() > 0) {
            console.log('of the top')
            console.log('scroll top value is: ' + $(window).scrollTop())
        } else {
            console.log('the top')
            console.log('scroll top value is: ' + $(window).scrollTop())
        }
    })
</script>

YESSINE's code is correct. but can make it simple

<script>
jQuery(window).on('scroll', function() {
        var window = $(window);
        if(window.scrollTop() > 0) {                
            console.log('of the top')
            console.log('scroll top value is: ' + window.scrollTop())
        } else {
            console.log('the top')
            console.log('scroll top value is: ' + window.scrollTop())
        }
    })
</script>
about 4 years ago · Juan Pablo Isaza Relatório
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