0

I just want the value to increase when scrolling down and decrease when scrolling up.

This is the HTML:

<html>
<head>
<script src="/jquery.min.js"></script>
<script src="9.js"></script>
<style>
#addit
{
position:fixed;
top:0px;
left:0px;
}
</style>
</head>

<body>
<div id="addit">
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br>
</body>
</html>

Here is the jQuery code:

$(window).scroll(function(){
    var x=1;
    x=x+1;
    $("#addit").html(x);
});

As I am new to jQuery, I can't find a way for it! Can anyone help?

Greenonline
  • 1,330
  • 8
  • 23
  • 31
hemnath mouli
  • 2,617
  • 2
  • 17
  • 35
  • its already answered here http://stackoverflow.com/questions/17441065/how-to-detect-scroll-position-of-page-using-jquery – Pepo_rasta Jul 16 '15 at 14:07

2 Answers2

5

You don't need a variable, just:

$(window).scroll(function(){
    $("#addit").html($(window).scrollTop());
});

DEMO

lmgonzalves
  • 6,518
  • 3
  • 22
  • 41
2

Please try this:

$(window).scroll(function() {
    $("#addit").html($(window).scrollTop());
});
JochenJung
  • 7,183
  • 12
  • 64
  • 113
GS Bala
  • 390
  • 1
  • 11