Dưới Mobile Safari, có thể cho phép một vị trí hoàn toàn được định vị div
để cuộn mà không cho phép toàn bộ trang cuộn lên và xuống khi cuộn đến các cạnh (cuộn đàn hồi)?ngăn di chuyển toàn trang iOS
Dưới đây là một ví dụ làm việc tối thiểu của vấn đề tôi phải đối mặt:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#a, #b {
position: absolute;
top: 0;
left: 0;
height: 100%;
padding: 10px;
overflow: auto;
}
#a {
width: 80px;
background: #f00;
}
#b {
background: #00f;
left: 80px;
width: 100%;
}
</style>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
function pdcb(e) {
e.preventDefault();
}
function npcb(e) {
e.stopPropagation();
}
$(document).on('touchstart touchmove', pdcb).
on('touchstart touchmove', '.scrollable', npcb);
</script>
</head>
<body>
<div id="a" class="scrollable">
This<br>
should<br>
be<br>
scrollable<br>
but<br>
not<br>
scroll<br>
the<br>
whole<br>
page<br>
This<br>
should<br>
be<br>
scrollable<br>
but<br>
not<br>
scroll<br>
the<br>
whole<br>
page<br>
This<br>
should<br>
be<br>
scrollable<br>
but<br>
not<br>
scroll<br>
the<br>
whole<br>
page<br>
This<br>
should<br>
be<br>
scrollable<br>
but<br>
not<br>
scroll<br>
the<br>
whole<br>
page<br>
This<br>
should<br>
be<br>
scrollable<br>
but<br>
not<br>
scroll<br>
the<br>
whole<br>
page<br>
</div>
<div id="b">
this should never scroll
</div>
</body>
</html>
Giải pháp:
$(document).on('touchmove', function(e) {
e.preventDefault();
}).ready(function() {
$(".scrollable").on('touchstart', function(e) {
this.allowUp = (this.scrollTop > 0);
this.allowDown = (this.scrollTop < this.scrollHeight - this.clientHeight);
this.prevTop = null;
this.prevBot = null;
this.lastY = e.originalEvent.pageY;
}).on('touchmove', function(e) {
var event = e.originalEvent;
var up = (event.pageY > this.lastY), down = !up;
this.lastY = event.pageY;
if ((up && this.allowUp) || (down && this.allowDown))
event.stopPropagation();
else
event.preventDefault();
});
});
không, không phải là một trùng lặp. có hai thay đổi 'div'. –
Mọi người đánh dấu mục này là trùng lặp - Đây không phải là câu hỏi trùng lặp - câu hỏi chỉ đề cập đến việc ngăn chặn động lượng gốc tự động trong iOS - câu hỏi/giải pháp này liên quan đến việc cho phép di chuyển động lượng trên các div cụ thể trong khi chặn cuộn trên toàn bộ trang . Hữu ích cho các ứng dụng web với nhiều yếu tố vị trí cố định. – 1nfiniti
Nếu bạn muốn tính năng này hoạt động với các phần tử được thêm động (như trong một ứng dụng JS trang đơn), thay thế '$ (". Scrollable "). ('Touchstart', ...)' for '$ (document.body) .on ('touchstart', '.scrollable', ...) '. – micho