do a barrel roll in javascript

To do a barrel roll animation in JavaScript, you can use CSS3 transform property. Here's an example code snippet that rotates an HTML element 360 degrees when clicked:

<html>
<head>
<style>
.barrel-roll {
    transition: transform 1s;
} 
</style>
</head>
<body>

<div class="barrel-roll" onclick="this.style.transform='rotate(360deg)'">
    <p>Click me to do a barrel roll!</p>
</div>

</body>
</html>
234 chars
17 lines

In this code, when the .barrel-roll div element is clicked, its transform property is set to rotate(360deg), which triggers the animation. The transition property in the CSS code smoothly animates the rotation over a period of 1 second.

You can also use JavaScript to toggle the class of the element to trigger the animation. For example:

<html>
<head>
<style>
.barrel-roll {
    transition: transform 1s;
} 

.rotate {
    transform: rotate(360deg);
}
</style>

<script>
function doBarrelRoll() {
    var element = document.getElementById("barrel");
    element.classList.toggle("rotate");
}
</script>

</head>
<body>

<div class="barrel-roll" id="barrel" onclick="doBarrelRoll()">
    <p>Click me to do a barrel roll!</p>
</div>

</body>
</html>
409 chars
29 lines

In this code, the doBarrelRoll() function toggles the class of the div element between barrel-roll and rotate. When the class is set to rotate, the transform property is set to rotate(360deg), triggering the animation.

gistlibby LogSnag