2021-02-06 04:42:36 +00:00
---
id: 587d7faa367417b2b2512bd4
2021-09-24 06:31:25 -07:00
title: Añade un efecto "Hover" a un elemento D3
2021-02-06 04:42:36 +00:00
challengeType: 6
forumTopicId: 301469
dashedName: add-a-hover-effect-to-a-d3-element
---
# --description--
2021-09-24 06:31:25 -07:00
Es posible añadir efectos que resalten una barra cuando el usuario pasa mouse sobre ella. Hasta ahora, el estilo de los rectángulos se aplica con los métodos incorporados en D3 y SVG, pero también puedes usar CSS.
2021-02-06 04:42:36 +00:00
2021-10-12 08:20:30 -07:00
Tú estableces la clase CSS en los elementos SVG con el método `attr()` . Después la pseudo-clase `:hover` para tu nueva clase mantiene las reglas de estilo para cualquier efecto hover.
2021-02-06 04:42:36 +00:00
# --instructions--
2021-09-24 06:31:25 -07:00
Utiliza el método `attr()` para añadir una clase de `bar` a todos los elementos `rect` . Esto cambia el color de (relleno) `fill` de la barra a marrón cuando pases el mouse sobre ella.
2021-02-06 04:42:36 +00:00
# --hints--
2021-09-24 06:31:25 -07:00
Tu elemento `rect` debe tener una clase de `bar` .
2021-02-06 04:42:36 +00:00
```js
2021-07-09 21:23:54 -07:00
assert($('rect').attr('class').trim().split(/\s+/g).includes('bar'));
2021-02-06 04:42:36 +00:00
```
# --seed--
## --seed-contents--
```html
< style >
.bar:hover {
fill: brown;
}
< / style >
< body >
< script >
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
const w = 500;
const h = 100;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - 3 * d)
.attr("width", 25)
.attr("height", (d, i) => 3 * d)
.attr("fill", "navy")
// Add your code below this line
// Add your code above this line
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text((d) => d)
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - (3 * d) - 3);
< / script >
< / body >
```
# --solutions--
```html
< style >
.bar:hover {
fill: brown;
}
< / style >
< body >
< script >
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
const w = 500;
const h = 100;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - 3 * d)
.attr("width", 25)
.attr("height", (d, i) => 3 * d)
.attr("fill", "navy")
// Add your code below this line
.attr('class', 'bar')
// Add your code above this line
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text((d) => d)
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - (3 * d) - 3);
< / script >
< / body >
```