Jquery ScrollTop将ID + SetTimeout倍增

这是我的情况,我有一个页面与mutiples div / id创建从php while循环,基于文件夹上存在的pdf文件的数量。 我的目标是每5秒滚动到每个ID,然后从头开始重新滚动。 以下代码工作正常,但问题是,如果我将5个新文件添加到该文件夹​​,那么我需要复制五次该function,我要找的是滚动以便文档上的所有可用ID。 有没有办法写一个函数来做到这一点? 任何灯都将不胜感激。

这是我的实际代码:

$(document).ready(function () { function loopanchors() { setTimeout(function () { $('html, body').animate({ 'scrollTop': $('#1').offset().top }, 2000); }, ); setTimeout(function () { $('html, body').animate({ 'scrollTop': $('#2').offset().top }, 2000); }, ); setTimeout(function () { $('html, body').animate({ 'scrollTop': $('#3').offset().top }, 2000); }, ); setTimeout(function () { $('html, body').animate({ 'scrollTop': $('#4').offset().top }, 2000); }, ); setTimeout(function () { $('html, body').animate({ 'scrollTop': $('#5').offset().top }, 2000); }, ); setTimeout(function () { $('html, body').animate({ 'scrollTop': $('#6').offset().top }, 2000); }, ); setTimeout(function () { $('html, body').animate({ 'scrollTop': $('#7').offset().top }, 2000); }, ); x = setTimeout(function () { loopanchors() }, ); //will loop every 5 seconds. } loopanchors(); //start it up the first time }); 

尝试此代码根据您的需要调整$ pdf_time和totalPDF: