HTML: Play sounds on mouse hover or click
On my site I want to play tick/开发者_开发知识库click like sounds on mouse over (hover) or on mouse clicks. Can this be achieved?
I can do this in flash, but I want to avoid flash.
See here for all the options that you have if you dont want flash http://www.phon.ucl.ac.uk/home/mark/audio/play.htm
Javascript cannot play audio so one of the above is the only way out.
See 3 & 4 in above link and you can trigger it probably on mouse over with javascript.
OR DHTML to play sound http://webdesign.about.com/od/sound/a/play_sound_oncl.htm
You can play audio with tag or object or embed. This is the best way like below. You can control audio tag with javascript.
You can control audio by clicking these button:
Play StopAnd you must put this script to
<script>
$(document).ready(function() {
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'audio.mp3');
audioElement.setAttribute('autoplay', 'autoplay');
//audioElement.load()
$.get();
audioElement.addEventListener("load", function() {
audioElement.play();
}, true);
$('.play').click(function() {
audioElement.play();
});
$('.pause').click(function() {
audioElement.pause();
});
});
But for Chrome you cant use .play() and .pause(). You must use .Play() and .Stop.
You can control an HTML5 <audio>
element from JavaScript.
This won’t work in IE 8 and below (not sure about 9) though, nor in older versions of Firefox, Safari, Chrome and Opera.
精彩评论