我创建了一个脚本,可以确保文档中的元素滚动而不滚动其他任何内容,包括正文。这适用于浏览器以及移动设备/平板电脑。
// desktop scroll
$( '.scrollable' ).bind( 'mousewheel DOMMouseScroll', function ( e ) {
var e0 = e.originalEvent,
delta = e0.wheelDelta || -e0.detail;
this.scrollTop += delta * -1;
e.preventDefault();
});
var lastY;
var currentY;
// reset touch position on touchstart
$('.scrollable').bind('touchstart', function (e){
var currentY = e.originalEvent.touches[0].clientY;
lastY = currentY;
e.preventDefault();
});
// get movement and scroll the same way
$('.scrollable').bind('touchmove', function (e){
var currentY = e.originalEvent.touches[0].clientY;
delta = currentY - lastY;
this.scrollTop += delta * -1;
lastY = currentY;
e.preventDefault();
});