我的页面上有多个音轨,如下所示:
http://jsfiddle.net/24eLsv6r/2/

问题是音量和进度控件仅附加到第一个音频。
我试着为他们做些类似的事情:

$vol = $('volume', $(this).parent())[0];




$bar = $('progressbar', $(this).parent())[0];


但是它仍然无法正常工作。

有人有主意吗?

最佳答案

您需要使用jQuery.each()遍历每个音频。在这种情况下,您可以独立获取和设置控件。

我添加ID只是为了证明一种解决方案。 (第1卷,编1 ...)

我提出了可以为您提供帮助的解决方案。 CSS3有效。

的HTML

<div class="player">
    <audio class="audio" src="https://upload.wikimedia.org/wikipedia/commons/7/79/Common_Pauraque_VOL_11-05_Dudley_T._Dougherty_Natural_Sounds_Collection.ogg" loop>
        <p>Your browser does not support the audio element</p>
    </audio>
    <div class="playpause"></div>
    <div id="volume0" class="volume"></div>
    <br>
    <div id="prog0" class="progressbar"></div>
    <br>
</div>
<div class="player">
    <audio class="audio" src="https://upload.wikimedia.org/wikipedia/commons/7/79/Common_Pauraque_VOL_11-05_Dudley_T._Dougherty_Natural_Sounds_Collection.ogg" loop>
        <p>Your browser does not support the audio element</p>
    </audio>
    <div class="playpause"></div>
    <div id="volume1" class="volume"></div>
    <br>
    <div id="prog1" class="progressbar"></div>
    <br>
</div>
<div class="player">
    <audio class="audio" src="https://upload.wikimedia.org/wikipedia/commons/7/79/Common_Pauraque_VOL_11-05_Dudley_T._Dougherty_Natural_Sounds_Collection.ogg" loop>
        <p>Your browser does not support the audio element</p>
    </audio>
    <div class="playpause"></div>
    <div id="volume2" class="volume"></div>
    <br>
    <div id="prog2" class="progressbar"></div>
    <br>
</div>


的CSS

    .player {
        position: relative;
        margin: 50px auto;
        text-align: center;
        width: 300px;
    }
    .playpause {
        background: url(https://upload.wikimedia.org/wikipedia/commons/b/b8/WikiWidgetPlayButton.png) no-repeat center;
        border: 1px solid grey;
        border-radius: 7px;
        cursor: pointer;
        font-size: 12px;
        margin-bottom: 15px;
        padding: 12px 0;
    }
    .playpause:hover {
        border-color: #ccc;
    }
    .playing {
        background: url(http://www.pokeroffice.com/img/manual/pause.gif) no-repeat center;
    }
    .volume, .progressbar {
        border: none;
        height: 2px;
    }
    .volume {
        background: hsla(120, 55%, 50%, 1);
        color: inherit;
    }
    .progressbar {
        background: grey;
        color: inherit;
    }
    .ui-slider-handle {
        border-radius: 50% !important;
        height: 11px !important;
        margin-left: -5px !important;
        top: -5px !important;
        width: 15px !important;
    }


Java脚本

$(function () {
    var $aud = $(".audio"),
        $pp = $(".playpause");

    $aud.each(function (i, audio) {
        var $bar = $("#prog" + i);
        var $vol = $("#volume" + i);
        var AUDIO = $aud[i];

        AUDIO.volume = 0.15;
        AUDIO.addEventListener("timeupdate", function () {

            $bar.slider("value", ~~ (100 / AUDIO.duration * AUDIO.currentTime));
        }, false);

        $vol.slider({
            value: AUDIO.volume * 100,
            slide: function (ev, ui) {
                $vol.css({
                    background: "hsla(180," + ui.value + "%,50%,1)"
                });
                AUDIO.volume = ui.value / 100;
            }
        });

        $bar.slider({
            value: AUDIO.currentTime,
            slide: function (ev, ui) {
                AUDIO.currentTime = AUDIO.duration / 100 * ui.value;
            }
        });
    });

    $pp.click(function () {
        $aud = $("audio", $(this).parent())[0];
        if ($aud.paused) {
            $(this).addClass("playing");
            $aud.play();
        } else {
            $(this).removeClass("playing");
            $aud.pause();
        }
    });
});


Demo

关于javascript - 多个音量和进度控制JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32575985/

10-09 23:53