javascript - Check whether entire div is viewable for the user - Stack Overflow

Is there a way to check whether the div is entirely visible to the user? I mean the scroll bar is posit

Is there a way to check whether the div is entirely visible to the user? I mean the scroll bar is positioned so that the entire div fits inside the viewport.

Is there a way to check whether the div is entirely visible to the user? I mean the scroll bar is positioned so that the entire div fits inside the viewport.

Share Improve this question edited Dec 21, 2011 at 20:32 Lightness Races in Orbit 386k77 gold badges666 silver badges1.1k bronze badges asked Dec 21, 2011 at 20:22 keepwalkingkeepwalking 2,6546 gold badges32 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

This is exactly what this was created for: http://www.appelsiini/projects/viewport

You need to check positions for the scrollbar and the div in question, and pare them in the scroll event handler:

$(window).scroll(function() {
    var top = $(window).scrollTop();
    var bottom = top + $(window).height();
    var dtop = $('#mydiv').position().top;
    var dbottom = dtop + $('#mydiv').height();

    if (dtop>=top && dbottom<=bottom) {
        alert('okay!');
    }
});

http://jsfiddle/mblase75/dMwMb/

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信