使用HTML5自制视频控件

本文详细介绍了如何使用HTML5和CSS3自制视频播放器,包括播放、暂停功能,暂停时显示广告,进度条计时以及声音控制。通过DOM元素获取和JavaScript实现动态交互,增强了用户体验。同时,文章还提供了关键代码示例和最终效果展示。
摘要由CSDN通过智能技术生成

前言

随着网络时代的到来,人们的生活也是相当便利,例如:当我们点击一个视频时,视频就会开始播放。在这个例子中,有没有大胆想过我们常用的播放,暂停,音量控制,进度条等等,这些功能我们自己来实现呢?如果你有这个想法,那么我的这篇文章就可以帮您实现。

静态样式实现

标签部分:

    <div id="father">
        <video src="birds.mp4" volume="0.5" currentTime></video>
        <div class="jdt">
            <div class="div1"></div>
            <div class="div2"></div>
            <div class="div3">00:00/00:00</div>
            <div id="ran">
                <div></div>
                <i></i>
            </div>
            <div class="div4"></div>
            <div class="sy">
                <i></i>
            </div>
            <div class="div5"></div>
        </div>
    </div>

样式部分:

#father {
            position: relative;
            width: 885px;
            height: 480px;
            transition: all 3s linear;
        }

        .div1 {
            position: absolute;
            width: 40px;
            height: 40px;
            background-image: url("播放_wps图片.png");
            background-position: center;
            background-size: 40px 40px;
            top: 8px;
            left: 30px;
        }

        .div2 {
            position: absolute;
            width: 450px;
            height: 250px;
            /* background-image: url("gg.jpeg"); */
            background-position: center;
            background-size: 450px 250px;
            top: -345px;
            left: 225px;

        }

        .div3 {
            position: absolute;
            width: 100px;
            height: 30px;
            left: 95px;
            top: 20px;
            font-size: 16px;
        }

        .div4 {
            position: absolute;
            width: 30px;
            height: 30px;
            background-image: url("声音.png");
            background-position: center;
            background-size: 30px 30px;
            top: 10px;
            right: 290px;
        }

        .sy {
            position: absolute;
            width: 100px;
            height: 2px;
            top: 25px;
            left: 563px;
            background-color: cornflowerblue;
        }

        .sy>i {
            position: absolute;
            width: 10px;
            height: 10px;
            left: 0px;
            top: -4px;
            background-color: snow;
            border-radius: 10px;
        }


        .div5 {
            position: absolute;
            width: 50px;
            height: 50px;
            background-image: url("放大.png");
            background-position: center;
            background-size: 50px 50px;
            top: 2px;
            right: 30px;
        }

        #ran {
            position: absolute;
            width: 856px;
            height: 2px;
            top: 0px;
            left: 0px;
            background-color: cornflowerblue;
        }

        #ran>i {
            position: absolute;
            width: 10px;
            height: 10px;
            left: 0px;
            top: -4px;
            background-color: snow;
            border-radius: 10px;
        }
        .jdt {
            position: absolute;
            width: 855px;
            height: 50px;
            left: 0px;
            top: 430px;
            background-color: rgba(0, 0, 0, 0.5);
            display: none
        }

        video {
            position: absolute;
            width: 855px;
            height: 480px;
        }

实现之后的页面是这样的:
在这里插入图片描述
完成这些之后,我们就得加强真实度:
1.当鼠标触碰进度条的时候进度条放大,进度条计时点放大
在这里插入图片描述
2.当鼠标接触声音控件的时候,音量计时点放大
在这里插入图片描述
实现代码为:

        .sy:hover i {
            height: 15px;
            width: 15px;
            top: -7px;
        }
                #ran:hover {
            height: 5px;
            top: -2px;
        }

        #ran:hover>div {
            height: 5px;
        }

        #ran:hover>i {
            height: 15px;
            width: 15px;
        }
        #father:hover .jdt {
            display: block;
        }

js的dom元素获取

        var playvid;
        var bf = document.getElementsByClassName("div1")[0];
        var gg = document.getElementsByClassName("div2")[0];
        var tim = document.getElementsByClassName("div3")[0];
        var sybt = document.getElementsByClassName("div4")[0];
        var vid = document.getElementsByTagName("video")[0];
        var jd = document.getElementById("ran");
        var sy = document.getElementsByClassName("sy")[0];
        var num = 60;

播放、暂停功能

第一次点击时播放,播放控件图标变为暂停,第二次点击时暂停,播放控件图标变为播放

        bf.addEventListener("click", function () {
            if (playvid == true) {
                vid.play();
                playvid = false;
                bf.style.backgroundImage = "url(暂停.png)"
            } else {
                vid.pause();
                bf.style.backgroundImage = " url(播放_wps图片.png)"
                playvid = true;
            }
        })

暂停时弹出广告

        vid.onplay = function () {
            gg.style.display = "none";
        }
        vid.onpause = function () {
            gg.style.display = "block";
        }

gg

进度条功能

 var timer = setInterval(function () {
                // 获取视频的总时长 播放的时间
                var total = vid.duration;
                var nowTime = vid.currentTime;
                // 获取进度条总宽度  20/60 * 总宽度
                // 当前播放节点  20
                // 总时长  60
                var owidth = nowTime / total * jd.clientWidth;

                // 改变进度条的位置
                jd.children[0].style.width = owidth + 'px';
                jd.children[1].style.left = owidth + 'px';
            }, 30);

计时功能实现

                // 将时间设置到页面
                if (parseInt(nowTime % 60) < 10) {
                    tim.innerHTML = parseInt(nowTime / 60) + ":" + "0" + parseInt(nowTime % 60) + "/" + "0" + parseInt(total / 60) + ":" + parseInt(total % 60) + "0"
                } else if (parseInt(nowTime % 60) >= 10 && parseInt(nowTime % 60) <= 60) {
                    tim.innerHTML = parseInt(nowTime / 60) + ":" + parseInt(nowTime % 60) + "/" + "0" + parseInt(total / 60) + ":" + parseInt(total % 60) + "0"
                }

声音控件

        sy.children[0].style.left = num + "px"
        vid.volume = num / sy.clientWidth;
          sy.onclick = function (e) {
            if (e.offsetX >= 0) {
                sy.children[0].style.left = e.offsetX + 'px';
                vid.volume = e.offsetX / sy.clientWidth;
                console.log(e.offsetX / sy.clientWidth)
                if(e.offsetX / sy.clientWidth==0||e.offsetX / sy.clientWidth==0.01) {
                    sybt.style.backgroundImage="url(禁音.png)"
                }else{
                    sybt.style.backgroundImage="url(声音.png)"
                }
            }

总结

本文主要根据本人测试视频编写,不同浏览器打开不同视频的显示效果不同,本文主要提供学习使用。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值