JSFiddle - React, Tailwind, and code Playground
CSS
div {
width: 100px;
background: red;
}
JavaScript
function shake(element, time, coefficient){
$(element).css({"transition": "0.1s"});
setInterval(function(){
var randomInt1 = Math.floor((Math.random() * 3) + 1);
var randomInt2 = Math.floor((Math.random() * 3) + 1);
var randomInt3 = Math.floor((Math.random() * 2) + 1);
var phase1 = (randomInt1 % 2) == 0 ? "+" : "-";
var phase2 = (randomInt2 % 2) == 0 ? "+" : "-";
var phase3 = (randomInt3 % 2) == 0 ? "+" : "-";
var transitionX = ((phase1 + randomInt1) * (coefficient / 10)) + "px";
var transitionY = ((phase2 + randomInt2) * (coefficient / 10)) + "px";
var rotate = ((phase3 + randomInt3) * (coefficient / 10)) + "deg";
$(element).css({"-webkit-transform": "translate("+transitionX+","+transitionY+") rotate("+rotate+")"});
}, time);
}
$("div").click(function(){
shake(this, 50 , 10);
});