How do I change the default cursor in leaflet maps?

前端 未结 6 1990
独厮守ぢ
独厮守ぢ 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:23
    $('.leaflet-container').css('cursor','crosshair');
    
    0 讨论(0)
  • 2020-12-14 17:24

    This is what worked for me:

    // 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");
    }
    
    0 讨论(0)
  • 2020-12-14 17:37

    Set to crosshair:

    document.getElementById('map').style.cursor = 'crosshair'
    

    Reset it back:

    document.getElementById('map').style.cursor = ''
    
    0 讨论(0)
  • 2020-12-14 17:42

    Use the active pseudo class.

    #map:active {
        cursor: url('..custom.png');
    }
    

    JSFiddle

    For overriding a cursor you will probably want to use the css3 attribute user-select: none so that it doesn't toggle between the text and default cursor when dragging on the element. That implementation is also shown in the JSFiddle.

    0 讨论(0)
  • 2020-12-14 17:47

    Leaflet's styles allow you to change some cursor behavior. Put these in your local CSS to make the change.

    /* Change cursor when mousing over clickable layer */
    .leaflet-clickable {
      cursor: crosshair !important;
    }
    /* Change cursor when over entire map */
    .leaflet-container {
      cursor: help !important;
    }
    
    0 讨论(0)
  • 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 = '<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','');});
    
    0 讨论(0)
提交回复
热议问题