0

I have a problem with the ().scrollTop() function. I want to check if the body of my html is scrolled. I have tried this code:

p = $('body').scrollTop();
console.log(p);

also instead of the ('body') in the selector I tried (document.body), ("#ID").

Problem is, that in the console i always get a return value of "null" instead of "0". I tried the same with (window), then it returns "0" and works fine. But in my case I can not use (window) because I have to set the overflow of html to hidden. The body of the document is scrollable and has height/width 100%, so I dont understand why it is not working..

Thanks a lot for your help!!

ibanes88
  • 123
  • 2
  • 9

2 Answers2

0

use document instead of body:

$(document).scrollTop();
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
  • $(document).scrollTop(); would return the scrollValue of the The problem is, like I wrote before, that I dont want the scrollValue of the entire HTML because this would always be "0" as I have overflow:hidden on my . Instead I want the Value for the -element. – ibanes88 Apr 09 '14 at 15:26
  • @user1430544 : would be great if you can create the fiddle – Milind Anantwar Apr 09 '14 at 15:33
0

you may want to use this :

<html>
<body>
<div id="wrap">
you html ...
</div>
</body>
</html>

js:

p=$("#wrap").scrollTop()
console.log(p)
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
  • I want to get the current scroll value, in the end I want it to return "0" for being at the top or eg. "50" if i scrolled the body down for 50 pixels.. Problem is that i dont get the scrollvalue of body which should be "0" instead I get "null" – ibanes88 Apr 09 '14 at 15:23
  • @user1430544 sorry my bad , then milind answer is correct . – ProllyGeek Apr 09 '14 at 15:26
  • Anyway , scroll top will work on something that have a parent dom , try wrapping the whole content of your body in a container div – ProllyGeek Apr 09 '14 at 15:27
  • No, it would be correct if I wanted to get the scrollValue of the instead i search for the scrollValue of .. – ibanes88 Apr 09 '14 at 15:29
  • http://jsfiddle.net/gw6yb/1/ here is the project, the problem is the shadow under the navBar which I try to inicialize with jquery – ibanes88 Apr 09 '14 at 16:13
  • @user1430544 this will work just clean up your code please.http://jsfiddle.net/prollygeek/gw6yb/3/ – ProllyGeek Apr 09 '14 at 18:49