bgsound 專門用來插入背景音樂,它的名稱就是 background sound 的縮寫。要留意的是它只對 IE 有效,在 Firefox 等瀏覽器中未必會響。
語法:
<bgsound src="背景音樂的位址"/>
說明:音訊檔可以是 mp3、wav 等常見格式;路徑可以是相對路徑或絕對路徑,強烈不建議用絕對路徑。
舉例:
<!DOCTYPE html> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <bgsound src="media/西班牙舞曲.mp3"/> </body> </html>
分析:用 bgsound 加入背景音樂,跟用 embed 加入音訊最大的差別是「有沒有操作介面」。embed 會給使用者一個播放列,bgsound 則完全隱形,音樂自己在背景播放。
背景音樂通常希望一再重播,這就靠 loop 屬性。除了 src,loop 是唯一你真正需要掌握的屬性。
語法:
<bgsound src="背景音樂的位址" loop="播放次數">
說明:loop="2" 播放兩次;loop="infinite" 無限循環;loop="-1" 同樣代表無限循環。
還有一個實用細節:用 bgsound 設定的背景音樂,視窗最小化時會自動暫停,視窗還原後繼續播放,可以自己試試看。
舉例:
<!DOCTYPE html> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form name="form1" method="post" action="index.html"> <bgsound src="media/西班牙舞曲.mp3" loop="infinite"/> </form> </body> </html>
最後提醒兩點:bgsound 只在 IE 中有效;除了它,設定背景音樂也可以改用 embed 或 object 標籤。