Skip to content

Commit

Permalink
added: support for asynchronous loading
Browse files Browse the repository at this point in the history
Defined both modules using UMD.
They are now defined as browser globals as well as using the AMD
pattern.
See here for more info:
#160
  • Loading branch information
janpaepke committed Oct 10, 2014
1 parent 32d37a4 commit 052a846
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 39 deletions.
9 changes: 7 additions & 2 deletions dev/src/class.ScrollMagic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
define('ScrollMagic', ['jquery', 'TweenMax', 'TimelineMax'], function ($, TweenMax, TimelineMax) {
/**
* The main class that is needed once per scroll container.
*
Expand Down Expand Up @@ -25,7 +26,7 @@
If you don't use custom containers, trigger elements or have static layouts, where the positions of the trigger elements don't change, you can set this to 0 disable interval checking and improve performance.
*
*/
var ScrollMagic = function(options) {
ScrollMagic = function(options) {

/*
* ----------------------------------------------------------------
Expand Down Expand Up @@ -581,5 +582,9 @@

// INIT
construct();
ScrollMagic.version = "%VERSION%"; // version number for each instance
return ScrollMagic;
};
};
ScrollMagic.version = "%VERSION%"; // version number for browser global
return ScrollMagic;
});
12 changes: 10 additions & 2 deletions dev/src/class.ScrollScene.extend.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
@license Dual licensed under MIT license and GPL.
@author Jan Paepke - e-mail@janpaepke.de
*/
(function($, ScrollScene) {
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['ScrollScene', 'jquery'], factory);
} else {
// no browser global needed, just execute
factory(root.ScrollScene, root.jQuery);
}
}(this, function(ScrollScene, $) {
/**
* Add Indicators for a ScrollScene.
* __REQUIRES__ ScrollMagic Debug Extension: `jquery.scrollmagic.debug.js`
Expand Down Expand Up @@ -236,4 +244,4 @@
}
}
};
})(jQuery, ScrollScene);
}));
9 changes: 6 additions & 3 deletions dev/src/class.ScrollScene.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
define('ScrollScene', ['jquery', 'TweenMax', 'TimelineMax'], function ($, TweenMax, TimelineMax) {
/**
* A ScrollScene defines where the controller should react and how.
*
Expand Down Expand Up @@ -40,7 +41,7 @@
** `3` => errors, warnings, debuginfo
*
*/
var ScrollScene = function (options) {
ScrollScene = function (options) {

/*
* ----------------------------------------------------------------
Expand Down Expand Up @@ -893,7 +894,7 @@
*
* @fires ScrollScene.update
*
* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next tweenmax tick (better performance).
* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next update cycle (better performance).
* @returns {ScrollScene} Parent object for chaining.
*/
this.update = function (immediately) {
Expand Down Expand Up @@ -1734,4 +1735,6 @@
// INIT
construct();
return ScrollScene;
};
};
return ScrollScene;
});
25 changes: 17 additions & 8 deletions dev/src/core.ScrollMagic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@
@todo: bug: having multiple scroll directions with cascaded pins doesn't work (one scroll vertical, one horizontal)
@todo: feature: optimize performance on debug plugin (huge drawbacks, when using many scenes)
*/
(function($, window) {
(function(root) {

"use strict";

var define = root.define, ScrollMagic, ScrollScene;
if (typeof define !== 'function' || !define.amd) {
// No AMD loader -> Provide custom method to to register browser globals instead
define = function (moduleName, dependencies, factory) {
for (var x = 0, dependency; x<dependencies.length; x++) {
dependency = dependencies[x];
if (dependency === 'jquery') { // lowercase with require, but camel case as global
dependency = 'jQuery';
}
dependencies[x] = root[dependency];
}
root[moduleName] = factory.apply(root, dependencies);
};
}

// (BUILD) - INSERT POINT: class.ScrollMagic

// (BUILD) - INSERT POINT: class.ScrollScene

// store version
ScrollMagic.prototype.version = "%VERSION%";
// make global references available
window.ScrollScene = ScrollScene;
window.ScrollMagic = ScrollMagic;

// (BUILD) - INSERT POINT: utils

})(jQuery, window);
})(this || window);
38 changes: 14 additions & 24 deletions dev/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
var debug = (function (console) {
var loglevels = ["error", "warn", "log"];
if (!console.log) {
console.log = $.noop; // no console log, well - do nothing then...
console.log = function(){}; // no console log, well - do nothing then...
}
$.each(loglevels, function (index, method) { // make sure methods for all levels exist.
for(var i = 0, method; i<loglevels.length; i++) { // make sure methods for all levels exist.
method = loglevels[i];
if (!console[method]) {
console[method] = console.log; // prefer .log over nothing
}
});
}
// debugging function
return function (loglevel) {
if (loglevel > loglevels.length || loglevel <= 0) loglevel = loglevels.length;
Expand All @@ -30,27 +31,16 @@
}(window.console = window.console || {}));
// (BUILD) - REMOVE IN MINIFY - END
// a helper function that should generally be faster than jQuery.offset() and can also return position in relation to viewport.
var getOffset = function ($elem, relativeToViewport) {
var offset = {
top: 0,
left: 0
},
elem = $elem[0];
if (elem) {
if (elem.getBoundingClientRect) { // check if available
var rect = elem.getBoundingClientRect();
offset.top = rect.top;
offset.left = rect.left;
if (!relativeToViewport) { // clientRect is by default relative to viewport...
offset.top += $(document).scrollTop();
offset.left += $(document).scrollLeft();
}
} else { // fall back to jquery
offset = $elem.offset() || offset; // if element has offset undefined (i.e. document) use 0 for top and left
if (relativeToViewport) { // jquery.offset is by default NOT relative to viewport...
offset.top -= $(document).scrollTop();
offset.left -= $(document).scrollLeft();
}
var getOffset = function (elem, relativeToViewport) {
var offset = {top: 0, left: 0};
elem = elem[0]; // tmp workaround until jQuery dependency is removed.
if (elem && elem.getBoundingClientRect) { // check if available
var rect = elem.getBoundingClientRect();
offset.top = rect.top;
offset.left = rect.left;
if (!relativeToViewport) { // clientRect is by default relative to viewport...
offset.top += (window.pageYOffset || document.scrollTop || 0) - (document.clientTop || 0);
offset.left += (window.pageXOffset || document.scrollLeft || 0) - (document.clientLeft || 0);
}
}
return offset;
Expand Down

0 comments on commit 052a846

Please sign in to comment.