我试图更改鼠标悬停时多面要素的不透明度。我可以在鼠标悬停时使用该功能并更改样式,但是无法对不透明度从0.3变为0.8的过程进行动画处理。
我已经阅读了文档,但找不到任何东西...
有任何线索吗?
答案 0 :(得分:2)
当唯一的更改是样式函数被调用时所经过的时间时,功能不会自动重新渲染,但是调用feature.changed()将触发另一个渲染。
var map = new ol.Map({
target: document.getElementById('map'),
view: new ol.View({
center: ol.proj.fromLonLat([0, 50]),
zoom: 7,
})
});
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature(
ol.geom.Polygon.fromExtent([-1, 50, 1, 51]).transform('EPSG:4326', map.getView().getProjection())
)
]
})
});
var selectedStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
width: 2,
color: 'blue'
}),
fill: new ol.style.Fill()
});
var start;
map.addLayer(layer);
var select = new ol.interaction.Select({
condition: ol.events.condition.pointerMove,
style: function(feature) {
var elapsed = new Date().getTime() - start;
var opacity = Math.min(0.3 + elapsed/10000, 0.8);
selectedStyle.getFill().setColor('rgba(255,0,0,' + opacity + ')');
feature.changed();
return selectedStyle;
}
});
select.on('select', function(){ start = new Date().getTime(); });
map.addInteraction(select);
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
<div id="map" class="map"></div>