Trigger a random tune when hovering the mouse

I need help figuring out how to make a fixed image on my page trigger a random sound track when hovered over. The sound could be one of five different tracks.

Here is the HTML for my image:

<img src="images/Airplane.png" class="Airplane-photo">

Below is the CSS code I am using:

.Airplane-photo {
  position: fixed;
  z-index: 1;
  height: 200px;
  bottom: 0px;
  right: 0px;
}

All of the sound tracks are in MP3 and OGG formats. I have already looked at this resource but it doesn't provide the solution I'm looking for: https://css-tricks.com/play-sound-on-hover/

If anyone knows of a way to achieve this, please let me know!

Thank you in advance

Answer №1

http://jsbin.com/fiyomafuce/2/edit

To implement random audio selection, you can create an array containing the paths to your mp3/ogg files:

var audio = ["audio-1.mp3", "audio-2.mp3", "audio-3.mp3", "audio-4.mp3", "audio-5.mp3"];

The following function generates a random number between 0 and 5 (the length of the audio array), ensuring that no updates are necessary when adding more mp3 files:

function getRandomAudio() {
    var max = audio.length;
    return Math.floor(Math.random() * (max + 1));
}

By hovering over a button, the audio source will be updated with a new item from the audio array.

$btn.on('mouseover', function(){
  var index = getRandomAudio(); // get a random index
  $audio.attr('src', audio[index]); // update source
  $audio.get(0).play();
});

If you wish to stop the sound when the cursor moves outside the button, simply utilize the mouseout event like this:

$btn.on('mouseout', function() { $audio.get(0).pause() }); 

Please note that no sound will be heard as the paths provided are not real.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporate CSS styling from a separate .css file into a material component

Just starting out with material UI. I have a css file that looks like this: .bgItem { font-size: 14px; } My component setup is as follows: <MenuItem key={status.Id} value={status.Value} classes={css.bgItem}> {status.Description} </Men ...

Incorporate fresh data into dropdown options upon selection using Vue3

Can anyone assist me with populating specific input fields on a form using Vue 3? Currently, when a user selects an option from my dropdown menu, all inputs are displayed instead of just the relevant ones. Below is the select dropdown code: <select v- ...

Showing the image as a backdrop while scrolling through text

How can I create an effect that continuously displays the image while scrolling text in Internet Explorer without using position: sticky or position: fixed? var sticky = document.querySelector('.sticky-container'); var img = document.querySele ...

What is the method for arranging objects in AngularJS using a custom sorting sequence?

I would like to display an array of object's properties in a custom sorted order. Here is the initial array: $scope.weekDays = [ { "day" : "TUESDAY", "count": 10 }, { ...

Incorporating Ajax comments into an Ajax-powered status feature

I'm currently in the final phase of implementing ajax in my feed. Originally, I thought it would be a simple matter of pasting the comment input, but it turned out to be more complex than I anticipated. I placed the input below the main ajaxed status ...

Implementing setTimeout with the copy button: A guide

How can I implement a setTimeout function in the copy button so that when a user clicks on it, the text will change to "copied" and then revert back to "copy" after 3-4 seconds? Please help me find a solution to this problem and also optimize the JavaScrip ...

Ways to insert text inside a border

I'm struggling to figure out how to insert text within a border. Can someone guide me on how to accomplish this? Is there a way to place text in the center of a border? I have included a screenshot for reference. ...

What is the best way to utilize Content-Disposition: attachment?

I am new to creating websites and I am trying to make it easier for users to download mp3's from my site by allowing them to left click instead of the traditional right click and save as method. In order to achieve this, I need to set the Content-Disp ...

Executing a function in JQuery with care

Hey there, I have a total value output and I am trying to add a function that links to it. Unfortunately, I am encountering some issues while trying to put it together. The console keeps showing NaN or the "not defined" error. Here is the function I want ...

Optimizing Your HTML5 Code: Best Practices

Lately, I've come across some articles on HTML5 best practices and they all seem to emphasize the following guideline: "Prefer using id attribute over name attribute" But is this really the best approach? How can I effectively manage forms in PHP wi ...

Issue with IE7 when using JQuery to repopulate a <ul> unordered list: new elements showing up under previously hidden elements

Within this javascript snippet, I am utilizing the code below to clear out a list of countries within a <ul> element and then repopulate it (with a slight animation using jQuery's hide() function). The functionality works smoothly in Chrome and ...

Struggling to achieve a horizontal scroll using 'overflowX' in MUI, but unfortunately, it's not functioning as expected

Is there a way to create a properly-sized card with a horizontal scrollbar in a paper format? I've tried using 'overflow' but it doesn't seem to be working in this scenario. import React from 'react'; import { makeStyles } fro ...

Try utilizing a variety of background hues for uib progressbars

Looking to incorporate the ui-bootstrap progressbar into my template in two different colors, background included. My initial idea was to stack two progress bars on top of each other, but it ended up looking too obvious and messy, especially in the corner ...

Using the Nodejs Array prototype filter method within a JSON object

Attempting to create a function that filters and returns specific strings within JSON data. Any advice is appreciated. Thank you! [ { line: '{"status":"waiting"}' } ] var body_W = []; body_W.push({line: JSON.stringif ...

What is the best way to extract HTML using selenium.webdriver in Python?

Appreciate your attention, and please excuse any errors in my English. I am currently attempting to retrieve HTML from . The process involves entering some text into an input box and clicking a button, which triggers the following actions: Load the Yaho ...

executing minimal tasks when including a new particular category

When my page is loading, the "changed_value" class is not present on the page. I am adding the "changed_value" class in multiple places within the code. Upon adding this class, I want to execute several actions. The issue is that I don't want to man ...

Avoiding the use of if statements in Javascript

I've recently started learning Javascript and I'm facing an issue with my code. I want to create a functionality where clicking on an image on one page redirects you to another page and shows a specific div based on the clicked image. To achieve ...

What can be done to stop $.ajax from automatically appending [] to my key names when making requests (aside from using processData: false

When attempting to utilize $.ajax for a POST request with data in the following format: { list: ["fake_id_0"], mapType: "fakeToRealId" } jQuery seems to alter the structure by adding brackets only to the key list, transforming it into: { lis ...

Trouble with Printing Query - MySQL, PHP, and HTML -

In my IIS class, I encountered a puzzling issue that even my tutor couldn't solve. So here I am, attempting to describe it as best as I can! Currently, I'm using Xampp with Apache and MySQL. When I run a query and display the results in a table, ...

Refreshing Child's Activities

In my scenario, I am displaying data in a child action and utilizing buttons to modify the displayed information. Here is an example of how I am achieving this: Index.cshtml <head> <script type="text/javascript"> $("#RefreshView").on ...