下面是我做的音樂播放器如何調節音頻音量的代碼:
//增加切換音量事件(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,所以在下面判斷了一下
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屬性的使用詳解的詳細內容,其它編程語言
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答