Im trying to make an image spin when you click it. The more you click it the faster it spins, and if you stop clicking it will slow down over time.
The problem is that the only way to spin an object without jQuery is with the "transform" property in CSS (What I know of at least). Is there any way to use JavaScript variables in CSS? Or is there another way to spin my image? Or will I need to use jQuery?
Code:
var spinner = document.getElementById("spinner");
var speed = 0;
var addSpeed = 10;
var slowSpeed = 2;
//Activates Slowdown
window.onload = loop();
//Speed up
function spin() {
if (speed < 0) {
speed = speed + 10;
loop()
} else {
speed = speed + 10;
}
}
spinner.addEventListener('click', spin);
//Slowdown
function loop() {
setTimeout(
function slow() {
speed = speed - slowSpeed;
document.getElementById("speed").innerHTML = speed;
if (speed > 0) {
loop();
}
}, 1000)
}
//Selectors
function wheel() {
spinner.src = "http://pngimg.com/uploads/car_wheel/car_wheel_PNG23305.png";
}
function spiral() {
spinner.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Black_bold_spiral.svg/2000px-Black_bold_spiral.svg.png";
}
#spinner {
width: 500px;
}
#spinner {
transform: rotate("speed"deg);
}
/* The "speed" is the variable I want to use from the JavaScript */
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="spinnerContainer">
<img id="spinner" src="http://pngimg.com/uploads/car_wheel/car_wheel_PNG23305.png"/>
<h4 id="speed">N/A</h4>
</div>
<div id="selectors">
<ul>
<button onclick="wheel()">Wheel</button>
<button onclick="spiral()">Spiral</button>
</ul>
</div>
<footer>
</footer>
<script src="script.js"></script>
</body>
</html>
<!-- END -->