JavaScript How to toggle an animation upon clicking a button

I have the following j-query code (using the jquery plugin ajax/libs/jquery/1.10.1) to move a div element to the left upon clicking on a separate div element:

<script>
$(document).ready(function(){
$("#tab_box3").click(function(){
$.fx.speeds._default = 800;             
$("#tab3").animate({left:"+=97%"});
});  
});
</script>

What I want to happen is when the #tab_box3 div is clicked on a second time, the #tab3 div moves back to where it originally started.

I tried the following, using Toggle, but it does not seem to have worked:

<script>
$(document).ready(function(){
$("#tab_box3").click(function(){
$.fx.speeds._default = 800;             
$("#tab3").animateToggle({left:"+=97%"});
});  
});
</script>

Can anyone offer advice please?

Answer:1

You must doing something like this :

    $(document).ready(function(){
    $("#tab_box3").click(function(e){
        var element = $(this),
            clicked = parseInt(element.data('clicked')) || 0;

        element.data('clicked', clicked + 1);
        $("#tab3").stop();
        if (clicked % 2 == 0)
        {
            $("#tab3").animate({left:"+=97%"}, 800);
        }
        else
        {
            $("#tab3").animate({left:"-=97%"}, 800);
        }
        e.preventDefault();
    });  
});

An example here : http://jsfiddle.net/Q5HDu/

Answer:2

I'm writing something in meteor (in windows 8.1) and want to show a map, with the US states outlined, but the neighboring countries still showing. Ideally, the map would be an orthographic ...

I'm writing something in meteor (in windows 8.1) and want to show a map, with the US states outlined, but the neighboring countries still showing. Ideally, the map would be an orthographic ...

I understand that JavaScript automatically assigns each function/method the parameter this, and that this is bound to the object which contains it. A method is a function stored as the property of ...

I understand that JavaScript automatically assigns each function/method the parameter this, and that this is bound to the object which contains it. A method is a function stored as the property of ...

  1. invoke inner function

i am using backbonejs framework for front-end and using chartjs for charts, so to keep it simple : i have a view that renders tabs perfectly and in every tab there is a chart in it first time ...

i am using backbonejs framework for front-end and using chartjs for charts, so to keep it simple : i have a view that renders tabs perfectly and in every tab there is a chart in it first time ...

In Magento I have a savings box, which calculates how much you save based on the difference between the regular and discount price. Now I want that box to increase the saving amount based on the ...

In Magento I have a savings box, which calculates how much you save based on the difference between the regular and discount price. Now I want that box to increase the saving amount based on the ...