質問

私は、ボックス幅* N幅の絶対位置決め容器にN等サイズのボックスを持っています。これは隠れたオーバーフローを備えた比較的配置されたコンテナにあります。複数の行があります。左または右スワイプでは、ボックスコンテナの水平位置は、その最大位置(0,0)を超えずに、コンテナ左の位置(0,0)を超えず((BoxWidth * N)、0)

例

http://jsfiddle.net/rte8u/8/

これは現在ローリングしているものの簡単なバージョンです。ただし、CSS遷移が完了する前に2つのスワイプが行われた場合は、間違った「現在の」位置が参照されている場合に機能します。私はこれについて何をすることができますか?

HTML

<div class="widget a">
    <div class="overflow">
        <div class="box">1</div>
        <div class="box">2</div>
        <div class="box">3</div>
        <div class="box">4</div>
    </div>
</div>
<div class="widget b">
    <div class="overflow">
        <div class="box">1</div>
        <div class="box">2</div>
        <div class="box">3</div>
        <div class="box">4</div>
        <div class="box">5</div>
    </div>
</div>
<div class="widget c">
    <div class="overflow">
        <div class="box">1</div>
        <div class="box">2</div>
    </div>
</div>
<div class="widget d">
    <div class="overflow">
        <div class="box">1</div>
        <div class="box">2</div>
        <div class="box">3</div>
        <div class="box">4</div>
    </div>
</div>
.

CSS

* {
    margin:0;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.widget {
    width: 200px;
    position: relative;
    height: 100px;
    overflow: hidden;
}
.overflow {
    transition: all 0.5s ease-out;
    -o-transition: all 0.5s ease-out;
    -moz-transition: all 0.5s ease-out;
    -webkit-transition: all 0.5s ease-out;
    height: 100px;
    position:absolute;
    left: 0;
}
.box {
    user-select: none;
    -o-user-select: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    float: left;
    text-align:center;
    line-height: 100px;
    width: 200px;
    height: 100px;
    border: 1px solid #000;
}
.box:hover{
    cursor:pointer;
}
.

js

var fullbox = '200',
    overflows = [$('.a .overflow'),$('.b .overflow'),$('.c .overflow'),$('.d .overflow')];

// Set Widths of Overflow Boxes
for (var i=0; i < 4; i++){
    overflows[i].width(overflows[i].children().length * fullbox);   
}

// Swipe Handlers
$('.overflow').on({
    swipeleft: function () {
        if ($(this).position().left != '-' + ($(this).children().length - 1) * fullbox) {
            $(this).css('left', '-=' + fullbox);
        }
    },
    swiperight: function () {
        if ($(this).position().left !== 0) {
            $(this).css('left', '+=' + fullbox);
        }
    }
});
.

役に立ちましたか?

解決

ボックスが遷移している間は、おそらくジェスチャを止めたくないです。私が二度スワイプした場合、ユーザーエクスペリエンスのスタンドポイントから、ボックスが2倍の程度移動すると予想されます。使用できる1つのメソッドは、ボックスの位置を検出し、その位置に対して移動するのは、ユーザーがユーザーがどのインデックスのタブを保持することです。varまたはDOM自体にトラックを追跡します。

<div class="overflow" data-index="0">
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>
    <div class="box">4</div>
    <div class="box">5</div>
</div>
.

それからスワイプジェスチャーを変更してインデックスを追跡します。

$('.overflow').on({
  swipeleft: function () {
    var currentPosition = parseInt($(this).attr('data-index'));
    var totalBoxes = $(this).children('.box').length - 1;

    // Calculate the next position and ensure it doesn't pass the last box.
    var nextPosition = currentPosition + 1;
    if (nextPosition >= totalBoxes) {
        nextPosition = totalBoxes;
    }

    moveBox($(this), nextPosition);
  },
  swiperight: function () {
    var currentPosition = parseInt($(this).attr('data-index'));

    // Calculate the next position and ensure it doesn't pass the first box.
    var nextPosition = currentPosition - 1;
    if (nextPosition < 0) {
        nextPosition = 0;
    }

    moveBox($(this), nextPosition);
  }
});
.

移動ボックス機能:

function moveBox($ele, position) {
    var movePosition = parseInt(fullbox) * position;

    $ele
        .attr('data-index', position)
        .css('left', '-' + movePosition + 'px');
}
.

> http://jsfiddle.net/rte8u/11/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top