Javascript 在视频上叠加播放按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30309584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:57:51  来源:igfitidea点击:

Overlay Play button over video

javascriptjquerycssvideo

提问by Sam Pittman

I currently have a script that plays and pauses a video onclick. What I would like to do is overlay a play button over the video at the start and when it is paused, and for that same button to disappear when the video plays again.

我目前有一个脚本可以播放和暂停视频 onclick。我想要做的是在视频开始时和暂停时在视频上覆盖一个播放按钮,并且当视频再次播放时该按钮消失。

Any suggestions would be appreciated.

任何建议,将不胜感激。

HTML

HTML

<video class="video"><source src="http://e14aaeb709f7cde1ae68-a1d0a134a31b545b257b15f8a8ba5726.r70.cf3.rackcdn.com/projects/31432/1427815464209-bf74131a7528d0ea5ce8c0710f530bb5/1280x720.mp4" type="video/mp4">
</video>

CSS

CSS

.video {
width: 50%;
border: 1px solid black;
}

JS

JS

// Plays and pauses video on click

$('.video').click(function(){this.paused?this.play():this.pause();});

回答by Ben Philipp

If you want to have an actual overlay with content you could edit, maybe this will suit you: https://jsfiddle.net/svArtist/9ewogtwL/

如果你想有一个可以编辑的内容的实际覆盖,也许这适合你:https: //jsfiddle.net/svArtist/9ewogtwL/

$('.video').parent().click(function () {
    if($(this).children(".video").get(0).paused){
        $(this).children(".video").get(0).play();
        $(this).children(".playpause").fadeOut();
    }else{
       $(this).children(".video").get(0).pause();
        $(this).children(".playpause").fadeIn();
    }
});
.video {
    width: 100%;
    border: 1px solid black;
}
.wrapper{
    display:table;
    width:auto;
    position:relative;
    width:50%;
}
.playpause {
    background-image:url(http://png-4.findicons.com/files/icons/2315/default_icon/256/media_play_pause_resume.png);
    background-repeat:no-repeat;
    width:50%;
    height:50%;
    position:absolute;
    left:0%;
    right:0%;
    top:0%;
    bottom:0%;
    margin:auto;
    background-size:contain;
    background-position: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
    <video class="video">
        <source src="http://e14aaeb709f7cde1ae68-a1d0a134a31b545b257b15f8a8ba5726.r70.cf3.rackcdn.com/projects/31432/1427815464209-bf74131a7528d0ea5ce8c0710f530bb5/1280x720.mp4" type="video/mp4" />
    </video>
    <div class="playpause"></div>
</div>

回答by Vishal Kumar Sahu

On click toggling will work fine too… and it will reduce the jQuery code. .toggle()

单击切换也可以正常工作……并且它会减少 jQuery 代码。 .toggle()

回答by Julian Avar

I think I will only need to explain this.

我想我只需要解释一下。

when .videois clicked and the video is paused ->
.video{visibility: hidden;}
when .videois clicked and the video is running ->
.video{visibility: visible;}

.video单击时视频暂停 ->单击
.video{visibility: hidden;}
.video视频正在运行 ->
.video{visibility: visible;}

If you want more code, please comment

如果你想要更多代码,请评论