【问题标题】:Raphael js-onclick action that changes color?remain that color until returning to click in otherRaphael js-onclick 更改颜色的操作?保持该颜色,直到返回单击其他
【发布时间】:2015-01-21 00:38:58
【问题描述】:

我评论了。我对这个库有疑问,因为这是我第一次使用它。花了几个小时寻找解决方案,但我没有找到。

我需要将鼠标悬停时的颜色更改为 X 颜色,并在没有鼠标悬停时返回原始颜色。

单击时更改为 Y 颜色并保持不变,直到您再次单击,鼠标悬停使其继续运行。

我一直被这个带有 svg 地图的例子指导:http://totaki.com/poesiabinaria/2014/10/crear-mapas-html5-interactivos-con-raphaeljs/

这是我的代码:

<link href="includes/css_includes/noticias_index.css" rel="stylesheet" type="text/css" />
 <head>
    <title>Ejemplo Raphaeljs</title>
    <script type="text/javascript" src="includes/js_includes/jquery.min.js"></script>
    <script type="text/javascript" src="includes/js_includes/raphael-min.js"></script>
  </head>

<div class="contenido_noti">

    <h1>RESULTADOS ELECCIONES 2012 - MUNICIPIOS</h1>

<center>
<table width="1180" border="0">
  <tr>
    <th scope="col">MAPA</th>
    <th scope="col" style="text-align:center">ESTADISTICAS</th>
  </tr>
  <tr>
    <th scope="col" width="">    <div id="lienzo">

    </div></th>
    <th scope="col" width="650" style="text-align:left">
    <div id="municipiotxt"><img id="loadingicon" src="img_main/selecciona.png" /></div>
    </th>
  </tr>
</table>
</center>
   <script>
      var municipios_data = {
    'ph1': 'Cadereyta de Montes',
    'ph2': 'Jalpan de Serra',
    'ph3': 'Colón',
    'ph4': 'Querétaro',
    'ph5': 'Pinal de Amoles',
    'ph6': 'Arroyo Seco',
    'ph7': 'Peñamiller',
    'ph8': 'El Marqués',
    'ph9': 'Tolimán',
    'ph10': 'Landa de matamoros',
    'ph11': 'Tequisquiapan',
    'ph12': 'Pedro Escobedo',
    'ph13': 'Ezequiel Montes',
    'ph14': 'San Joaquín',
    'ph15': 'Corregidora',
    'ph16': 'Huimilpan',
    'ph17': 'San Juan del Río',
    'ph18': 'Amealco de Bonfil'};

      var default_attributes = {
            fill: '#999999',
            stroke: '#000000',
            'stroke-width': 1,
        };  
      var $munictxt = $('#municipiotxt');

      $.ajax({
        url: 'includes/mapas/Mapa_muni.svg',
    type: 'GET',
    dataType: 'xml',
    success: function(xml) {
      var rjs = Raphael('lienzo', 570, 670);
      var corr="";
      $(xml).find('svg > g >  path').each(function() {
        var path = $(this).attr('d');
    var pid = $(this).attr('id');
    var pid_select="";
        var munic = rjs.path(path);

        munic.attr(default_attributes);
                /*funcion de hover*/
        munic.hover(function() {    


          this.animate({ fill: '#00bbff' });

        }, /*funcion al moverso mouse*/ function() {


          this.animate({ fill: default_attributes.fill});


        })      /*funcion de click*/ .click(function() {
         var muni_query=municipios_data[pid];
         muni_select=pid;
$("#municipiotxt").load("includes/querys_includes/mapa_muni_SVG_QUERY.php",{muni_query:muni_query});
   $munictxt.html(muni_select);
          this.animate({ fill: '#FF0000' });

        });
          });
    }
      });

    </script>

</div>

对不起,如果我的英语不好。

