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

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

programmeradmin4浏览0评论

With strange reasons $(window).scroll() not working. I am working on this for a while but I can't figured out what silly thing I am doing in it.

fiddle

$(document).ready(function(){

    // fill body with data so we can see the scroll
    for(var i=0; i<1000; i++)
    {        
        $('#list').append(i+'<br>');
    }
    // jQuery plug-in
    $(window).scroll(function(){
        if($(window).scrollTop() == 800)
        {
            alert('mission acplished');
        }
    });
});

Console error

TypeError: window.scrollTop is not a function

thanks in advance!

With strange reasons $(window).scroll() not working. I am working on this for a while but I can't figured out what silly thing I am doing in it.

fiddle

$(document).ready(function(){

    // fill body with data so we can see the scroll
    for(var i=0; i<1000; i++)
    {        
        $('#list').append(i+'<br>');
    }
    // jQuery plug-in
    $(window).scroll(function(){
        if($(window).scrollTop() == 800)
        {
            alert('mission acplished');
        }
    });
});

Console error

TypeError: window.scrollTop is not a function

thanks in advance!

Share Improve this question asked Feb 27, 2014 at 6:00 Aamir ShahzadAamir Shahzad 6,8348 gold badges48 silver badges72 bronze badges 1
  • 4 It works just fine. You're probably not getting the expected result, because you will never hit exactly 800. Try using >= 800 - Fiddle – Bic Commented Feb 27, 2014 at 6:03
Add a ment  | 

1 Answer 1

Reset to default 9

Check this http://jsfiddle/KHeZY/39/

It is not always necessary that $(window).scroll() stops at 800, this event only triggers after you stop scrolling $(window).scrollTop()

you need to set some window for it.

$(window).scroll(function(){console.log($(window).scrollTop());
    if($(window).scrollTop() > 800 && $(window).scrollTop() < 850)
    {
        alert('mission acplished');
    }
});
发布评论

评论列表(0)

  1. 暂无评论