I am learning to write JS and am trying to create a page where the image is flipped horizontally and vertically with 2 buttons.
At the moment, the buttons toggle a CSS class in the image tag to flip the image, however I cannot find a way for both classes to be added simultaneously in the image tag, to have the result of the image being flipped horizontally and vertically.
Is there a way in JS classic, to toggle multiple classes in a HTML tag?
HTML:
<body>
<img id="image" src="./img/img_1.jpg">
<div class="button">
<button id="button_horizontal">Flip Horizontal</button>
<button id="button_vertical">Flip Vertical</button>
</div>
<script src="script.js"></script>
</body>
CSS:
.flip_horizontal{
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
}
.flip_vertical{
-webkit-transform: scaleY(-1);
transform: scaleY(-1);
}
JS:
const horizontal = document.getElementById('button_horizontal');
const vertical = document.getElementById('button_vertical');
const image = document.getElementById('image');
horizontal.addEventListener('click', function(){
if (image.classList.contains("flip_horizontal")){
image.classList.remove('flip_horizontal')}
else image.classList.add('flip_horizontal')
})
vertical.addEventListener('click', function(){
if (image.classList.contains("flip_vertical")){
image.classList.remove("flip_vertical")}
else image.classList.add("flip_vertical")
})