좋아, 일종의 해커지만, 그런 식으로 뭔가를 달성하는 방법의 데모로 그것을 참조하십시오. 변수 selected
을 만들고 click
핸들러가 실행되면 강조된 기능을 저장합니다. 이제 mouseout
처리기에서 선택이 이루어 졌는지 확인하고 mouseout
을 실행 한 레이어와 일치하는지 확인한 다음 강조 표시된 스타일을 제거하지 마십시오. 또한 click
핸들러에 일부 논리를 작성해야 선택이 이미 이루어진 경우 하이라이트가 제거됩니다.
코드 예제 : http://plnkr.co/edit/fdTnA9CyJdJejiPq2q8M?p=preview
당신은 또한 당신이 달성하고 거기에하려고하는지도 click
또는 moveend
이든 UX 용 핸들러를 작성해야합니다 : 여기
function highlight (layer) {
layer.setStyle({
weight: 5,
dashArray: ''
});
if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
}
function dehighlight (layer) {
if (selected === null || selected._leaflet_id !== layer._leaflet_id) {
geojson.resetStyle(layer);
}
}
// Variable to store selection
var selected = null;
function select (layer) {
// See if there is already a selection
if (selected !== null) {
// Store for now
var previous = selected;
}
map.fitBounds(layer.getBounds());
// Set new selection
selected = layer;
// If there was a previous selection
if (previous) {
// Dehighlight previous
dehighlight(previous);
}
}
var geojson = L.geoJson(rower, {
style: function (feature) {
return {
weight: 2,
opacity: 1,
color: feature.properties.colour,
dashArray: 3,
};
},
onEachFeature: function (feature, layer) {
layer.on({
'mouseover': function (e) {
highlight(e.target);
},
'mouseout': function (e) {
dehighlight(e.target);
},
'click': function (e) {
select(e.target);
}
});
}
}).addTo(map);
는 Plunker에 작업 예제 선택 항목이 있는지 확인한 다음 강조 표시를 제거하십시오. 그러나 개념을 이해하면 오히려 간단합니다.
출처
2015-01-28 11:22:23
iH8
당신이 무엇을하려했는지 보여줄 필요가 있습니다. 더 많은 것을 읽어보십시오 : http://stackoverflow.com/help/how-to-ask – knowbody
하이라이트를 유지하기 위해서, 당신은 클래스 토글 링을 사용할 수 있다고 생각합니다 (호버에 'toggleClass') 클릭하면 'addClass') – flks