HTML5 怎么给网页加屏幕点击声音。

HTML5 怎么给网页加屏幕点击声音。,第1张

利用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>

<bgsound loop="1" id="bgs">

<script>

function test(fileName){

//注意文件路径,音频文件尽量使用wav、mid、或MP3格式的文件,否则无法播放

document.all.bgs.src=fileName

}

</script>

<a href="#" onMouseOver="test('msg.wav')">声音1</a>

<br><br><br>

<a href="#" onMouseOver="test('up.wav')">声音2</a></p>

◆那是网页设计的技巧,使用网页制作软件,例如 Dreamweaver 就很容易实现。在图上作个热区,然后选用【链接】,再找到链接对象(可以是音乐、文本、另一个图片、视频等等)确认就行了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11740320.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存