function ElementPositionRelativeOfWindow(element){
this.element = element;
this.autoupdate = false;
this.update();
}
ElementPositionRelativeOfWindow.prototype.element = null;
ElementPositionRelativeOfWindow.prototype.autoupdate = false;
ElementPositionRelativeOfWindow.prototype.getElementBoundingClientRect = function(el){
// getBoundingClientRect() is the key functionality;
// figure out the position of the element, in relation to the current viewport.
// top : position of top edge of element relative to top edge of viewport
// bottom: position of bottom edge of element relative to top edge of viewport
// left : position of left edge of element relative to left edge of viewport
// right : position of right edge of element relative to left edge of viewport
if (el && el.getBoundingClientRect) {
return el.getBoundingClientRect();
}
// TODO fallback to calculating the same using document offset and scrolling etc.
// NB: below is returned too if element is not visible
return {
top: 0,
right: 0,
bottom: 0,
left: 0,
width: 0,
height: 0
};
}
ElementPositionRelativeOfWindow.prototype.update = function(){
this.bounding_client_rect = this.getElementBoundingClientRect(this.element);
this.window_size_x = parseInt($(window).width() || window.innerWidth, 10);
this.window_size_y = parseInt($(window).height() || window.innerHeight, 10);
}
ElementPositionRelativeOfWindow.prototype.autoUpdateIfNeeded = function(){
if (this.autoupdate) {
this.update();
}
}
ElementPositionRelativeOfWindow.prototype.isFullyAbove = function(){
this.autoUpdateIfNeeded();
return this.bounding_client_rect.bottom < 0;
}
ElementPositionRelativeOfWindow.prototype.isFullyBelow = function(){
this.autoUpdateIfNeeded();
return this.bounding_client_rect.top > this.window_size_y;
}
ElementPositionRelativeOfWindow.prototype.isFullyLeftOf = function(){
this.autoUpdateIfNeeded();
return this.bounding_client_rect.right <...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.