html5中关于volume属性的使用详解


本文摘自PHP中文网,作者黄舟,侵删。

Audio对象属性: volume 描述:设置或返回音频的音量,取值范围(0――1)

下面是我做的音乐播放器如何调节音频音量的代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

//增加切换音量事件

(function(){

    var height = $("#myAudio ul.control li.volume .alert-box .volume-wrap .bar .scroll-bar").height();

    $("#myAudio ul.control li.volume .alert-box .volume-wrap .bar .scroll-bar .scroll-btn").on("mousedown",function(e){

        e.preventDefault();

        var downHeight = $("#myAudio ul.control li.volume .alert-box .volume-wrap .bar .scroll-bar").height();

        var downY = e.clientY;

        document.onmousemove = function(e){

            e.preventDefault();

            var moveY = e.clientY;

            var nowHeight = downY-moveY+downHeight;

            if(nowHeight<=0){

                nowHeight =0;

            }else if(nowHeight >= height){

                nowHeight = height;

            }

            $("#myAudio ul.control li.volume .alert-box .volume-wrap .bar .scroll-bar").height(nowHeight);

            var precent = nowHeight/height;

            audio.volume = precent;

        }

  

        document.onmouseup = function(){

            document.onmousemove = null;

            document.onmouseup = null;

        }

    });

})();

上面的主要思路:声明height变量先获取调节音量的滑动条的高度(设置的是80px),

给滑动条上的滑动块绑定mousedown事件,取消其默认事件e.preventDefault();

声明downHeight获取未滑动时的音量滑动条的高度, 声明downY获取点击位置距离窗口上方的y(垂直)方向距离var downY = e.clientY;

给整个dom添加mousemove事件,取消其默认事件e.preventDefault();

声明moveY获取光标移动到的位置距离窗口上方的y(垂直)方向距离var moveY = e.clientY;

声明nowHeight获取调节后音量滑动条的高度var nowHeight = downY-moveY+downHeight;

因为滑动条的高度为80px,所以在下面判断了一下

1

2

3

4

5

if(nowHeight <=0){

nowHeight=0;//最小值为0(对应volume静音)

}else if(nowHeight>=height){

nowHeight=height;//最大值为80px(对应volume最大值1)

}

将调节后的音量条高度赋值给滑动条,实现调节时滑动条同步变换高度;

由于音量vojume的取值范围(0-1),让nowHeight/height 得到调节后高度对总体高度的百分比,值为(0-1)

最后将这个值赋予audio.volume=nowHeight/height;

当调节结束后,松开鼠标添加mouseup事件,将mousemove和mouseup事件都清空

以上就是html5中关于volume属性的使用详解的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

详解html5 canvas drawing的示例代码(三)

如何使用插件数字滚动插件numberanimate.js?

h5实现桌面通知

h5的存储方式详解

自定义实现可以播放暂停、进度拖拽、音量控制及全屏的h5播放器

介绍html5+canvas调用手机拍照功能实现图片上传(下篇)

html5开发实例-threejs实现粒子动画飘花效果代码分享

h5怎样实现获取用户地理定位

html5 touch事件实现触屏页面上下滑动

几个很好用的html5移动开发框架

更多相关阅读请进入《volume》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...