Skip to content

Instantly share code, notes, and snippets.

@automagisch
Last active September 12, 2017 11:04
Show Gist options
  • Save automagisch/38b781379e5c81d375eaae076414a7c4 to your computer and use it in GitHub Desktop.
Save automagisch/38b781379e5c81d375eaae076414a7c4 to your computer and use it in GitHub Desktop.
/*
jQuery plugin for dynamically loading pages
$("#pageHolder").loadPage({
file: 'index.html',
effect: 'slide|fade',
duration: 1000
}).then(function() {
// done loading, this = $(html)
}, function(error) {
// loading failed, error = error object
});
*/
$(function() {
$.fn.loadPage = function( props, callback ) {
var _self = $(this);
var config = $.extend({
file: 'page.html',
effect: 'slide',
duration: 1000
}, props);
var effect = function() {
return {
in: config.effect + 'In',
out: config.effect + 'Out'
};
}();
return new Promise(function(resolve, reject) {
$.load(config.file, function(html) {
_self[effect.out](config.duration, function() {
_self.html(html);
_self[effect.in](config.duration, function() {
resolve.apply(html);
});
});
}, function(err) {
reject(err);
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment