页面特效

CSS+HTML<视频播放video标签的使用>

2019-11-27  本文已影响0人  誰在花里胡哨
效果图:
video.gif
<video>标签属性
image.png
<source>标签
例如:
<video>
<source src="movie.mp4"type="video/mp4">
<source src="movie.ogg"type="video/ogg">
Yourbrowserdoesnotsupportthevideotag.
</video>

source是为了兼容各个浏览器。因为浏览器对视频格式支持不统一,有些浏览器例如有些浏览器支持mp4格式,那么它就播放第一个mp4格式的视频,如果它不支持就检查是不是支持下面的ogg视频,如果支持就播放,不支持的话继续向下,下面没有source了,那么就显示“Your browser does not support the video tag.”

注意:
如果你在 <video>内设置了 muted属性,是可以通过 autoplay实现视频的自动播放,反之则需要用户手动授权,点击播放才可以实现自动播放。
因为谷歌浏览器禁止页面在没有用户授权的情况下进行有声的视频播放

代码如下:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body,
        html {
            height: 100%;
        }

        body {
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .video-box {
            width: 300px;
            height: 200px;
            box-shadow: 0 0 5px #ccc;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        video {
            position: absolute;
            top: 0;
            left: 0;
        }

        .play {
            width: 50px;
            height: 50px;
            box-shadow: 0 0 5px #ccc;
            border-radius: 50%;
            position: relative;
            z-index: 5;
            cursor: pointer;
            transition: 0.3s;
        }

        .play:hover {
            box-shadow: 0 0 30px #ccc;
        }

        .play::before {
            content: '';
            width: 100%;
            height: 100%;
            position: absolute;
            clip-path: polygon(20% 0, 100% 50%, 20% 100%);
            background: #5c5c5c;
            transform: scale(0.4);
        }
    </style>
</head>

<body>
    <div class="video-box">
        <video id="video" width="100%" height="100%" preload loop muted>
            <source src="https://waaark.com/wp-content/uploads/2016/05/waaark-melville-design.mp4" type="video/mp4">
        </video>
        <div class="play" onclick="clickPlay()"></div>
    </div>
</body>
<script>
    //点击外部标签播放视频
    function clickPlay() {
        let video = document.getElementById('video')
        let btn = document.querySelector('.play')
        btn.style.display = 'none'
        video.play()
    }
</script>

</html>
上一篇下一篇

猜你喜欢

热点阅读