0

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?

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
user2279981
  • 71
  • 1
  • 6
  • Does the plugin you're using allow you to get the angle from the icon itself? If so you would not need to store a `nextAngle` variable anymore, you would just get the rotation and use that. – Alberto Zaccagni Sep 01 '14 at 16:09
  • you can start by providing more info. As in Jquery version and some of the structure of your page, jsfiddle is also a acceptable first step. The more info you give, the faster your question will be answered ;) – Hristo Valkanov Sep 01 '14 at 16:10

1 Answers1

1

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;
    }

});
Community
  • 1
  • 1
Stryner
  • 7,288
  • 3
  • 18
  • 18