Reputation: 1248
I would like to toggle/switch the value of the variable every time a button is clicked so that
var ccw = $("#part1, #part3, #screw1, #screw3, #screw5, #part7");
turns into
ccw = $("#part4, #part2, #screw2, #screw4");
and
cw = $("#part1, #part3, #screw1, #screw3, #screw5, #part7");
turns into
cw = $("#part4, #part2, #screw2, #screw4");
my code
var angle;
var reverseAngle;
var angleCW;
var angleCCW;
var cw = $("#part1, #part3, #screw1, #screw3, #screw5, #part7");
var ccw = $("#part4, #part2, #screw2, #screw4");
function machine() {
angle = 0;
reverseAngle = 0;
$("#part10").unbind('click').click(function(e) {
$(this).toggle(function() {
});
ccw = $("#part1, #part3, #screw1, #screw3, #screw5, #part7");
cw = $("#part4, #part2, #screw2, #screw4");
$("#part10").hide();
$("#part11").show();
$("#part10").delay(800).fadeIn();
$("#part11").delay(800).fadeOut();
reverseRotation();
});
setInterval(function(){
reverseRotation();
cw.rotate(angleCW);
ccw.rotate(angleCCW);
$("#part9").animate({
opacity: "-=0.1",
top: "-=1"
}, 200 , function() {
if($("#part9").css("opacity") <= 0){
$(this).css({
opacity: '1',
top: '0'
});
}
});
},50);
}
Upvotes: 0
Views: 152
Reputation: 4350
You could bind a true/false switch on the click event and use a ternary operator to decide which one to use.
var switch_array = true;
$('#button').click(function(){
switch_array = !switch_array;
rotation = switch_array ? cw : ccw;
});
Now the rotation
variable will always contain the selectors you want.
Upvotes: 0
Reputation: 2998
Just bind a click event on whatever button you want to use to swap the variables like this:
$("#button_id").click(function() {
var temp = cw;
cw = ccw;
ccw = temp;
});
Upvotes: 1