视频叠加层上的播放按钮 - 更改按钮文本

Play button on video overlay - changing button text

我在页面上有一个视频滑块,我想在活动视频的顶部添加一个功能播放按钮。我按照这里的教程进行操作:https://codeconvey.com/add-html5-video-overlay-play-button/ 该按钮可用于 pausing/playing 视频;但是,它不会在单击时将文本从“播放”更改为“暂停”。我还尝试更改不透明度或隐藏按钮,以便按钮在播放时不会覆盖视频。 我用适用的代码制作了一个 JSFiddle(但出于某种原因,该按钮根本没有出现在 fiddle 中):https://jsfiddle.net/xasw7dqL 我在控制台中收到错误消息“$ is not defined”,所以我确定它与此有关,但我不知道如何定义它。 我以前没有在站点中真正实现 JavaScript。在此先感谢您的帮助。 代码如下:
HTML:

        <div class="image-slider-fade fade">
          <button class="_active">Play</button>
          <video id="vid1" controls>
            <source src="videos/testvid1.mp4" type="video/mp4">
            Your browser does not support the video.
          </video>
        </div>

CSS:

#vid1, #vid2, #vid3, #vid4 {
    width: 100%;
    z-index: -1;
}

button {
    background-color: #666;
    border: medium none;
    color: #fff;
    display: block;
    font-size: 18px;
    left: 0;
    margin: 0 auto;
    padding: 8px 16px;
    position: absolute;
    right: 0;
    top: 50%;
}

button._active {
    opacity: 0.5;
}

button:hover {
    background-color: #e05a00;
}

JavaScript:

//video play button

$(document).ready(function() {
    var ctrlVideo = document.getElementById("video");
    ('button').click(function() {
        if ($('button').hasClass("_active")) {
            ctrlVideo.play();
            $('button').html("Pause");
            $('button').toggleClass("_active");
        } else {
            ctrlVideo.pause();
            $('button').html("Play");
            $('button').toggleClass("_active");
        }
    });
});

有 3 个问题需要解决:

  1. 你写了var ctrlVideo = document.getElementById("video"); 但在 html 中你使用 <video id="vid1" controls>。 将您的js更改为: var ctrlVideo = document.getElementById("vid1");

  2. 设置你的按钮的 z-index,因为现在你的按钮在播放器后面,你不能按下它(如果你删除 html 中的控件,你会注意到,该点击根本不会触发):

     button {
     background-color: #666;
     border: medium none;
     color: #fff;
     display: block;
     font-size: 18px;
     left: 0;
     margin: 0 auto;
     padding: 8px 16px;
     position: absolute;
     right: 0;
     top: 50%;
     z-index: 123;
    

    }

  3. 第5行:在开头添加$$('button')

此外,要使 jquery 在 jsfiddle 上运行,请将其与您的 js 代码一起添加到 window(在下拉列表中)