gpt4 book ai didi

jquery - 在 CSS 转换完成之前禁用滑动手势

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:49 24 4
gpt4 key购买 nike

我在 boxWidth * n 宽度的绝对定位容器中有 n 个大小相同的盒子。这位于一个相对定位的容器中,带有隐藏的溢出。有多行。在向左或向右滑动时,盒子容器的水平位置将在滑动方向上从容器左侧位置增加或减去 1 boxWidth,而不超过其最大位置 (0, 0) 和 ((boxWidth * n), 0)

example

http://jsfiddle.net/rTe8U/8/

这是我目前正在使用的一个简单版本,它可以工作,但是,如果在 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);
}
}
});

最佳答案

您可能不想在框转换时停止手势。从用户体验的 Angular 来看,如果我滑动两次,我可能希望盒子移动两倍远。您可以使用的一种方法不是检测框的位置,而是相对于该位置移动,而是记录用户所在的索引。要么在 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/

关于jquery - 在 CSS 转换完成之前禁用滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25069291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com