Answer the question
In order to leave comments, you need to log in
How to change photo when mouse is moved (js)?
I have a photo of a bike:
I want to make it rotate by 180 when you click the mouse and move to the left and back, so that the bike changes its angle:
How it will look in js, at least approximately.
Answer the question
In order to leave comments, you need to log in
function nextImage(image) {
var imageNames = ["blackbird.jpg", "duck.jpg", "sheep.jpg"];
image.current++;
if (image.current === imageNames.length) image.current = 0;
image.src = "images/" + imageNames[image.current];
}
window.onload = function() {
var img = document.getElementById("main");
img.current = 1;// создаём новое свойство (счётчик)
img.onmousedown = function(event) {
var startX = event.clientX;
img.onmousemove = function(event) {
console.log(startX);
if (startX - event.clientX < -40) {
nextImage(this);
this.onmousemove = null;
}
};
return false; //Позволяет избавиться от стадартного обработчика
}
}
<!DOCTYPE HTML>
<html lang=en>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href=""/>
<script src="type.js"></script>
</head>
<body>
<img id="main" src="images/duck.jpg">
</body>
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question