我認爲這是因爲包含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