javascript - $(window).scroll(function) is not working - Stack Overflow

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

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');
    }
});

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');
    }
});
Share Improve this question edited Jun 25, 2016 at 14:07 Mohammad 21.5k16 gold badges57 silver badges85 bronze badges asked Jun 25, 2016 at 14:06 mukul sonimukul soni 1272 silver badges7 bronze badges 2
  • It work, See this – Mohammad Commented Jun 25, 2016 at 14:12
  • it works just add <div style="height:1500px"></div> – Maksym Semenykhin Commented Jun 25, 2016 at 14:29
Add a ment  | 

4 Answers 4

Reset to default 2

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.

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="height:1500px"></div>

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.

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
   }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745164027a4614536.html

相关推荐

  • javascript - $(window).scroll(function) is not working - Stack Overflow

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

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信