javascript - Play an audio file on hover -
i have made script forum , wondering if guide me how browser play audio file if user hovers on class style113
, has give them warning alert says "audio play"
if press ok on alert should play if press cancel should not. how can achieve this? here script have far:
my script has been removed
you can use following code:
var elems = document.getelementsbyclassname("style113"); for(i in elems) { elems[i].addeventlistener("mouseover", function() { if(confirm(" %%% confirmation message %%% ")) { // %%% code play sound %%% } }); }
what does:
- loops on elements of class
style113
- adds event listener each element event
mouseover
- in each event listener, creates
confirm()
popup (has 2 buttons, 1 confirm , 1 cancel) - if
confirm()
method returnstrue
(if positive button clicked), play sound
update per op request in comments below, can add code specific code in for
loop:
document.getelementsbyclassname('style113')[x].addeventlistener("mouseover", function() { if(confirm("audio play")) { // %%% code play sound %%% } });
i'd advise clean source code better indenting practices. also, avoid making many dom requests (e.g., repetitive document.getelementsbyclassname()
) , instead caching dom requests.
Comments
Post a Comment