Hi, I'm making a website with a picture of a bed mattress that is turning right/left. I've made a js script to rotate the picture 5 degrees forward when the right-arrow button on a keyboard is pressed and -5 degrees back when the left-arrow button is pressed. However, it is possible to completely flip the bed, which is not in my interest, so I was wondering if it was possible to somehow limit the rotation in the interval -20 deg ; 90 deg?
Heres the code for the js:
Thanks in advance,
Marcus
Heres the code for the js:
JavaScript:
<script>
var current_rotation = 0
function rotate(grader) {
current_rotation = grader + current_rotation;
document.getElementById("madras").style.transform = 'rotate(' + current_rotation + 'deg)';
console.log("Der er blevet roteret!");
};
document.addEventListener('keydown', function(event) {
if (event.keyCode == 39) {
rotate(5)
} else if (event.keyCode == 37) {
rotate(-5)
}
}, true);
</script>
Thanks in advance,
Marcus