0
首先,請原諒我可能過度膨脹的JS函數(s)..也有一些代碼在我的腳本,可能會被刪除。這就是說,我仍然習慣於JS/jQuery,所以對我的腳本的任何和所有批評,建議或更正都是受歡迎的。優化jQuery動畫以獲得更好的移動性能?
也就是說,我已經建立了一個腳本,通過在攝影師的投資組合網站內容的各個部分創建動畫。腳本似乎在桌面計算機上運行得非常好,但移動性能充其量是笨重的。所以,我在這裏尋找如何優化我的腳本或添加一些代碼,以幫助理順動畫幫助..
繼承人的代碼在行動: http://jsbin.com/icereg/2/edit
編輯:更新JSbin項目: http://jsbin.com/icereg/3/edit
,這裏是麻煩的動畫功能:
// Bounce Top
function bounce_top() {
$isAnimating = 1;
$content.animate({top: '+=44'}, 200, 'easeInOutQuart',
function() {
$content.animate({top: '-=44'}, 200, 'easeInOutQuart');
$isAnimating = 0;
});
} // end bounce_top()
// Bounce Right
function bounce_right() {
$isAnimating = 1;
$content.animate({left: '-=44'}, 200, 'easeInOutQuart',
function() {
$content.animate({left: '+=44'}, 200, 'easeInOutQuart');
$isAnimating = 0;
});
} // end bounce_right()
// Bounce Bottom
function bounce_btm() {
$isAnimating = 1;
$content.animate({top: '-=44'}, 200, 'easeInOutQuart',
function() {
$content.animate({top: '+=44'}, 200, 'easeInOutQuart');
$isAnimating = 0;
});
} // end bounce_btm()
// Bounce Left
function bounce_left() {
$isAnimating = 1;
$content.animate({left: '+=44'}, 200, 'easeInOutQuart',
function() {
$content.animate({left: '-=44'}, 200, 'easeInOutQuart');
$isAnimating = 0;
});
} // end bounce_left()
// Scroll Content
function scroll_content() {
$isAnimating = 1;
if ($curr_section === ($total_section - 1) && ($event_target === 'down' || ($event_target === 'scroll' && $mouse_delta < 0))) {
// we're at the bottom; - 1 because of included 0 section;
bounce_btm();
} else if ($curr_section === 0 && ($event_target === 'up' || ($event_target === 'scroll' && $mouse_delta >= 0))) {
// we're already at the top
bounce_top();
} else if ($event_target === 'down' || $event_target === 'up' || $event_target === 'scroll' || $event_target === 'nav') {
// now either UP or DOWN inputs (mouse wheel, keyboad, nav links)
$nav_work_links.removeClass('active_nav');
$('#right_arrow').add('#left_arrow').remove();
if ($event_target === 'down' || ($event_target === 'scroll' && $mouse_delta < 0)) { // down or scroll down
$curr_section = $curr_section + 1;
$vertslidePos = '-=675';
} else if ($event_target === 'up' || ($event_target === 'scroll' && $mouse_delta >= 0)) { // up or scroll up
$curr_section = $curr_section - 1;
$vertslidePos = '+=675';
} else if ($event_target === 'nav') {
// now nav click inputs
$curr_section = $event_target_eq;
$vertslidePos = '-' + ($curr_section * 675);
}
if ($curr_page > 1) {
$content.animate({marginLeft: 0}, 450, 'easeInOutQuart'); // end .animate() method
}
$content.animate({top: $vertslidePos}, 450, 'easeInOutQuart',
function() {
if ($('.nav_work_items').eq($curr_section).index() === 0) {
$nav_work_links.removeClass('active_nav');
} else {
$('.nav_work_items').eq($curr_section - 1).find('.nav_work_links').addClass('active_nav');
}
$('.active_nav').before($left_arrow).after($right_arrow);
$curr_page = 1;
window.location.hash = '#work' + $curr_section;
$isAnimating = 0;
}); // end .animate() method
} else if ($event_target === 'left' || $event_target === 'right') {
// now either RIGHT or LEFT inputs
$right_arrow = $('#right_arrow');
$left_arrow = $('#left_arrow');
$active_nav = $('.active_nav');
$total_pages = $('#work' + $curr_section).children('.workcontent').length;
if ($curr_page === $total_pages && $event_target === 'right') {
bounce_right();
} else if ($curr_page === 1 && $event_target === 'left') {
bounce_left();
} else {
if ($event_target === 'right') {
$curr_page = $curr_page + 1;
$horzslidePos = '-=600px';
} else if ($event_target === 'left') {
$curr_page = $curr_page - 1;
$horzslidePos = '+=600px';
}
$content.animate({marginLeft: $horzslidePos}, 450, 'easeInOutQuart',
function() {
$isAnimating = 0;
}); // end .animate() method
}
}
} // end scroll_content();
這是相當多的代碼,它甚至不在你的問題。你可以在自己的問題中發佈它的相關部分(例如,在移動設備上遭受最差性能損失的動畫)嗎? –
是的,我試圖保持所有的內容移動到一個大功能,佔所有可能的導航輸入(按鍵,元素點擊,滾動鼠標滾輪)..將它分割成更小,更具體的個人功能更好? – mroncetwice
乍一看,如果僅從可讀性/可維護性的角度來看,最可能的是。 'scroll_content()'包含很多依賴於'$ curr_section'或'$ event_target'的分支,並且可以從抽象到他們自己的函數/處理程序中受益。這很難說沒有看到它的呼叫站點,但從你的jsbin鏈接,我會說這是一個相當安全的賭注。 –