Reputation: 71
I have the following function which works fine when only one instance of the image being rotated:
// Next Angle Variable
nextAngle = 0;
$( ".portfolioDropLink" ).click(function() {
// Icon Variable
var currentIcon = $(this).find(".fa-angle-down");
// Rotate Icon
currentIcon.rotate(getNextAngle());
function getNextAngle() {
nextAngle += 180;
if(nextAngle >= 360) {
nextAngle = 0;
}
return nextAngle;
}
});
When two instances of the .portfolioDropLink
class are present the nextAngle
variable clashes, how can I prevent this?
Upvotes: 0
Views: 54
Reputation: 7328
One solution would be to retrieve the angle by getting its CSS value
Another solution could be to store the angle with the elements' data:
$( ".portfolioDropLink" ).click(function() {
// Icon Variable
var currentIcon = $(this).find(".fa-angle-down");
// Rotate Icon
currentIcon.rotate(getNextAngle(this));
function getNextAngle(el) {
//Get Value and Parse
var currentAngle = el.getAttribute('data-angle');
if (parseInt(currentAngle) == NaN) currentAngle = 0;
//Get Next Value
nextAngle = parseInt(currentAngle) + 180;
if(nextAngle >= 360) {
nextAngle = 0;
}
//Set Value and Return
el.setAttribute('data-angle', nextAngle)
return nextAngle;
}
});
Upvotes: 1