Every time the submit button is clicked, I want all the images to spin. However, this rotation effect only works the first time the button is pressed, until the page is refreshed. I have used jQuery to add and remove a class from the images when the button is clicked, and the rotate animation is defined in my CSS.
I've streamlined my code to this point, but something seems off. Any help would be greatly appreciated!
function diceRoll() {
$(".dice").removeClass("rotate");
$(".dice").addClass("rotate");
}
document.getElementById("submit").addEventListener("click", function(e) {
e.preventDefault();
diceRoll();
})
/* Images */
img {
width: 70px;
line-height: 0;
margin: 0 1%;
display: flex;
justify-content: center;
display: inline;
}
figure {
display: inline-block;
vertical-align: middle;
}
.rotate {
animation: rotation 0.1s infinite linear;
animation-iteration-count: 2;
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}
<title>Diceey</title>
<!-- Bootstrap, CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="styles2.css">
<!-- Jquery Links -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<div class="container-fluid heading">
<h1 id="title"> Hello there</h1>
<h5>Press to roll dice</h5>
</div>
<div class="container-fluid instructions">
<!-- Images -->
<div class="container-fluid">
<div class="container-of-images">
<figure>
<img id="img1" class="dice" src="https://image.ibb.co/nFqkvx/dice6.png">
</figure>
<figure>
<img id="img2" class="dice" src="https://image.ibb.co/nFqkvx/dice6.png">
</figure>
<figure class="threeChoices">
<img id="img3" class="dice" src="https://image.ibb.co/nFqkvx/dice6.png">
</figure>
<figure class="fourChoices">
<img id="img4" class="dice" src="https://image.ibb.co/nFqkvx/dice6.png">
</figure>
</div>
</div>
<div class="container-fluid">
<div class="container-of-forms">
<a href="" id="submit" class="btn btn-info btn-lg" role="button">Go</a>
</div>
<script src="index2.js" charset="utf-8">
</script>
</body>
<footer>
</footer>