45 degree rotation increment on click w/out using

2019-03-04 15:43发布

I have some buttons I'm trying to have rotate 45 degrees on click, but because I'm using a variable (or I think this is why) when a button is clicked it rotates 45 degrees, and then when another one is clicked because the variable has already been increased 45 degrees it goes 90. Is there a way to do this w/out variables or modify the set up I have so this doesn't' happen?

Here is the code and the setup in a demo: http://jsbin.com/atinap/16/edit

$(".info_btn").css("-webkit-transition", "-webkit-transform 0.25s ease-in-out");

var info_btn_DEG = 0;

$(".info_btn").toggle(function() {
$(this).closest('.article_wrapper').find('.description').slideDown("250");
info_btn_DEG += 45;
$(this).css("-webkit-transform", "rotateZ(" + info_btn_DEG + "deg)");
$(this).fadeTo("200", 0.65);
}, function() {

$(this).closest('.article_wrapper').find('.description').slideUp("200");
info_btn_DEG += 45;
$(this).css("-webkit-transform", "rotateZ(" + info_btn_DEG + "deg)");
$(this).fadeTo("250", 0.3);
});

any help with this would be amazing thanks

1条回答
甜甜的少女心
2楼-- · 2019-03-04 16:03

You need to associate an angle with each element, not have one global angle for all elements - use .data():

function increaseAngle(elem) {
    var oldAngle = $(elem).data('angle');
    var newAngle = oldAngle + 45;
    $(elem)
        .css("-webkit-transform", "rotateZ(" + newAngle + "deg)")
        .data('angle', newAngle);
}

$(".info_btn")
    .css("-webkit-transition", "-webkit-transform 0.25s ease-in-out")
    .data('angle', 0);
    .toggle(function() {
        $(this).closest('.article_wrapper').find('.description').slideDown(250);
        increaseAngle(this);
        $(this).fadeTo(200, 0.65);
    }, function() {
        $(this).closest('.article_wrapper').find('.description').slideUp(200);
        increaseAngle(this);
        $(this).fadeTo(250, 0.3);
    });
查看更多
登录 后发表回答