我认为这是因为包含pan事件的元素被移出屏幕。
因此而不是添加上泛的回调函数来设置.gallery-inner
像下面的left
CSS值:
<script>
var startPos = 0;
var scrollContainer = document.getElementById('scrollable');
var hammertime = new Hammer(scrollContainer);
hammertime.on('panend', function(ev) {
startPos = parseInt(scrollContainer.style.left.replace('px', ''));
});
hammertime.on('pan', function(ev) {
var pos = parseInt(startPos) + ev.deltaX;
scrollContainer.style.left = pos + 'px';
});
</script>
<style>
.gallery {
overflow: hidden;
height: 200px;
width: 300px;
position: relative;
}
.gallery-inner {
position: relative;
white-space: nowrap;
overflow: visible;
}
.gallery-inner > div {
display: inline-block;
width: 50px;
height: 200px;
background: #000;
color: #fff;
}
</style>
<div class="gallery">
<div id="scrollable" class="gallery-inner">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
<div>13</div>
</div>
</div>
添加另一个容器的触摸区域,并在其上设置left
值。
例如。
<script>
var startPos = 0;
var hammerContainer = document.getElementById('hammer');
var scrollContainer = document.getElementById('scrollable');
var hammertime = new Hammer(hammerContainer);
hammertime.on('panend', function(ev) {
startPos = parseInt(scrollContainer.style.left.replace('px', ''));
});
hammertime.on('pan', function(ev) {
var pos = parseInt(startPos) + ev.deltaX;
scrollContainer.style.left = pos + 'px';
});
</script>
<style>
.gallery {
overflow: hidden;
height: 200px;
width: 300px;
position: relative;
}
.gallery-scrollable {
position: relative;
white-space: nowrap;
overflow: visible;
}
.gallery-scrollable > div {
display: inline-block;
width: 50px;
height: 200px;
background: #000;
color: #fff;
}
</style>
<div class="gallery">
<div id="hammer" class="gallery-inner">
<div id="scrollable" class="gallery-scrollable">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
<div>13</div>
</div>
</div>
</div>
代码示例在这里:http://codepen.io/anon/pen/QbgLzz
这方面有任何见解? – 2015-04-13 12:18:02
我通过在其中一个父元素上初始化锤子对象来解决此问题。基本上我没有改变除选择器以外的任何代码,问题就没有了。 – janhocevar 2015-04-13 13:02:32
让我试试这个 – 2015-04-13 13:05:05