Last active
October 30, 2022 06:35
-
-
Save srikat/8525732 to your computer and use it in GitHub Desktop.
Playing a sound on hover/click of an element in WordPress. http://sridharkatakam.com/play-sound-hoverclick-element-wordpress/
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
<?php | |
//* Do NOT include the opening php tag | |
//* Enqueue audio script | |
add_action( 'wp_enqueue_scripts', 'enqueue_audio_on_menu_items_click' ); | |
function enqueue_audio_on_menu_items_click() { | |
wp_enqueue_script( 'load-audio', get_stylesheet_directory_uri() . '/js/load-audio.js', array( 'jquery' ), '', true ); | |
} |
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
jQuery(document).ready(function($) { | |
// Adds a source element, and appends it to the audio element, represented by elem. | |
function addSource(elem, path) { | |
$('<source>').attr('src', path).appendTo(elem); | |
} | |
$(".site-header .genesis-nav-menu a").click(function(){ | |
// Create an audio element, and set it to autoplay | |
var audio = $('<audio />', { | |
autoPlay : 'autoplay' | |
}); | |
// Call our addSource function, and pass in the audio element and the path(s) to your audio. | |
addSource(audio, 'http://example.com/sounds/bell.ogg'); | |
addSource(audio, 'http://example.com/sounds/bell.mp3'); | |
// Add the audio + source elements to the page. | |
audio.appendTo('body'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment