特に iPad で使用するための HTML/JS アプリを設計しています。アプリには、スクロール可能な要素がいくつかあります。
ドキュメントの幅と高さをそれぞれ 1024 と 768 に設定します。ビューポートを
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
次に、クラスを使用します
.scrollable {
overflow: auto;
-webkit-overflow-scrolling: touch;
}
スクロール可能なdivが適切にスクロールするようにします。最後に、JavaScript を少し使用して、スクロール可能な div とリストを許可しながら、ドキュメント自体のオーバースクロールを停止します。
$(document).on('touchmove',function(e){
e.preventDefault();
});
//uses body because jquery on events are called off of the element they are
//added to, so bubbling would not work if we used document instead.
$('body').on('touchstart','.scrollable',function(e) {
if (e.currentTarget.scrollTop === 0) {
e.currentTarget.scrollTop = 1;
} else if (e.currentTarget.scrollHeight === e.currentTarget.scrollTop + e.currentTarget.offsetHeight) {
e.currentTarget.scrollTop -= 1;
}
});
//prevents preventDefault from being called on document if it sees a scrollable div
$('body').on('touchmove','.scrollable',function(e) {
e.stopPropagation();
});
これはすべて機能します-ほとんど。ただし、問題が 1 つあります。スクロール可能な要素にスクロールが必要なほどのコンテンツが含まれていない場合、スクロールしようとすると、ドキュメント全体でオーバースクロールが開始されます。何百ものブログやその他の SO の質問を読みましたが、これに対する解決策が見つかりません。どんなアイデアでも大歓迎です。