【问题讨论】:

    标签: javascript jquery html svg raphael


    【解决方案1】:

    让我给你一个简短的例子来说明你是如何做到的: http://jsfiddle.net/x0pv7580/

    我创建了所有需要的元素...

    var rect1 = paper.rect(20,30,100,12).attr({fill: "orange"});   
    ...
    

    并将它们推入一个数组:

    var elements = [rect1, rect2, rect3];
    ....
    

    之后,每个元素都会获得鼠标动作:

    for(var i = 0; i< elements.length; i++) {
      appendActionToElement(elements[i]);
    }
    

    这些是关键行动:

    function appendElement(element) {
    
    
      element.mouseover(function () {   //on mouseover change to color X
        this.attr('fill', 'green');
      });
    
      element.mouseout(function () {    //on mouseout change color depending on status
        if(!this.active)
          this.attr('fill', 'orange');
        else
          this.attr('fill', 'white');
      });
    
      element.click(function() {
        this.attr('fill', 'white');   //white should be your color Y
        this.active = true;           //set it active
        for(var i = 0; i< elements.length; i++) {
          if(elements[i] != this) {      //very important, on click we loop through all elements
            elements[i].active = false;  //and set them inactive and orange.
            elements[i].attr('fill', 'orange');
          }
    
        }
      });
    }
    

    【讨论】:

    • 谢谢!它帮助我遵循代码。实现在for-if中进行比较,但只比较变量而不访问对象或元素“municipios_data”来更改属性。
    【解决方案2】:

    谢谢!它帮助我遵循代码。实现在for-if中进行比较,但只比较变量而不访问对象或元素“municipios_data”来更改属性。

     <script>
          var municipios_data = {
        'ph1': 'Cadereyta de Montes',
        'ph2': 'Jalpan de Serra',
        'ph3': 'Colón',
        'ph4': 'Querétaro',
        'ph5': 'Pinal de Amoles',
        'ph6': 'Arroyo Seco',
        'ph7': 'Peñamiller',
        'ph8': 'El Marqués',
        'ph9': 'Tolimán',
        'ph10': 'Landa de matamoros',
        'ph11': 'Tequisquiapan',
        'ph12': 'Pedro Escobedo',
        'ph13': 'Ezequiel Montes',
        'ph14': 'San Joaquín',
        'ph15': 'Corregidora',
        'ph16': 'Huimilpan',
        'ph17': 'San Juan del Río',
        'ph18': 'Amealco de Bonfil'};
        /*----*/
          var default_attributes = {
                fill: '#999999',
                stroke: '#000000',
                'stroke-width': 1,
            };  
        /*----*/
          var $munictxt = $('#municipiotxt');
        /*----*/
    
          $.ajax({
            url: 'includes/mapas/Mapa_muni.svg',
        type: 'GET',
        dataType: 'xml',
        success: function(xml) {
          var rjs = Raphael('lienzo', 570, 670);
          var corr="";
          $(xml).find('svg > g >  path').each(function() {
            var path = $(this).attr('d');
        var pid = $(this).attr('id');
            var munic = rjs.path(path);
        munic.attr(default_attributes);
    
        munic.mouseover(function () {   //hover
       this.animate({ fill: '#00bbff' });
      });
    
      munic.mouseout(function () {    //mouse out
        if(!this.active)
       this.animate({ fill: default_attributes.fill});
        else
        this.animate({ fill: 'red' });
      });
    
      munic.click(function() {
    
             var muni_query=municipios_data[pid];
             var muni_select=pid;
    $("#municipiotxt").load("includes/querys_includes/mapa_muni_SVG_QUERY.php",{muni_query:muni_query});
       $munictxt.html(muni_select);
    
    
         this.animate({ fill: 'red' });  
        this.active = true;     
    
        for(var i = 1; i< 18; i++) {
            posicion = 'ph'+i;
            //alert(municipios_data[posicion])
            //alert(municipios_data[pid])
            alert(this)
          if(municipios_data[posicion] != municipios_data[pid]) {
              //alert("si")
    
    
            municipios_data[posicion].attr('fill', 'orange');
            municipios_data[posicion].active = false;
          }
    
        }
    
    
      });                           
              });                         
        }
          });
        </script>
    

    【讨论】:

      【解决方案3】:

      我不确定你想要什么,但我相信这就是你想要的。

      var ab = ["a", "b"];
      var p = Raphael(0,0,(ab.length * 150),300);
      function resetColor() {
        ab.map(function(notActive) {
          return notActive.attr({fill: "red"});
        });  
      }
      function colorChange(o) {
        o.attr({fill: "blue"})
      }
      for (i=0; i<ab.length; i++) {
        ab[i] = p.circle(75 + (i*150),150,75).attr({fill: "red"}).click(function(){
          active = this,
          resetColor(),
          this.attr({fill : "green"})
        }).hover(function(){
          this.attr({fill : "green"})
        }, function(){
          resetColor(),
          colorChange(active)
        });
      }
      

      更新:

      抱歉,我了解到您希望在单击后保持 mouseOver 运行 http://jsfiddle.net/crockz/k4sgk7sa/

      如果您想用活动颜色打破 mouseOver,请将点击功能上的绿色替换为蓝色

      this.attr({fill : "blue"})
      

      fiddle 仅供参考: http://jsfiddle.net/crockz/Ldkm0aju/

      【讨论】:

        猜你喜欢
        • 2021-05-02
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2013-04-12
        • 1970-01-01
        • 1970-01-01
        • 2017-10-05
        相关资源
        最近更新 更多