您可以在 InfoWindow 和 Map 上使用 Google maps 事件侦听器来禁用正文滚动。
var infowindow = new google.maps.InfoWindow();
// This event is fired when the <div> containing the InfoWindow's content is attached to the DOM.
google.maps.event.addListener(infowindow, 'domready', function () {
toggleBodyOverflow('hidden');
});
// This event is fired when the close button was clicked.
google.maps.event.addListener(infowindow, 'closeclick', function () {
toggleBodyOverflow('auto');
});
// This event is fired when the user clicks on the map (and therefore closes the InfoWindow).
google.maps.event.addListener(map, 'click', function () {
toggleBodyOverflow('auto');
});
为什么是地图click
事件监听器?因为有两种方法可以关闭信息窗口:使用关闭按钮——或者——点击地图。由于 InfoWindow 没有适当的close
事件,因此您需要同时使用这两个事件。
以及切换正文溢出的功能:
function toggleBodyOverflow(param) {
$('body').css({
overflow: param
});
}
JSFiddle demo