Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
html + javascript scroll detection not working

I cannot seem to get scroll event to work for this layout. I am stuck and I was wondering if you guys can help me out. What I am trying to do is to detect when the content div, which has id="myContent", is scrolling. What am I doing wrong?

Thanks in advance.

<!DOCTYPE html>
<html>
<head>

<script>    

    window.onscroll = function() {scrollFunction()};

    function scrollFunction() {
        console.log("scrollFunction");
    }
    
    console.log(document.getElementById("myContent")); //dont know why it cannot find this arrrgghhh !!!

</script>
    
</head>
<body style="width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    margin: 0;
    padding: 0;
    position: absolute;">

    <!--container-->
    <div style="border: 1px solid red;
        width: 100%;
        height: 100%;
        position: fixed;
        overflow: auto;
        overflow-x: hidden;">
            
        <!--header-->       
        <div style="border: 1px solid green;
            width: 100%;
            height: 75px;
            top: 0;
            position: sticky;
            margin: 0 auto;
            align-items: center;"> 
        </div>
        
        <!--content-->
        <div id="myContent" 
            onscroll="scrollFunction();"            
            style="border: 1px solid blue; 
                width: 100%;
                height: auto;
                margin: 0 auto;">   
                
            <!--content 1-->                    
            <div style="width: 100%; height: 1000px;">
            
            </div>          
        </div>
        
        <!--footer-->
        <div style="border: 1px solid pink;     
            width: 100%;
            height: 150px;
            margin: 0 auto;">   
        </div>          
    </div>  
</body> 

Any help is appreciated. Thanks in advance

Best regards

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is that currently the vertical scrollbar does not appear on the div that has the onscroll listener defined but on its parent. In order to see the scrollbar on that div, you must explicitly define its height together with overflow-y: auto. Make sure also to define overflow: hidden on the parent div.

Please take a look at your amended code and see how it works.

<!DOCTYPE html>
<html>
<head>

<script>    

    window.onscroll = function() {scrollFunction()};

    function scrollFunction() {
        console.log("scrollFunction");
    }
    
    console.log(document.getElementById("myContent")); //dont know why it cannot find this arrrgghhh !!!

</script>
    
</head>
<body style="width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    margin: 0;
    padding: 0;
    position: absolute;">

    <!--container-->
    <div style="border: 1px solid red;
        width: 100%;
        height: 100%;
        position: fixed;
        overflow: hidden;">
            
        <!--header-->       
        <div style="border: 1px solid green;
            width: 100%;
            height: 75px;
            top: 0;
            position: sticky;
            margin: 0 auto;
            align-items: center;"> 
        </div>
        
        <!--content-->
        <div id="myContent" 
            onscroll="scrollFunction();"            
            style="border: 1px solid blue; 
                background-color: gray;
                width: 100%;
                height: 500px;
                overflow-y: auto;
                margin: 0 auto;">   
                
            <!--content 1-->                    
            <div style="width: 100%; height: 1000px;">
            
            </div>          
        </div>
        
        <!--footer-->
        <div style="border: 1px solid pink;     
            width: 100%;
            height: 150px;
            margin: 0 auto;">   
        </div>          
    </div>  
</body>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda