Skip to content

Commit

Permalink
Isolate window dependency on boot
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellito committed May 2, 2017
1 parent 5ddb348 commit 115e88f
Showing 1 changed file with 50 additions and 40 deletions.
90 changes: 50 additions & 40 deletions src/vivus.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ var requestAnimFrame, cancelAnimFrame, parsePositiveInt;
*/
function Vivus (element, options, callback) {

setupEnv();

// Setup
this.isReady = false;
this.setElement(element, options);
Expand Down Expand Up @@ -685,12 +687,6 @@ Vivus.prototype.isInViewport = function (el, h) {
return (elTop + elHeight * h) <= viewed && (elBottom) >= scrolled;
};

/**
* Alias for document element
*
* @type {DOMelement}
*/
Vivus.prototype.docElem = window.document.documentElement;

/**
* Get the viewport height in pixels
Expand Down Expand Up @@ -718,41 +714,55 @@ Vivus.prototype.scrollY = function () {
return window.pageYOffset || this.docElem.scrollTop;
};

/**
* Alias for `requestAnimationFrame` or
* `setTimeout` function for deprecated browsers.
*
*/
requestAnimFrame = (function () {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback){
return window.setTimeout(callback, 1000 / 60);
}
);
})();
var setupEnv = function () {

/**
* Alias for `cancelAnimationFrame` or
* `cancelTimeout` function for deprecated browsers.
*
*/
cancelAnimFrame = (function () {
return (
window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.oCancelAnimationFrame ||
window.msCancelAnimationFrame ||
function(id){
return window.clearTimeout(id);
}
);
})();
if (Vivus.prototype.docElem) {
return;
}

/**
* Alias for document element
*
* @type {DOMelement}
*/
Vivus.prototype.docElem = window.document.documentElement;

/**
* Alias for `requestAnimationFrame` or
* `setTimeout` function for deprecated browsers.
*
*/
requestAnimFrame = (function () {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback){
return window.setTimeout(callback, 1000 / 60);
}
);
})();

/**
* Alias for `cancelAnimationFrame` or
* `cancelTimeout` function for deprecated browsers.
*
*/
cancelAnimFrame = (function () {
return (
window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.oCancelAnimationFrame ||
window.msCancelAnimationFrame ||
function(id){
return window.clearTimeout(id);
}
);
})();
}

/**
* Parse string to integer.
Expand Down

0 comments on commit 115e88f

Please sign in to comment.