0

I have some simple JS that changed the background image based on the menu element that is clicked. I'm new to JS so I apologize for my repetitive code. Currently the code works but if a menu item is clicked in quick succession the background change animation cancels out and jumps to the next image without animation. I've tried setting a timeout function and setting a transition timer but no luck. Is there a way I can keep the animation fluent? Below is a portion of my js, I'm no gonna put it all because it essentially the same thing a few times over.

      var counter = 1;

    $(document).ready(function(){
        $(".home-bg").click(function(){
            counter = 1;
            if(counter == 1){
                $(".container").css("background-image", "url(images/1-1.jpg)"); 
                $(".container").css("-webkit-transition", "background 1000ms ease-in-out");
                $(".container").css("-moz-transition", "background 1000ms ease-in-out");
                $(".container").css("-o-transition", "background 1000ms ease-in-out");
                $(".container").css("-ms-transition", "background 1000ms ease-in-out");
                $(".container").css("transition", "background 1000ms ease-in-out");
            }
        });

        $(".service-bg").click(function(){
            counter = 2;
            if(counter == 2){
                $(".container").css("background-image", "url(images/2.jpg)");
                $(".container").css("-webkit-transition", "background 1000ms ease-in-out");
                $(".container").css("-moz-transition", "background 1000ms ease-in-out");
                $(".container").css("-o-transition", "background 1000ms ease-in-out");
                $(".container").css("-ms-transition", "background 1000ms ease-in-out");
                $(".container").css("transition", "background 1000ms ease-in-out"); 
            }
        });
});
Tyharo
  • 383
  • 2
  • 5
  • 23

1 Answers1

2

You need to use the event:

element.addEventListener( 'webkitTransitionEnd', 
    function( event ) { alert( "Finished transition!" ); }, false );
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252