【发布时间】:2018-10-23 22:04:31
【问题描述】:
我有一个带有多个 SVG 叠加层的 Google 地图。但是当我在这些 SVG 上添加点击事件时,所有的叠加层都是可点击的,我希望它只适用于 SVG 路径。
这是一个小提琴 https://jsfiddle.net/gmkfhr9s/1/
我使用 Custom overlays 文档作为基础 https://developers.google.com/maps/documentation/javascript/customoverlays
USGSOverlay.prototype.onAdd = function() {
var div = document.createElement('div');
div.style.borderStyle = 'dotted';
div.style.borderWidth = '2px';
div.style.borderColor = 'white';
div.style.position = 'absolute';
// Create the img element and attach it to the div.
var img = document.createElement('img');
img.src = this.image_;
img.style.width = '100%';
img.style.height = '100%';
img.style.position = 'absolute';
div.appendChild(img);
this.div_ = div;
// Add the element to the "overlayLayer" pane.
var panes = this.getPanes();
panes.overlayLayer.appendChild(div);
//add element to clickable layer
this.getPanes().overlayMouseTarget.appendChild(div);
google.maps.event.addDomListener(img, 'mouseover', function() {
img.style.opacity = '0.5';
});
google.maps.event.addDomListener(img, 'mouseout', function() {
img.style.opacity = '1';
});
};
您可以看到,通过 mouseover 事件,所有覆盖(带边框的框)都被选中,并且只有 SVG。
是否可以只使 SVG 可点击?
如果这对您有帮助的话,这个thread 是一个类似的问题。
编辑:
@Sphinxxx 的回答效果很好。
我只想补充一点,如果您有多个 SVG,其中一些高于其他 SVG,则必须添加此 CSS 才能点击它们。
svg {
pointer-events: none;
}
path {
pointer-events: all;
}
【问题讨论】:
标签: javascript google-maps svg google-maps-api-3