-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #810 from Aathish101/patch-14
Update script.js
- Loading branch information
Showing
1 changed file
with
28 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
// const audio = new Audio('caraudio.mp3'); | ||
// var resp = audio.play(); | ||
|
||
// if(resp != undefined){ | ||
// resp.then(_ => { | ||
// //autoplay starts | ||
// }).catch(error => { | ||
// //shoe error | ||
// }); | ||
// } | ||
// audio.loop()=true; | ||
|
||
var audio = document.createElement("audio"); | ||
document.body.appendChild(audio); | ||
audio.src="caraudio.mp3" | ||
audio.src = "caraudio.mp3"; | ||
|
||
let isMoving = false; | ||
|
||
// Start playing the audio when the mouse moves | ||
document.body.addEventListener("mousemove", function() { | ||
if (!isMoving) { | ||
audio.play(); | ||
audio.loop = true; // Set the audio to loop | ||
isMoving = true; // Indicate that the car is moving | ||
} | ||
}); | ||
|
||
// Stop playing the audio when the left mouse button is released | ||
document.body.addEventListener("mouseup", function(event) { | ||
if (event.button === 0) { // 0 is the left mouse button | ||
audio.pause(); | ||
audio.currentTime = 0; // Optionally reset audio to the beginning | ||
isMoving = false; // Indicate that the car has stopped | ||
} | ||
}); | ||
|
||
document.body.addEventListener("mousemove", function(){ | ||
audio.play(); | ||
audio.loop(); | ||
}) | ||
// Optional: Start playing audio again if the mouse is moved while stopped | ||
document.body.addEventListener("mousedown", function() { | ||
if (!isMoving) { | ||
audio.play(); | ||
audio.loop = true; | ||
isMoving = true; | ||
} | ||
}); |