我正在尝试在按下某个控制按钮时修改默认光标图标。 虽然我在容器div上使用css部分成功,但这样做会覆盖移动光标状态,这是我不想要的。我的意思是在移动地图时不再出现移动图标(但在标记上时不会出现!)。
我想知道是否有一种非hacky方式通过api来实现特殊的游标行为而不需要重新设置所有内容。
这就是我试图做的,#map是传单地图的容器div。
#map[control=pressed] {
cursor: url('..custom.png');
}
答案 0 :(得分:31)
编辑5.18.2017:通过传单框架的原始CSS和Javascript(推荐)
我正在查看the BoxZoom plugin的源代码并注意到他们使用Leaflet's built-in DOM mutators的方法,并希望在此处进行推广......这无疑是最佳做法。
CSS中的某个地方包含了这样的类..
.leaflet-container.crosshair-cursor-enabled {
cursor:crosshair;
}
如果要启用十字准线,请在JS中执行此操作..
// Assumes your Leaflet map variable is 'map'..
L.DomUtil.addClass(map._container,'crosshair-cursor-enabled');
然后,当您想要禁用十字准线时,请在JS中执行此操作..
L.DomUtil.removeClass(map._container,'crosshair-cursor-enabled');
原始答案:地图级十字准线
@ scud42让我走上了正确的道路。您可以使用JQuery更改Leaflet映射游标,如下所示:
$('.leaflet-container').css('cursor','crosshair');
然后,当你想重置地图光标时,你可以这样做:
$('.leaflet-container').css('cursor','');
编辑1.21.2016:每个功能十字准线
您还可以为支持className
选项的单个要素启用十字准线,例如多边形或要素顶点等。
这是一个可拖动指针十字准线(jsfiddle)的可拖动顶点示例:
var svg_html_default = '<div style="margin:0px;padding:0px;height:8px;width:8px;border-style:solid;border-color:#FFFFFF;border-width:1px;background-color:#424242"</div>';
var default_icon = L.divIcon({
html: svg_html_default,
className: 'leaflet-mouse-marker',
iconAnchor: [5,5],
iconSize: [8,8]
});
var m = new L.marker([33.9731003, -80.9968865], {
icon: default_icon,
draggable: true,
opacity: 0.7
}).addTo( map );
m.on("mouseover",function(){$('.leaflet-mouse-marker').css('cursor','crosshair');});
m.on("mouseout",function(){$('.leaflet-mouse-marker').css('cursor','');});
答案 1 :(得分:9)
Leaflet的样式允许您更改某些游标行为。将它们放在您的本地CSS中进行更改。
/* Change cursor when mousing over clickable layer */
.leaflet-clickable {
cursor: crosshair !important;
}
/* Change cursor when over entire map */
.leaflet-container {
cursor: help !important;
}
答案 2 :(得分:3)
设置为十字准线:
document.getElementById('map').style.cursor = 'crosshair'
将其重置:
document.getElementById('map').style.cursor = ''
答案 3 :(得分:2)
使用active
伪类。
#map:active {
cursor: url('..custom.png');
}
<强> JSFiddle 强>
对于覆盖游标,您可能希望使用css3属性user-select: none
,以便在拖动元素时不会在文本和默认光标之间切换。该实现也在JSFiddle中显示。
答案 4 :(得分:1)
这对我有用:
// CSS first. Add this to leaflet stylesheet.
.leaflet-interactive.wait-cursor-enabled {
cursor: wait !important;
}
// JS select from map container and add class to each element
let map = L.map('map');
let els = map.getContainer().querySelectorAll('.leaflet-interactive');
for(let el of els){
el.classList += ' wait-cursor-enabled';
}
//JS remove class once no longer needed
let els = map.getContainer().querySelectorAll('.leaflet-interactive.wait-cursor-enabled');
for(let el of els){
el.classList.remove("wait-cursor-enabled");
}
答案 5 :(得分:0)
$('.leaflet-container').css('cursor','crosshair');