2010-02-18 53 views
1

我在站點上的菜單按鈕上使用了以下腳本來實現onmouseover聲音效果。Firefox中的Onmouseover聲音

<script LANGUAGE="JavaScript"><!-- 


var aySound = new Array(); 

aySound[0] = "s.mp3"; 

document.write('<BGSOUND ID="auIEContainer">') 
IE = (navigator.appVersion.indexOf("MSIE")!=-1 && document.all)? 1:0; 
NS = (navigator.appName=="Netscape" && navigator.plugins["LiveAudio"])? 1:0; 
ver4 = IE||NS? 1:0; 
onload=auPreload; 

function auPreload() { 
if (!ver4) return; 
if (NS) auEmb = new Layer(0,window); 
else { 
Str = "<DIV ID='auEmb' STYLE='position:absolute;'></DIV>"; 
document.body.insertAdjacentHTML("BeforeEnd",Str); 
} 
var Str = ''; 
for (i=0;i<aySound.length;i++) 
Str += "<EMBED SRC='"+aySound[i]+"' AUTOSTART='FALSE' HIDDEN='TRUE'>" 
if (IE) auEmb.innerHTML = Str; 
else { 
auEmb.document.open(); 
auEmb.document.write(Str); 
auEmb.document.close(); 
} 
auCon = IE? document.all.auIEContainer:auEmb; 
auCon.control = auCtrl; 
} 
function auCtrl(whSound,play) { 
if (IE) this.src = play? aySound[whSound]:''; 
else eval("this.document.embeds[whSound]." + (play? "play()":"stop()")) 
} 
function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); } 
function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); } 
//--> 
</script> 

這在IE中正常工作,但不是Firefox。

有沒有人知道在沒有使用閃存的情況下,在IE和Firefox中是否有相同的onmouseover音效?

感謝

回答

0

聲音在您的網站唯一方法是使用Flash或任何的Facebook用於傳入IM,但同樣需要的Apple QuickTime播放。

0

有人知道如果在沒有使用Flash的情況下IE和Firefox都有相同的onmouseover音效嗎?

那麼,你可以使用JavaScript庫,它規範跨瀏覽器onmouseover行爲。或者,如果你真的不想 - 在普通的'JavaScript'中應該是可能的,並且有很多黑客支持所有不同的瀏覽器。你的選擇。

,張貼在gist 243946,這裏是一個jQuery的片段做的正是你想要什麼:

var $sound = $('<div id="sound" />').appendTo('body'); 
$('#special-links-that-play-annoying-sounds-when-hovered a').hover(function() { 
$sound.html('<embed src="foo.mp3" hidden="true" autostart="true" loop="false">'); 
}, function() { 
// We could empty the innerHTML of $sound here, but that would only slow things down. 
}); 

當然,這可以在普通的舊的JavaScript寫成(無jQuery的)爲好。