jquery - $("#id").scrollTop() returns null -


I am trying to get the position of a coulpe by using the scrollTop () method in my html page , But this only gives blank the code here:

  & lt; Div id = "div1" class = "transition" & gt; & Lt; Img src = "bg1.jpg" /> & Lt; / Div & gt; & Lt; Div id = "div2" class = "transition" & gt; & Lt; Img src = "bg2.jpg" /> & Lt; / Div & gt;  

and Javascript:

  var t1 = $ ('# div1'). ScrollTop (); Var t2 = $ ('# div2'). ScrollTop (); Console.log (T1 + "+ + T2);  

And it prints T1 and T2 = blank. Can you see what the problem is?

Those div s scrollbar (I'm assuming), so they are naturally You can not scroll through.

If you want to find the situation from which you need to scroll so that they are on top, then try:

$ ('# Id'). Offset (). Top

here There is an active demo:


  var txt = $ ('# div1'). Offset (). Top + '' $ ('# div2'). Offset (). Top; Document.getElementById ('Output'). AppendChild (document.createTextNode (txt));  
  & Lt; script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Div id = 'div1' & gt; This is some stuff & lt; / Div & gt; & Lt; Div id = 'div2' & gt; More stuff & lt; / Div & gt; & Lt; Div id = 'output' & gt; & Lt; / Div & gt;  


Comments