javascript 传单Js自定义控件按钮添加(文本、悬停)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31924890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:35:29  来源:igfitidea点击:

leaflet Js custom control button add (text, hover)

javascriptbuttonleaflet

提问by Jeremy Hunts

I followed this control-button-leaflet tutorialand it worked for me. Now I want to:

我遵循了这个控制按钮传单教程,它对我有用。现在我想:

  1. show some text when i hover over the button (like with the zoom buttons)
  2. Change the color of the button when i hover over it
  3. be able to write text inside the button instead of an image.
  1. 当我将鼠标悬停在按钮上时显示一些文本(如缩放按钮)
  2. 当我将鼠标悬停在按钮上时更改按钮的颜色
  3. 能够在按钮内写入文本而不是图像。

Here's the code:

这是代码:

    var customControl =  L.Control.extend({        
      options: {
        position: 'topleft'
      },

      onAdd: function (map) {
        var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');

        container.style.backgroundColor = 'white';     
        container.style.backgroundImage = "url(http://t1.gstatic.com/images?q=tbn:ANd9GcR6FCUMW5bPn8C4PbKak2BJQQsmC-K9-mbYBeFZm1ZM2w2GRy40Ew)";
        container.style.backgroundSize = "30px 30px";
        container.style.width = '30px';
        container.style.height = '30px';

        container.onclick = function(){
          console.log('buttonClicked');
        }

        return container;
      }
    });

    var map;

    var readyState = function(e){
      map = new L.Map('map').setView([48.935, 18.14], 14);
      L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
      map.addControl(new customControl());
    }

    window.addEventListener('DOMContentLoaded', readyState);

回答by Krxldfx

It seems you more need a Button than a div:

看起来你更需要一个按钮而不是一个 div:

    var container = L.DomUtil.create('input');
    container.type="button";
  1. Then you can easily set a mouseover text:

    container.title="No cat";
    
  2. And some Text instead of an image:

    container.value = "42";
    
  3. And you can use the mouse events to style the button:

    container.onmouseover = function(){
      container.style.backgroundColor = 'pink'; 
    }
    container.onmouseout = function(){
      container.style.backgroundColor = 'white'; 
    }
    
  1. 然后您可以轻松设置鼠标悬停文本:

    container.title="No cat";
    
  2. 还有一些文本而不是图像:

    container.value = "42";
    
  3. 您可以使用鼠标事件来设置按钮的样式:

    container.onmouseover = function(){
      container.style.backgroundColor = 'pink'; 
    }
    container.onmouseout = function(){
      container.style.backgroundColor = 'white'; 
    }
    

(you could of course do this last part with css, might be more elegant)

(你当然可以用 css 做这最后一部分,可能更优雅)

Full example: http://codepen.io/anon/pen/oXVMvy

完整示例:http: //codepen.io/anon/pen/oXVMvy