0

jqueryのscrollToプラグインを使用してこのスクロールの問題を誰かが助けてくれるかどうか疑問に思っていました.1回のクリックで一度に1つのh2要素をスクロールできるようにしたい...私のスクリプトでは、1回のクリックですべてのh2要素をスクロールします..助けてくださいみんなお願いします!!

 $(document).ready(function(){

 $('#down').click(function(){

  $("#container h2").each(function(i,h2){
   $("#wrap").scrollTo(h2, 800, { axis:'y' });

     });
    });

 $('#up').click(function(){

      $("#container h2").reverse().each(function(i,h2){
   $("#wrap").scrollTo(h2, 800, { axis:'y' });
     });
    });
 jQuery.fn.reverse = function() {

  return this.pushStack(this.get().reverse(), arguments);

};
});
4

1 に答える 1

0

さて、あなたは呼び出しているeach()ので、クリックごとにそれぞれにスクロールを実行しh2ます。渡した要素をスクロールして追跡し、次の要素にのみスクロールします。例:

 $(document).ready(function(){
     var next = 0;
     $('#down').click(function(){
        $("#wrap").scrollTo($("#container h2").eq(next), 800, { axis:'y' });
        next++;
     });
 });

アップデート:

You have to make sure that the counter does not increase or decrease to much. I changed it also a little to not always use the selector to find all the h2 elements. It is sufficient to get them once. The counter check should also fix the problem when you are at the last element and click down (resp. when you are at the first element and click up):

 $(document).ready(function(){
     var elements = $("#container h2");
     var next = 0;
     var max = elements.length;
     $('#down').click(function(){
        if(next+1 < max) {
            next++;
            $("#wrap").scrollTo(elements[next], 800, { axis:'y' });
        }
     });

     $('#up').click(function(){
        if(next-1 > -1) {
            next--;
            $("#wrap").scrollTo(elements[next], 800, { axis:'y' });
        }
     });
 });
于 2010-06-18T17:03:23.880 に答える