get position of a div with SCROLL

0

I have for example a div at a certain point on the page what I want is that when the scroll is in the same position as that div I execute a function, for example:

if(Scroll == div){div.innerHTML="altura es ???'" ;}

I tried to do with scrollTop but I only run the function when the scroll is on the top and well I want to run it when both are over there, I do not know if I understand , here is my code

I would appreciate any help friends.

//asi es como lo se hacer pero no se si este correcto con srollTop
onscroll=function(){ 
    var yScroll=self.pageYOffset || (document.documentElement.scrollTop+document.body.scrollTop); 
    var div = document.getElementById('pp');
if(yScroll == div.scrollTop){div.innerHTML="altura es: "+yScroll;}
} 
 div{
    background:grey;
    font-size:30px;
    padding:5px;
    font-weight:bold;
    width:200px;
    border-radius:5px;

}
<!DOCTYPE html > 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Documento sin título</title> 



</head> 

<body> 
 
 
<br /> 
<br /> 

<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br />

<!--cando el scroll lleque a este div quiero ejecutar una funcion-->
<div id="pp">0</div>  
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
  
</body> 
</html>  
    
asked by andy gibbs 08.06.2018 в 18:38
source

1 answer

0

you can use the

$("#div").scrollTop();

to know the position

you can put this in the body

onscroll="myFunction()

and be constantly asking about the position of the scroll and if it is equal to your div there to do the action

    
answered by 08.06.2018 в 20:58