How do I change the default cursor in leaflet maps?

前端 未结 6 2024
独厮守ぢ
独厮守ぢ 2020-12-14 17:02

I am trying to modify the default cursor icon when a certain control button is pressed. Although I was partially successful by using css on the container div, doing this ove

6条回答
  •  被撕碎了的回忆
    2020-12-14 17:48

    Edit 5.18.2017: Raw CSS and Javascript via Leaflet Framework (recommended)

    I was looking through the source code for the BoxZoom plugin and noticed their approach using Leaflet's built-in DOM mutators and wanted to promote it here...this is certainly the best practice.

    Example jsfiddle

    Somewhere in your CSS include a class like this..

    .leaflet-container.crosshair-cursor-enabled {
        cursor:crosshair;
    }
    

    When you want to enable crosshairs, do this in your JS..

    // Assumes your Leaflet map variable is 'map'..
    L.DomUtil.addClass(map._container,'crosshair-cursor-enabled');
    

    Then, when you want to disable crosshairs, do this in your JS..

    L.DomUtil.removeClass(map._container,'crosshair-cursor-enabled');
    

    Original Answer: Map-level Crosshairs

    @scud42 got me on the right path. You can use JQuery to change the Leaflet map cursor like this:

    $('.leaflet-container').css('cursor','crosshair');
    

    Then later, when you want to reset the map cursor, you can do this:

    $('.leaflet-container').css('cursor','');
    

    Edit 1.21.2016: Per-feature Crosshairs

    You can also enable crosshairs for individual features supporting the className option, such as a polygon, or feature vertices, etc.

    Here's an example of a draggable vertice that will toggle pointer crosshairs (jsfiddle):

    var svg_html_default = '
    '; 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','');});

提交回复
热议问题