用HTML5为你的网页添加音效(兼容Firefox 3.5+, IE 6-9, Safari 3.0+, Chrome 3.0+, Opera 10.5+)
来源:互联网 发布:淘宝钻石买家号出售 编辑:程序博客网 时间:2024/06/05 21:18
<span style="font-size:18px;">为交互添加恰当的音效,常常能改善用户体验。在我们所熟悉的windows里,清空回收站的碎纸声就是很好的例子。下面是一个利用HTML5, Jquery,给页面添加音效的小组件(只是添加音效,并不是播放器)。其实很简单,就是利用HTML5中的audio标签,播放声音。不过为了照顾IE 6-8,还是用上了bgsound。兼容所有主流浏览器(非主流不在考虑之列了)闲话少说,上代码</span>
<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>
0 0
- 用HTML5为你的网页添加音效(兼容Firefox 3.5+, IE 6-9, Safari 3.0+, Chrome 3.0+, Opera 10.5+)
- 兼容 ie6-9,firefox,chrome,opera,safari css
- IE Firefox Opera Chrome Safari 对MARQUEE标签的兼容问题
- firefox, chrome, safari, opera, IE 的javascript engine
- CSS半透明度设置,兼容Firefox, IE, Chrome, Opera and Safari
- CSS颜色渐变(兼容IE、Firefox、Chrome、Safari)
- css hack(ie6-9,firefox,chrome,opera,safari)
- css hack(ie6-9,firefox,chrome,opera,safari)
- css hack(ie6-9,firefox,chrome,opera,safari)
- css hack(ie6-9,firefox,chrome,opera,safari)
- Css Hack(ie6-9,firefox,chrome,opera,safari)
- 在IE,Firefox,Safari,Chrome,Opera浏览器上调试javascript
- Firefox、IE、Safari、Chrome、Opera浏览器之差异
- How to detect Safari, Chrome, IE, Firefox and Opera browser?
- 五大浏览器js 判断IE、Firefox、Safari、Chrome、Opera
- 播放音乐方法(兼容IE FF Chrome Opera Safari)
- onmusewheel事件(兼容IE,FF,opera,safari,chrome)
- onmusewheel事件(兼容IE,FF,opera,safari,chrome)
- 用intel驱动提升android模拟器速度(windows和OSX)
- HDU 4002 欧拉函数
- Windows的SHELLCODE编写高级技巧
- IOS UIKit框架类之视图动画和坐标系介绍
- 9种基本算法总结
- 用HTML5为你的网页添加音效(兼容Firefox 3.5+, IE 6-9, Safari 3.0+, Chrome 3.0+, Opera 10.5+)
- Python 16进制与字符串互转
- 基本数据库sql 语言,
- gcc 编译器对 sqrt 未定义的引用
- Game of Connections(大数java+Catalan数)
- WEB免费打印控件推荐
- 【网络爬虫】【python】网络爬虫(五):scrapy爬虫初探——爬取网页及选择器
- poj3107(树的重心)
- IE提示“Internet Explorer无法打开站点XX,已终止操作”的解决办法