2

I need to trigger the animation on each button click not toggling the button class as I have on my snippet bellow:

I tried with addClass() and removeClass() similar methods, but it doesnt work as expected, here is the code:

$(document).ready(function () {
    $("button[class*='navbar-toggler']").click(function () {
        $(this).toggleClass("my-animation")
    });
})
@keyframes my-animation {
	16.65% {
		transform: translateY(8px);
	}

	33.3% {
		transform: translateY(-6px);
	}

	49.95% {
		transform: translateY(4px);
	}

	66.6% {
		transform: translateY(-2px);
	}

	83.25% {
		transform: translateY(1px);
	}

	100% {
		transform: translateY(0);
	}
}

.my-animation {
		animation-name: my-animation;
		animation-duration: 1s;
		animation-timing-function: ease-in-out;
		animation-iteration-count: 1;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
                            <i class="fa fa-bars"></i>
                        </button>

0

2 Answers 2

3

Use setTimeout funtion for remove my-animation class and use

.navbar-toggler{
outline:0!important;
}

$(document).ready(function () {
    if(!$("button[class*='navbar-toggler']").hasClass('my-animation')){
    $("button[class*='navbar-toggler']").click(function () {
        $(this).addClass("my-animation");
        setTimeout(function(){  $("button[class*='navbar-toggler']").removeClass("my-animation"); }, 1000);
    });
    }
})
@keyframes my-animation {
	16.65% {
		transform: translateY(8px);
	}

	33.3% {
		transform: translateY(-6px);
	}

	49.95% {
		transform: translateY(4px);
	}

	66.6% {
		transform: translateY(-2px);
	}

	83.25% {
		transform: translateY(1px);
	}

	100% {
		transform: translateY(0);
	}
}

.my-animation {
		animation-name: my-animation;
		animation-duration: 1s;
		animation-timing-function: ease-in-out;
		animation-iteration-count: 1;
}

.navbar-toggler{
outline:0!important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
                            <i class="fa fa-bars"></i>
                        </button>

Sign up to request clarification or add additional context in comments.

1 Comment

And why it doesnt work if you use $(this).removeClass() instead of $("button[class*='navbar-toggler']").removeClass()??
0

this may help you using

$(document).ready(function () {
    $("button[class*='navbar-toggler']").click(function () {
        $(this).addClass("my-animation");
            setTimeout(function () {
                $('#YourId').removeClass('my-animation');
            }, 200);
    });
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.