Skip to content

Instantly share code, notes, and snippets.

@sourcec0de
Created December 6, 2012 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourcec0de/4222638 to your computer and use it in GitHub Desktop.
Save sourcec0de/4222638 to your computer and use it in GitHub Desktop.
Javascript Distance Library
$(document).ready(function(){
function distance(element) {
// About object is returned if there is no 'element' parameter
var about = {
Version: 0.1,
Author: "James Qualls",
Created: "Dec 5th, 2012",
Updated: "Not yet"
};
if (element) {
// Avoid clobbering the window scope:
// return a new distance object if we're in the wrong scope
if (window === this) {
return new distance(element);
}
// We're in the correct object scope:
// Init our element object and return the object
this.e = document.querySelector(element);
return this;
} else {
// No 'element' parameter was given, return the 'about' object
return about;
}
};
/* Prototype Functions
============================*/
distance.prototype = {
top: function () {
obj = $(this.e).offset();
distanceToTop = obj.top;
return distanceToTop;
},
bottom: function () {
var space = window.innerHeight - this.e.offsetTop + this.e.offsetHeight;
windowHeight = document.body.offsetHeight,
distanceToBottom = windowHeight - space;
return distanceToBottom;
},
height: function (){
var height = this.e.offsetHeight;
return height;
}
};
$(".stack1").click(function() {
var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
console.log("top: "+distance('div.stack1').top());
console.log("bottom: "+distance('div.stack1').bottom());
console.log("height: "+distance('div.stack1').height());
console.log("vscroll: "+vscroll);
});
$(".stack2").click(function() {
var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
console.log("top: "+distance('div.stack2').top());
console.log("bottom: "+distance('div.stack2').bottom());
console.log("height: "+distance('div.stack2').height());
console.log("vscroll: "+vscroll);
});
$(".stack3").click(function() {
var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
console.log("top: "+distance('div.stack3').top());
console.log("bottom: "+distance('div.stack3').bottom());
console.log("height: "+distance('div.stack3').height());
console.log("vscroll: "+vscroll);
});
$(".stack4").click(function() {
var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
console.log("top: "+distance('div.stack4').top());
console.log("bottom: "+distance('div.stack4').bottom());
console.log("height: "+distance('div.stack4').height());
console.log("vscroll: "+vscroll);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment