【发布时间】:2014-09-24 00:16:32
【问题描述】:
我在一个绝对定位的 boxWidth * n 宽度容器中有 n 个大小相等的盒子。这位于一个相对定位的容器中,具有隐藏的溢出。有多行。在向左或向右滑动时,盒子容器的水平位置将在滑动方向上从容器左侧位置增加或减少 1 个 boxWidth,但不会超过其最大位置 (0, 0) 和 ((boxWidth * 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);
}
}
});
【问题讨论】:
标签: jquery css jquery-ui jquery-mobile css-transitions