Last active
September 12, 2017 11:04
-
-
Save automagisch/38b781379e5c81d375eaae076414a7c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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