hemnath mouli
hemnath mouli

Reputation: 2755

jQuery increase and decrease value on scrolling

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?

Upvotes: 0

Views: 1972

Answers (2)

GS Bala
GS Bala

Reputation: 400

Please try this:

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

Upvotes: 2

lmgonzalves
lmgonzalves

Reputation: 6588

You don't need a variable, just:

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

DEMO

Upvotes: 5

Related Questions