最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
   }
});
发布评论

评论列表(0)

  1. 暂无评论