我评论道。我有这个库的问题,因为这是我第一次使用它。花了几个小时寻找解决方案,但我找不到它。
我需要将鼠标悬停时的颜色更改为X颜色,并在没有鼠标悬停时更改为原始颜色。
单击更改为Y颜色并保持不变,直到再次单击,鼠标悬停继续运行。
我受到了这个示例whit svg map的指导: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>
对不起,如果我的英语不好。
答案 0 :(得分:0)
让我简单举例说明一下如何做到这一点: 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');
}
}
});
}
答案 1 :(得分:0)
谢谢!它帮助我遵循代码。实现在for-if中进行比较,但只比较变量不能访问对象或元素&#34; municipios_data&#34;改变属性。
<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>
答案 2 :(得分:0)
我不确定你想要什么,但我相信这就是你要找的东西。
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"})
小提琴仅供参考: http://jsfiddle.net/crockz/Ldkm0aju/