Zack Sac S Zack Sac S - 29 days ago 7
jQuery Question

Get the position from top of current window

Is there any way to get bottom position using jquery of current viewport while scrolling . Not the bottom position of document but bottom position of visible window.

Answer

You need to sum window's scrollTop and innerHeight properties. It may be done this way:

var win = $(window);
var info = $('.bottom-position');
function onScroll() {
    var viewportBottom = win.scrollTop() + win.innerHeight();
    info.html('Bottom viewport at: ' + viewportBottom + 'px from top.');
};
win.on('scroll resize', onScroll);
onScroll();
body {
    margin: 0;
    font-family: sans-serif;
}
.content {
    height: 1000px;
    background: #000;
}
.bottom-position {
    position: fixed;
    bottom: 0;
    right: 0;
    color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content"></div>
<div class="bottom-position"></div>

Also on JSFiddle.

Comments