简体   繁体   English

添加类不适用于使用 jquery 的 html5 视频播放器

[英]add class not working with html5 video player using jquery

I have a simple video block, on video ended I am displaying certain gif,我有一个简单的视频块,在视频结束时我正在显示某些 gif,

Problem:问题:

Now when a user pauses the video the gif also shows up, meaning When the video has ended the gifs shows up now when I play again the video and pause the video the gifs show again现在,当用户暂停视频时,gif 也会显示,这意味着当视频结束时,gif 现在会显示,当我再次播放视频并暂停视频时,gif 会再次显示

To do去做

I want when a user pauses the video the gif should not display, meaning the gif should show up only on video ended我希望当用户暂停视频时,gif 不应显示,这意味着 gif 应仅在视频结束时显示

Here is my solution这是我的解决方案

HTML HTML

<div canplay id="video-block">
    <div id="video-container">
        <video id="videoplayer" playsinline muted autoplay>
            <source src="videos/good_job.mp4"></source>
        </video>
        <div id="interactive-layers">
        </div>

    </div>

    <div id="pause-play-button">
        <img id="play" src="images/play.png">
        <img id="pause" src="images/pause.png">
    </div>

Js JS

$(document).ready(function(){
        $("#videoplayer")[0].addEventListener("ended", onVideoEnded);
});


function showGif(gifname) {
        $("#gif-data").remove();
        var gif = $("<div id='gif-data'><audio class='audio' id='gifaudio'  autoplay src='" + gifs[gifname].audio + "'></audio><img class='gif' id='animatedgif' src='" + gifs[gifname].gif + "?rand=" + Math.random() + "'></div>")
        $("#interactive-layers").append(gif);
}


function onVideoEnded(e) {

        showGif("ed22");

}


$("#pause-play-button").on("click", function () {
        clearTheTimeout();
        if ($("#video-block video")[0].paused == true) {
                $("#gif-data").addClass("hidegif");
                $("#video-block video")[0].play();
                $("#pause").css("display", 'block');
                $("#play").css("display", "none");
        } else {
                $("#video-block video")[0].pause();
                $("#audioplayer")[0].pause();
                $("#gif-data").removeClass("hidegif");
                $("#pause").css("display", 'none');
                $("#play").css("display", "block");
        }
});

CSS CSS

.hidegif{
    display: none;
}

Unfortunately, still, my giffs shows up when I click pause video button,不幸的是,当我点击暂停视频按钮时,我的 gif 仍然出现,

What am I doing wrong?我究竟做错了什么?

I think the problem happens because element 'gif-data' created dynamically.我认为问题的发生是因为元素 'gif-data' 是动态创建的。 Try to find an element inside the parent static element.尝试在父静态元素中找到一个元素。

    $('#video-block').find("#gif-data").addClass("hidegif");

That should work.那应该工作。

It's a few months old post but in my experience addEventListener not works all the time with Jquery but the .on event works.这是几个月前的帖子,但根据我的经验, addEventListener并非一直适用于 Jquery,但.on事件有效。 so when you are listening the ended event, do it with .on and write the showGif function inside it.因此,当您正在侦听结束事件时,请使用.on并在其中编写 showGif 函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM