javascript - React-Leaflet 在鼠标悬停/鼠标移出时设置单个椭圆的填充不透明度
问题描述
我有一个使用React-Leaflet创建的地图,显示使用Leaflet.ellipse和React-Leaflet Core API创建的椭圆。
我将鼠标事件处理程序传递给椭圆的eventHandlers道具。当我将鼠标悬停在椭圆上时,fillOpacity
从 0.0 变为 0.5,然后在鼠标移出时从 0.5 变为 0.0。
问题是当事件被触发时,fillOpacity
每个椭圆的 变化而不仅仅是被悬停的椭圆。我怎样才能使它只有被悬停的特定椭圆才会fillOpacity
改变?
地图组件
const Map = () => {
const [map, setMap] = useState(null);
const [fillOpacity, setFillOpacity] = useState(0)
const onMouseEvent = (event, type) => {
switch (type) {
case 'over':
setFillOpacity(0.5)
break
case 'out':
setFillOpacity(0.0)
break
default:
break
}
}
return (
<>
<MapContainer
center={[38, -82]}
zoom={4}
zoomControl={false}
style={{ height: "100vh", width: "100%", padding: 0 }}
whenCreated={map => setMap(map)}
>
<LayersControl position="topright">
<LayersControl.BaseLayer checked name="Map">
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url={maps.base}
/>
</LayersControl.BaseLayer>
<LayersControl.Overlay checked name="Ellipses">
<FeatureGroup>
<>{CITIES.map((city, index) => {
return (
<Ellipse
key={index}
center={city.center}
radii={city.radii}
tilt={city.tilt}
pathOptions={{
fillOpacity,
opacity: 1,
weight: 2,
}}
eventHandlers={{
mouseover: (event, type) => onMouseEvent(event, 'over'),
mouseout: (event, type) => onMouseEvent(event, 'out'),
}}
>
<Popup>
<Typography variant='subtitle1'>{city.city}</Typography>
</Popup>
</Ellipse>
)
})}</>
</FeatureGroup>
</LayersControl.Overlay>
</LayersControl>
</MapContainer>
</>
);
};
椭圆组件
import { createPathComponent } from '@react-leaflet/core'
import L from 'leaflet'
import 'leaflet-ellipse'
const Ellipse = createPathComponent(createEllipse, updateEllipse)
function createEllipse(props, context) {
const { center, radii, tilt, options } = props
const instance = new L.Ellipse(center, radii, tilt, options)
return {
instance,
context: { ...context, overlayContainer: instance },
}
}
function updateEllipse(instance, props, prevProps) {
if (
props.center !== prevProps.center ||
props.radii !== prevProps.radii ||
props.tilt !== prevProps.tilt ||
props.options !== prevProps.options
) {
instance.setStyle(props.options)
instance.setLatLng(props.center)
instance.setRadius(props.radii)
instance.setTilt(props.tilt)
}
}
export default Ellipse
解决方案
问题是您对所有省略号都使用相同的状态变量。这里:
{CITIES.map((city, index) => {
return (
<Ellipse
key={index}
pathOptions={{
fillOpacity. // <---- problem here
}}
>
...
</Ellipse>
)
})}
因此,所有fillOpacity
椭圆都从状态变量中获取不透明度。当您将鼠标悬停在其中任何一个上时,状态变量会发生变化,并且它们都会做出响应。你最好不要将不透明度作为状态变量来处理。只需将其设置为常量 0,然后在事件处理程序中,深入到target
鼠标悬停的位置,然后调用setStyle
:
const onMouseEvent = (event, type) => {
switch (type) {
case 'over':
event.target.setStyle({ fillOpacity: 0.5 })
break
case 'out':
event.target.setStyle({ fillOpacity: 0.0 })
break
default:
break
}
}
所以现在每个椭圆都直接通过传单方法管理自己的不透明度,而不是全部通过应用过于广泛的反应状态变量来继承它们的不透明度。
工作演示
推荐阅读
- mysql - 一年中的定制周(今年 5 天或更多天)
- google-sheets - 谷歌表单数据分析
- javascript - Vue.js:延迟后运行方法,但如果用户输入发生则重置延迟
- c# - INotifyPropertyChanged 在可重置视图模型中不起作用
- r - 如何解决 R 中 ggplot 的多个错误栏问题?
- python - 遍历行并保存为 csv
- python - 在python中查询字典
- javascript - 如何使用 While 循环继续更改元素
- javascript - 如何在 HTML 中使用来自外部文件的 Jquery 对象?
- file - 需要帮助理解一个简短的批处理脚本(可能是骗局)