CSS 전환이 완료 될 때까지 스 와이프 제스처를 비활성화하십시오
-
26-12-2019 - |
문제
B 박스의 절대적으로 배치 된 용기에 똑같이 크기의 상자가 있습니다. n width.이것은 숨겨진 오버플로가있는 비교적 위치 된 컨테이너에 있습니다.여러 행이 있습니다.왼쪽 또는 오른쪽 스 와이프에서 상자 컨테이너의 수평 위치는 최대 위치 (0, 0)를 초과하지 않고 스 와이프 방향의 컨테이너 왼쪽 위치에서 1 박스 폭을 첨가하거나 뺍니다. ((박스 폭드 * n), 0)
이것은 현재 롤링하고있는 것의 간단한 버전이며, CSS 전환이 완료되기 전에 두 개의 스 와이프가 작성된 경우 잘못된 '현재'위치가 참조됩니다.이것에 대해 무엇을 할 수 있습니까?
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);
}
}
});
. 해결책
상자가 전환되는 동안 제스처를 멈추고 싶지 않을 것입니다.사용자 경험 스탠드 포인트에서 두 번 짜 렀을 경우 상자가 두 번 멀리 이동할 것으로 기대할 것입니다.사용할 수있는 한 가지 방법은 상자 위치를 감지하고 위치에 상대적으로 이동하는 대신 사용자가 인덱스가있는 탭을 유지하는 것입니다. 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');
}
.
제휴하지 않습니다 StackOverflow