简体   繁体   English

Google Chrome 84 + Google Maps InfoWindow:点击关闭按钮时防止网页跳转

[英]Google Chrome 84 + Google Maps InfoWindow: Prevent webpage jump when clicking close button

There seems to be an odd behavior in Google Chrome v84 when closing an InfoWindow in a Google Map embedded in a webpage using the JavaScript API:在使用 JavaScript API 嵌入网页中的 Google Map 中关闭 InfoWindow 时,Google Chrome v84 中似乎有一个奇怪的行为:

Whenever the page is not scrolled to the top, clicking the close button on an InfoWindow makes the scroll position of the page jump by 15px down.每当页面没有滚动到顶部时,单击 InfoWindow 上的关闭按钮会使页面的滚动 position 向下跳15px

This did not happen in Version 83 and lower.这在版本 83 及更低版本中没有发生。

Here is a simple reproducible example.这是一个简单的可重现示例。 Scroll the page down until the map is in the viewport, click the marker, close the Infowindow, the page will scroll down by 15px .向下滚动页面直到 map 在视口中,单击标记,关闭信息窗口,页面将向下滚动15px

 function initialize() { const mapOptions = { zoom: 5, mapTypeId: google.maps.MapTypeId.ROADMAP, center: new google.maps.LatLng(0, 0) }; const map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); const marker = new google.maps.Marker({ position: new google.maps.LatLng(0, 0), map: map, title: 'Nothing here' }); const infowindow = new google.maps.InfoWindow({ content: 'Hello World' }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map, marker); }); google.maps.event.addListener(infowindow, 'closeclick', function() { displayVerticalOffset(); }); window.onscroll = function() { displayVerticalOffset(); }; } function displayVerticalOffset() { document.getElementById('pageYOffset').innerHTML = 'Vertical offset: ' + window.pageYOffset + 'px' }
 #map-canvas { margin-top: 800px; height: 180px; width: 180px; } #pageYOffset { margin-bottom: 800px; }
 <div id="map-canvas"></div> <div id="pageYOffset"></div> <.-- Replace the value of the key parameter with your own API key: --> <script src="https.//maps.googleapis?com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize" async defer></script>

add overflow-anchor:none to parent, here is将溢出锚:无添加到父级,这里是

#map-canvas {
  overflow-anchor:none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM