2012-11-03 46 views
1

下面的腳本提供了任務,但我需要它運行另外20次左右。我怎樣才能優化腳本,所以我不需要一遍又一遍地調用相同的腳本?優化JQuery SmoothScroll腳本

「a1」變爲「a2」,「a3」等。 「#thing」也需要改變,因此它可以滾動到頁面

$(document).ready(function() { 
$(".a1").click(function() { 
     $('html,body').animate({ 
     scrollTop: $("#thing").offset().top 
    }, 500); 
}); 

回答

1

類似的東西可以做的工作,我認爲的另一部分:

var cnt = 0; 
$(".a1").click(function() { 
     $('html,body').animate({ 
     scrollTop: $("#schoonmaak").offset().top 
    }, 500, function() { 
     if (cnt++ > 20) $(".a1").click(); 
}); 

編輯 - 你的問題編輯取得了之後,我認爲這會解決你的問題:

var items = $('body ul li[id]'); 
$('.nobackground h2 a').each(function(i){ 

    $(this).click(function() { 
    $('html,body').animate({ scrollTop: $(items[i]).offset().top}, 500); 
    }); 

}); 
+0

謝謝,但我不清楚我想要什麼。 「a1」變爲「a2」,「a3」等。 「#schoonmaak」也變成了別的東西。 –

+0

檢查我的編輯 - 應該在你的情況下正常工作。 – Reflective

+0

你是最棒的!謝謝! –