0

jQueryタッチスワイプを使用して各アイテムをクリックしようとしています。これは私のHTMLです

<div class="slider-active" id="active_885">
<label for="slide1-885"></label>
<label for="slide2-885"></label>
<label for="slide3-885"></label>        
</div>

これは私のスクリプトです

jQuery("#slides_885").swipe({
  swipeRight:function(event, direction, distance, duration, fingerCount) {
  jQuery("#active_885 label:first").click();
  },
  swipeLeft:function(event, direction, distance, duration, fingerCount) {
  jQuery("#active_885 label:last").click();
  }
});

私のJavaScriptの知識がほとんどないので、右にスワイプすると最初のラベルをクリックし、左にスワイプすると最後のラベルをクリックするように上記のスクリプトを作成できます。しかし、私が望むのは、スクリプトがラベル(上記のhtmlに示されている)を順番にクリックするようにすることです。右にスワイプするたびに1つずつ下にスワイプし、左にスワイプして上にスワイプします。

ポイントを説明できれば幸いです。何か助けて??

4

1 に答える 1

2

アイテムの配列を作成し、<label>スワイプに基づいてそれを繰り返すことができます。

このようなもの:

HTML:

<div class="slider-active" id="active_885">
    <label class="swipeLabel" for="slide1-885"></label>
    <label class="swipeLabel" for="slide2-885"></label>
    <label class="swipeLabel" for="slide3-885"></label>        
</div>

JavaScript:

var active885Labels = jQuery('label.swipeLabel'),
    current885Label = -1;

jQuery("#slides_885").swipe({
    swipeRight: function(event, direction, distance, duration, fingerCount) {
                    if (current885Label < active885Labels.length) {
                        current885Label++;
                        active885Labels[current885Label].click()
                    }
                    else
                    {
                        //we are already at the last label, either wrap around or do nothing, or click the last label again, or do something else
                    }
                },
    swipeLeft: function(event, direction, distance, duration, fingerCount) {
                    if (current885Label > 0) {
                        current885Label--;
                        active885Labels[current885Label].click()
                    }
                    else
                    {
                        //we are already at the first label, either wrap around or do nothing, or click the first label again, or do something else
                    }
                }
});
于 2012-12-08T18:32:03.347 に答える