Skip to content

Commit

Permalink
optimize offset helper (uses getBoundingClientRect now)
Browse files Browse the repository at this point in the history
  • Loading branch information
callmecavs committed Jul 10, 2015
1 parent ee110ae commit 68e9d86
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 41 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "layzr.js",
"version": "1.4.1",
"version": "1.4.2",
"description": "A small, fast, modern, and dependency-free library for lazy loading.",
"homepage": "http://callmecavs.github.io/layzr.js/",
"main": ["dist/layzr.js"],
Expand Down
30 changes: 11 additions & 19 deletions dist/layzr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Layzr.js 1.4.0 - A small, fast, modern, and dependency-free library for lazy loading.
* Layzr.js 1.4.2 - A small, fast, modern, and dependency-free library for lazy loading.
* Copyright (c) 2015 Michael Cavalea - http://callmecavs.github.io/layzr.js/
* License: MIT
*/
Expand Down Expand Up @@ -50,7 +50,7 @@ function Layzr(options) {

Layzr.prototype._requestScroll = function() {
if(this._optionsContainer === window) {
this._lastScroll = window.scrollY || window.pageYOffset;
this._lastScroll = window.pageYOffset;
}
else {
this._lastScroll = this._optionsContainer.scrollTop + this._getOffset(this._optionsContainer);
Expand All @@ -67,18 +67,10 @@ Layzr.prototype._requestTick = function() {
};

// OFFSET HELPER
// borrowed from: http://stackoverflow.com/questions/5598743/finding-elements-position-relative-to-the-document
// remember, getBoundingClientRect is relative to the viewport

Layzr.prototype._getOffset = function(element) {
var offsetTop = 0;

do {
if(!isNaN(element.offsetTop)) {
offsetTop += element.offsetTop;
}
} while(element = element.offsetParent);

return offsetTop;
Layzr.prototype._getOffset = function(node) {
return node.getBoundingClientRect().top + window.pageYOffset;
};

// HEIGHT HELPER
Expand Down Expand Up @@ -113,15 +105,15 @@ Layzr.prototype._inViewport = function(node) {
var viewportBottom = viewportTop + this._getContainerHeight();

// get node top and bottom offset
var elementTop = this._getOffset(node);
var elementBottom = elementTop + this._getContainerHeight();
var nodeTop = this._getOffset(node);
var nodeBottom = nodeTop + this._getContainerHeight();

// calculate threshold, convert percentage to pixel value
var threshold = (this._optionsThreshold / 100) * window.innerHeight;

// return if element in viewport
return elementBottom >= viewportTop - threshold
&& elementTop <= viewportBottom + threshold
// return if node in viewport
return nodeBottom >= viewportTop - threshold
&& nodeTop <= viewportBottom + threshold
&& !node.hasAttribute(this._optionsAttrHidden);
};

Expand Down Expand Up @@ -151,7 +143,7 @@ Layzr.prototype._reveal = function(node) {
};

Layzr.prototype.updateSelector = function() {
// update cached list of elements matching selector
// update cached list of nodes matching selector
this._nodes = document.querySelectorAll(this._optionsSelector);
};

Expand Down
4 changes: 2 additions & 2 deletions dist/layzr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "layzr.js",
"version": "1.4.1",
"version": "1.4.2",
"description": "A small, fast, modern, and dependency-free library for lazy loading.",
"homepage": "http://callmecavs.github.io/layzr.js/",
"main": "dist/layzr.js",
Expand Down
28 changes: 10 additions & 18 deletions src/layzr.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Layzr(options) {

Layzr.prototype._requestScroll = function() {
if(this._optionsContainer === window) {
this._lastScroll = window.scrollY || window.pageYOffset;
this._lastScroll = window.pageYOffset;
}
else {
this._lastScroll = this._optionsContainer.scrollTop + this._getOffset(this._optionsContainer);
Expand All @@ -52,18 +52,10 @@ Layzr.prototype._requestTick = function() {
};

// OFFSET HELPER
// borrowed from: http://stackoverflow.com/questions/5598743/finding-elements-position-relative-to-the-document
// remember, getBoundingClientRect is relative to the viewport

Layzr.prototype._getOffset = function(element) {
var offsetTop = 0;

do {
if(!isNaN(element.offsetTop)) {
offsetTop += element.offsetTop;
}
} while(element = element.offsetParent);

return offsetTop;
Layzr.prototype._getOffset = function(node) {
return node.getBoundingClientRect().top + window.pageYOffset;
};

// HEIGHT HELPER
Expand Down Expand Up @@ -98,15 +90,15 @@ Layzr.prototype._inViewport = function(node) {
var viewportBottom = viewportTop + this._getContainerHeight();

// get node top and bottom offset
var elementTop = this._getOffset(node);
var elementBottom = elementTop + this._getContainerHeight();
var nodeTop = this._getOffset(node);
var nodeBottom = nodeTop + this._getContainerHeight();

// calculate threshold, convert percentage to pixel value
var threshold = (this._optionsThreshold / 100) * window.innerHeight;

// return if element in viewport
return elementBottom >= viewportTop - threshold
&& elementTop <= viewportBottom + threshold
// return if node in viewport
return nodeBottom >= viewportTop - threshold
&& nodeTop <= viewportBottom + threshold
&& !node.hasAttribute(this._optionsAttrHidden);
};

Expand Down Expand Up @@ -136,7 +128,7 @@ Layzr.prototype._reveal = function(node) {
};

Layzr.prototype.updateSelector = function() {
// update cached list of elements matching selector
// update cached list of nodes matching selector
this._nodes = document.querySelectorAll(this._optionsSelector);
};

Expand Down

0 comments on commit 68e9d86

Please sign in to comment.