0
我一直在重寫我的腳本文件幾次只是縮短了,我特地相當長,它除了有兩個funcitons,合併兩個功能合爲一體
我想合併這些運作成一個
首先與頁面加載功能,這在初始化文件準備
function pageload(elements, settings) {
elements = $.extend({}, defaultelements, elements || {});
settings = $.extend({}, defaultsettings, settings || {});
if(settings.url === '') {
console.log('No soup for you!');
movePage.prototype.pagein();
} else {
$.ajax({
url: location,
data: {
json: 'get_page',
slug: settings.url
},
success: function(data) {
var html = '<article id="' + data.page.slug + '" data-info="' + data.page.slug + '" class="subpage"></article>';
if(settings.motion) return false;
else settings.motion = true;
elements.thumbs.before(html);
var container = $('.subpage');
elements.body.prepend('<div class="spinner"></div>');
$('.spinner').hide();
container.load(data.page.url).hide();
elements.thumbs.stop().animate({
'top' : '100%'
}, settings.speed, settings.easing, function() {
$('.spinner').fadeIn(259);
setTimeout(function() {
$('.spinner').fadeOut(559);
setTimeout(function() {
container.fadeIn(659);
}, 450)
setTimeout(function() {
$('.spinner').remove();
}, 200)
}, 1250);
settings.motion = false;
movePage.prototype.pagein();
});
}
});
}
}
而且還有導航JSON負載腳本
function navigation(elements, settings) {
elements = $.extend({}, defaultelements, elements || {});
settings = $.extend({}, defaultsettings, settings || {});
elements.navitem.live('click', function(event) {
event.preventDefault();
$('.current-menu-item').removeClass('current-menu-item');
$(this).parent().addClass('current-menu-item');
settings.url = $(this).html();
$.ajax({
url: location,
data: {
json: 'get_page',
slug: settings.url
},
success: function(data) {
var current = $('.subpage');
if(settings.url == 'work') {
settings.motion = false;
} else if(current.attr('id') == settings.url) {
settings.motion = false;
return;
} else {
var html = '<article id="' + data.page.slug + '" data-info="' + data.page.slug + '" class="subpage"></article>';
if(settings.motion) return false;
else settings.motion = true;
elements.thumbs.before(html);
var container = $('.subpage');
elements.body.prepend('<div class="spinner"></div>');
$('.spinner').hide();
container.load(data.page.url).hide();
elements.thumbs.stop().animate({
'top' : '100%'
}, settings.speed, settings.easing, function() {
$('.spinner').fadeIn(259);
setTimeout(function() {
$('.spinner').fadeOut(559);
setTimeout(function() {
container.fadeIn(659);
}, 450)
setTimeout(function() {
$('.spinner').remove();
}, 200)
}, 1250);
settings.motion = false;
movePage.prototype.pagein();
$('article:not(#' + data.page.slug + ')').remove();
});
}
}
});
});
}
有沒有人在這裏有一個解決方案,使這些功能變成一個我可以在頁面加載和導航點擊中調用的功能。
這兩個函數正在完成這項工作,我只想將代碼縮小到一個函數中,因爲它們幾乎完成相同的工作 – Jonas
那麼難度是多少?那麼 - 是不是很難弄清楚/在哪裏合併兩個?你是要求我們爲你重寫你的代碼嗎? –
不,我已經解決了,我只是想要一些指針 – Jonas