我正在制作一个PhoneGap应用程序。我有一个带有登录按钮的index.html页面,该按钮将重定向到网站应用程序。
当单击登录按钮时,我希望显示一个加载gif,同时页面正在被缓存/预加载,并在完成后重定向到该页面。
我希望能提供一个示例脚本代码。
当单击登录按钮时,我希望显示一个加载gif,同时页面正在被缓存/预加载,并在完成后重定向到该页面。
我希望能提供一个示例脚本代码。
jQuery(document).ready(function ($) {
$app = $('.app'),
$pages = $('.page');
$('.login').on('click', function () {
$app.addClass('loading');
$pages.removeClass('active').filter('.page-2').addClass('active');
setTimeout(function () {
$app.removeClass('loading');
}, 500);
});
$('.logout').on('click', function () {
$app.addClass('loading');
$pages.removeClass('active').filter('.page-1').addClass('active');
setTimeout(function () {
$app.removeClass('loading');
}, 500);
});
$app.removeClass('loading');
});
但如果您需要更复杂的解决方案,最好使用一些js框架,例如Knockout.js和一些js路由器,或者更强大的Angular.JS
<iframe>
元素完成了类似的任务。我的 PhoneGap 应用程序需要加载本地页面,并可能通过 jQuery 进行修改。使用普通的重定向(通过 window.location
)会有两个问题造成的加载过程中的痕迹:
我通过在不可见的 <iframe>
中加载页面,并仅在其加载完成并通过 jQuery 进行修改后使 <iframe>
可见来解决了这个问题。我想有各种方法可以做到这一点,但我是通过调整它们的 z-index
来完成的。
我创建了一个带有注释的 fiddle,它稍微简单一些,并添加了一个加载旋转器:
http://jsfiddle.net/Leftium/L2HdV/(感谢 Umidbek 提供的 spinner!)
jQuery(document).ready(function ($) {
$app = $('.app');
// Attach behavior to Login button.
$('.login').on('click', function () {
$app.addClass('loading');
// Create an <iframe>.
$iframe = $('<iframe>');
// Set url of <iframe> to desired redirect URL.
// Note: the URL must be in the same domain,
// or set special HTTP headers to allow rendering inside an <iframe>.
$iframe.attr({src: 'http://doc.jsfiddle.net/'});
// Add <iframe> we just created to DOM.
$iframe.appendTo($('body'));
// When <iframe> has been loaded, remove <div> containing login button
// and loading spinner to reveal <iframe>.
$iframe.load(function() {
$('.app').remove()
});
});
});