【发布时间】:2017-01-26 22:23:55
【问题描述】:
有没有一种简单的方法可以让 Leaflet 地图中的标记闪烁?我的意思是动画闪烁 - 类似于在 1 秒内从不透明度 1.0 到不透明度 0.5 的过渡循环,然后反转,循环结束。
【问题讨论】:
-
我在我的项目中使用 animate.css。可以使用闪烁效果。
有没有一种简单的方法可以让 Leaflet 地图中的标记闪烁?我的意思是动画闪烁 - 类似于在 1 秒内从不透明度 1.0 到不透明度 0.5 的过渡循环,然后反转,循环结束。
【问题讨论】:
当您添加Marker 时,您可以指定Icon - 其中的选项包括className。您可以使用 className 选项通过 CSS 为标记的图标设置动画。
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
maxZoom: 18
}).addTo(map);
L.marker([51.5, -0.09], {
icon: L.icon({
iconUrl: 'https://unpkg.com/leaflet@1.0.3/dist/images/marker-icon.png',
className: 'blinking'
})
}).addTo(map);
#map {
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
}
@keyframes fade {
from { opacity: 0.5; }
}
.blinking {
animation: fade 1s infinite alternate;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<div id="map"></div>
要将标记从闪烁切换为不闪烁,您可以使用 Leaflet 的 DomUtil 将 blinking 类添加到标记的 img 元素:
// With the class added, the marker will blink:
L.DomUtil.addClass(marker._icon, "blinking");
// Without the class, it won't:
L.DomUtil.removeClass(marker._icon, "blinking");
【讨论】:
setIcon来更改标记的图标,所以只要您需要在非闪烁图标和闪烁图标之间进行切换,只需调用它即可。