mukul soni
mukul soni

Reputation: 127

$(window).scroll(function) is not working

I want to alert something whenever my window get scrolled but it's not working. Here is my code

$(window).scroll(function(){
    var ws = $(window).scrollTop();
    if(ws)
    {
        alert('Scrolled');
    }
});

Upvotes: 0

Views: 9609

Answers (4)

Bart Az.
Bart Az.

Reputation: 1645

In my case, this rule was to blame:

html,
body { overflow-x: hidden; }

Hiding overflowing elements is fine as long as the rule is applied only to the body element.

Upvotes: 2

Hanuman Sahay
Hanuman Sahay

Reputation: 29

If you want to execute or initialize function, you have to write below simple code.

$(window).on("scroll", function() {
   var scrollTop = $(window).scrollTop();
   if(scrollTop >= 10) {
     //here is your function name
   }
});

Upvotes: 0

frnt
frnt

Reputation: 8795

Try this,

CSS

body{
height:1200px;
}

Jquery

$(window).on('scroll',function(){
    alert('Hi');
});

(or)

  $(window).on('scroll',function(){
        var wstp = $(window).scrollTop();
       if(wstp)
      {
       alert('hi');
      }
    });

Just add height to your body tag and scroll.

Upvotes: 0

user2560539
user2560539

Reputation:

Your code works fine if you add an HTML element having height

$(window).scroll(function(){
    var ws = $(window).scrollTop();
   if(ws)
  {
        console.log(ws);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="height:1500px"></div>

Upvotes: 1

Related Questions