javascript - HTML Fade music on link -


i like, when navigating away page, execute fade of musical cue instead of cutting if off abruptly. okay me if fade adds half second time needed execute link.

i have following code execute fadeout of playing music cue.

<audio id="myaudio"   <source src="./audio/pluto.mp3" type="audio/mp3">   browser not support audio element. </audio> 

and

<script>   function fadeaudio()   {     if (myaudio.volume > 0)      {         myaudio.volume = math.max(0, myaudio.volume - 0.05);         settimeout(fadeaudio, 20);     }   } </script> 

i tested code button, , works fine (sometimes there bit of zippering, prefer abrupt cutoff).

<button onclick="fadeaudio()">try it</button> 

my problem don't know how call , execute fade before navigating away. looking @ html's onbeforeunload, seems geared entirely printing warning prompt, no way insert intervening fadeaudio() function.

simply doing following accomplished nothing:

<body onbeforeunload="fadeaudio()"> 

and didn't work onunload either.

i thinking approach rewrite links on page run fadeaudio() function first. (it's more important me transitions between pages sound nice if user decides close or go elsewhere.) perhaps can done via making button , calling fadeaudio , linking function in sequence. seems there might neater way. suggestions?

here found kind of works: (1) in link, 1 can call function (2) in function, first call fadeaudio() function, call settimeout() function 1000 ms time allotted allow fadeaudio() complete (3) settimeout calls window.open() method of target link.

i'm going tweaking various timings see how far can tighten without adding zippering. make things little sluggish going location location on site though.

wouldn't nice if there way sound fade out in own thread while new page appears. if there way, love know it.

in script tag:

  function linktohome()   {       fadeaudio();       settimeout(openhome, 1000);    }    function openhome()   {     window.open('index.htm','_self');   } 

the url call in html:

  <a href="javascript:linktohome()">home</a> 

will try figure out way paramaterize above allow method or tag include target url.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -