I'm learning how to deal with SVG,
My question is the following, I have 10 balls in SVG and 10 links. My idea is to click on a link and highlight a ball by taking the highlight of the others.
I'm learning how to deal with SVG,
My question is the following, I have 10 balls in SVG and 10 links. My idea is to click on a link and highlight a ball by taking the highlight of the others.
For purposes of demonstrating how to achieve what you want, "Highlight" the act of giving a different color to the circle in question.
In order to create your context, we will use the following markup where we have an SVG with 10 circles:
<svg id="circulos" width="720" height="120">
<circle id="circle0" cx="20" cy="60" r="10"></circle>
<circle id="circle1" cx="60" cy="60" r="10"></circle>
<circle id="circle2" cx="100" cy="60" r="10"></circle>
<circle id="circle3" cx="140" cy="60" r="10"></circle>
<circle id="circle4" cx="180" cy="60" r="10"></circle>
<circle id="circle5" cx="220" cy="60" r="10"></circle>
<circle id="circle6" cx="260" cy="60" r="10"></circle>
<circle id="circle7" cx="300" cy="60" r="10"></circle>
<circle id="circle8" cx="340" cy="60" r="10"></circle>
<circle id="circle9" cx="380" cy="60" r="10"></circle>
</svg>
And the respective 10 color change buttons:
<div>
<button onclick="destacar('circle0')">0</button>
<button onclick="destacar('circle1')">1</button>
<button onclick="destacar('circle2')">2</button>
<button onclick="destacar('circle3')">3</button>
<button onclick="destacar('circle4')">4</button>
<button onclick="destacar('circle5')">5</button>
<button onclick="destacar('circle6')">6</button>
<button onclick="destacar('circle7')">7</button>
<button onclick="destacar('circle8')">8</button>
<button onclick="destacar('circle9')">9</button>
</div>
Each button when clicked will call the destacar(idDoCirculo)
JavaScript function that receives as a parameter string
whose same is equivalent to id
of the circle that we want to highlight:
function destacar (id) {
// Remover classe "destaque"
var circulos = document.getElementById("circulos").getElementsByTagName('circle');
for (var i=0; i < circulos.length; i++) {
if (circulos[i].classList.contains('destaque')) {
circulos[i].removeClass('destaque');
}
}
// Adicionar classe "destaque" a quem diz respeito
var svg = document.querySelector('#circulos #'+id);
svg.addClass('destaque');
}
Within the function we go to all circles and remove the class destaque
if it exists. After this we go to the indicated element and add it to the class destaque
.
In this way, when you click on a button, you are highlighting a circle and removing the highlight from all the others, thus meeting what you want.
function destacar (id) {
// Remover classe "destaque"
var circulos = document.getElementById("circulos").getElementsByTagName('circle');
for (var i=0; i < circulos.length; i++) {
if (circulos[i].classList.contains('destaque')) {
circulos[i].removeClass('destaque');
}
}
// Adicionar classe "destaque" a quem diz respeito
var svg = document.querySelector('#circulos #'+id);
svg.addClass('destaque');
}
// Tem classe
SVGElement.prototype.hasClass = function (className) {
return new RegExp('(\s|^)' + className + '(\s|$)').test(this.getAttribute('class'));
};
// Adicionar Classe
SVGElement.prototype.addClass = function (className) {
if (!this.hasClass(className)) {
this.setAttribute('class', this.getAttribute('class') + ' ' + className);
}
};
// Remover Classe
SVGElement.prototype.removeClass = function (className) {
var removedClass = this.getAttribute('class').replace(new RegExp('(\s|^)' + className + '(\s|$)', 'g'), '$2');
if (this.hasClass(className)) {
this.setAttribute('class', removedClass);
}
};
// Alternar Classe
SVGElement.prototype.toggleClass = function (className) {
if (this.hasClass(className)) {
this.removeClass(className);
} else {
this.addClass(className);
}
};
circle{
fill:steelblue;
}
.destaque{
fill:darkgoldenrod;
}
<svg id="circulos" width="720" height="120">
<circle id="circle0" cx="20" cy="60" r="10"></circle>
<circle id="circle1" cx="60" cy="60" r="10"></circle>
<circle id="circle2" cx="100" cy="60" r="10"></circle>
<circle id="circle3" cx="140" cy="60" r="10"></circle>
<circle id="circle4" cx="180" cy="60" r="10"></circle>
<circle id="circle5" cx="220" cy="60" r="10"></circle>
<circle id="circle6" cx="260" cy="60" r="10"></circle>
<circle id="circle7" cx="300" cy="60" r="10"></circle>
<circle id="circle8" cx="340" cy="60" r="10"></circle>
<circle id="circle9" cx="380" cy="60" r="10"></circle>
</svg>
<div>
<button onclick="destacar('circle0')">0</button>
<button onclick="destacar('circle1')">1</button>
<button onclick="destacar('circle2')">2</button>
<button onclick="destacar('circle3')">3</button>
<button onclick="destacar('circle4')">4</button>
<button onclick="destacar('circle5')">5</button>
<button onclick="destacar('circle6')">6</button>
<button onclick="destacar('circle7')">7</button>
<button onclick="destacar('circle8')">8</button>
<button onclick="destacar('circle9')">9</button>
</div>
In order to make class handling in circle
elements within svg
more agile, we used 4 methods that are "hung" in the SVG element building prototype so that all nodes / sup> have the following:
┌─────────────────────────────────────────────────────────┐
│ Método │ Função |
├─────────────────────────────────────────────────────────┤
│ 'hasClass' │ Verifica se tem determinada classe |
├─────────────────────────────────────────────────────────┤
│ 'addClass' │ Adiciona determinada classe |
├─────────────────────────────────────────────────────────┤
│ 'removeClass' │ Remove determinada classe |
├─────────────────────────────────────────────────────────┤
│ 'toggleClass' │ Alternar presença de determinada classe |
└─────────────────────────────────────────────────────────┘
These methods are explained in detail in Todd Motto's article Todd Motto :
Hacking SVG, traversing with ease - addClass, removeClass, toggleClass functions
Each of these methods is found in the code snippet that can be viewed and executed directly in the response as well as in the JSFiddle referenced below.
Example also available on JSFiddle .