国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

用HTML5為你的網頁添加音效

2024-04-27 14:59:12
字體:
來源:轉載
供稿:網友

為交互添加恰當的音效,常常能改善用戶體驗。在我們所熟悉的windows里,清空回收站的碎紙聲就是很好的例子。

下面是一個利用HTML5, Jquery,給頁面添加音效的小組件(只是添加音效,并不是播放器)。

其實很簡單,就是利用HTML5中的audio標簽,播放聲音。不過為了照顧IE 6-8,還是用上了bgsound。

兼容所有主流瀏覽器(非主流不在考慮之列了)

閑話少說,上代碼:

<a href="#" class="fui-btn">播放</a>

<script>
/*Play sound component*/
/*
* PRofile: JSON, {src:'chimes.wav',altSrc:'',loop:false}
*
* setSrc: Function, set the source of sound
* play: Function, play sound
*/
if (!FUI){
var FUI = {};
}
FUI.soundComponent=function(profile){
this.profile={
src:'',          //音頻文件地址
altSrc:'',         //備選音頻文件地址 (不同瀏覽器支持的音頻格式不同,可見附表)
loop:false        //是否循環播放,這個參數現在沒有用上
};
if(profile) {
$.extend(this.profile,profile);
}
this.soundObj=null;
this.isIE = !-[1,]; /*這個方法是前輩大牛發明的,利用ie跟非ie中JScript處理數組最后一個逗號“,”的差異, 不過對于IE 9,這個辦法就無效了,但此處正合我用,因為IE 9支持audio*/
this.init();
};
FUI.soundComponent.prototype={
init:function(){
this._setSrc();
},
_setSrc:function(){
if(this.soundObj){
if(this.isIE){
this.soundObj[0].src=this.profile.src;
}else{
this.soundObj[0].innerHTML='<source src="'+this.profile.src+'" /><source src="'+this.profile.altSrc+'" />';
}
}else{
if(this.isIE){
this.soundObj=$('<bgsound volume="-10000" loop="1" src="'+this.profile.src+'">').appendTo('body'); //-10000是音量的最小值。先把音量關到最小,免得一加載就叮的一聲,嚇到人。
}else{
this.soundObj=$('<audio preload="auto" autobuffer><source src="'+this.profile.src+'" /><source src="'+this.profile.altSrc+'" /></audio>').appendTo('body');
}
}
},
setSrc:function(src,altSrc){
this.profile.src=src;
if(typeof altSrc!='undefined'){
this.profile.altSrc=altSrc;
}
this._setSrc();
},
play:function(){
if(this.soundObj){
if(this.isIE){
this.soundObj[0].volume = 1;  //把音量打開。
this.soundObj[0].src = this.profile.src;
}else{
this.soundObj[0].play();
}
}
}
};
var sd=new FUI.soundComponent({src:'ding.wav',altSrc:'ding.mp3'});
$('.fui-btn').bind('click',function(e){
sd.play();
});
</script>

附表

Format IE 9 Firefox 3.5 Opera 10.5 Chrome 3.0 Safari 3.0
Ogg Vorbis No Yes Yes Yes No
MP3 Yes No No Yes Yes
Wav No Yes Yes Yes Yes

Format

IE9

Firefox3.5

Opera10.5

Chrome3.0

Safari3.0

OggVorbis

No

Yes

Yes

Yes

No

MP3

Yes

No

No

Yes

Yes

Wav

No

Yes

Yes

Yes

Yes


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延寿县| 宝坻区| 温泉县| 凤阳县| 眉山市| 武城县| 龙口市| 庆云县| 额尔古纳市| 岳西县| 临朐县| 焦作市| 信阳市| 中卫市| 长武县| 长宁区| 四子王旗| 贺兰县| 华安县| 股票| 贺州市| 沂源县| 盈江县| 车致| 资溪县| 贡觉县| 绍兴市| 微山县| 株洲县| 长岛县| 新安县| 宜都市| 酉阳| 米林县| 墨竹工卡县| 宜宾市| 子洲县| 山阴县| 溧水县| 德格县| 昂仁县|