前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jQuery实现点击图片弹出视频并自动播放

jQuery实现点击图片弹出视频并自动播放

作者头像
明知山
发布2020-09-03 10:21:02
5.6K0
发布2020-09-03 10:21:02
举报
文章被收录于专栏:前端开发随笔

有时候我们需要放一张视频的封面,当我们点击封面的时候就播放弹出视频并自动播放,而且我们还可以把视频的封面做成视频加载前的封面,代码较少

代码语言:javascript
复制
<!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>
        .rol {
            width: 100%;
            height: 100%;
        }

        .banner {
            width: 100%;
            height: 100%;
            overflow-x: scroll;
            white-space: nowrap;
        }

        .one {
            position: relative;
            width: 48%;
            height: 88%;
            margin-right: 1%;
            display: inline-block;
        }

        .one img {
            width: 100%;
            height: 80%;
        }

        .text {
            position: absolute;
            font-size: 18px;
            color: #FFF;
            top: 83%;
            left: 5%;
            /* width: 55%;
      white-space: pre-wrap; */
        }

        .zol {
            width: 100%;
            height: 27%;
            position: absolute;
            top: 37%;
            left: 0;
        }

        .videos {
            display: none;
            position: fixed;
            left: 50%;
            top: 30%;
            width: 92%;
            z-index: 100;
            transform: translate(-50%, -50%);
        }

        .vclose {
            position: absolute;
            right: 1%;
            top: 1%;
            border-radius: 100%;
            width: 25px !important;
            height: 25px !important;
        }
    </style>
</head>

<body>
    <div class="rol">
        <div class="banner">
            <div class="one"
                vpath="http://sucai.suoluomei.cn/sucai_zs/images/20190816141812-%E5%85%83%E6%B0%94%E5%B0%91%E5%A5%B3%E5%A6%86.png"
                ipath="http://hpl-px-output.oss-cn-shenzhen.aliyuncs.com/mall_paparela/Public/xcwz/%E5%9B%9B%E6%AC%BE%E5%A6%86%E5%AE%B9%E6%95%99%E7%A8%8B%EF%BC%8C%E8%BD%BB%E6%9D%BEhold%E4%BD%8F%E5%90%84%E7%A7%8D%E5%9C%BA%E5%90%88/5%E5%88%86%E9%92%9F%E5%85%83%E6%B0%94%E5%B0%91%E5%A5%B3%E5%A6%86.mp4">
                <img src="http://sucai.suoluomei.cn/sucai_zs/images/20190816141812-%E5%85%83%E6%B0%94%E5%B0%91%E5%A5%B3%E5%A6%86.png"
                    alt="1">
            </div>
            <div class="one"
                vpath="http://sucai.suoluomei.cn/sucai_zs/images/20190816141812-%E4%B8%AA%E6%80%A7%E6%BD%AE%E4%BA%BA%E5%A6%86.png"
                ipath="http://hpl-px-output.oss-cn-shenzhen.aliyuncs.com/mall_paparela/Public/xcwz/%E5%9B%9B%E6%AC%BE%E5%A6%86%E5%AE%B9%E6%95%99%E7%A8%8B%EF%BC%8C%E8%BD%BB%E6%9D%BEhold%E4%BD%8F%E5%90%84%E7%A7%8D%E5%9C%BA%E5%90%88/%E4%B8%AA%E6%80%A7%E6%BD%AE%E4%BA%BA%E5%A6%86%EF%BC%8C%E6%8B%92%E7%BB%9D%E8%A2%AB%E6%A0%87%E7%AD%BE.mp4">
                <img src="http://sucai.suoluomei.cn/sucai_zs/images/20190816141812-%E4%B8%AA%E6%80%A7%E6%BD%AE%E4%BA%BA%E5%A6%86.png"
                    alt="2">
            </div>
        </div>
        <div class="videos"></div>
    </div>
    <script src="http://cdn.suoluomei.com/common/js/jquery-2.1.4.min.js "></script>
    <script>
        $('.one').each(function () { //遍历视频列表
            $(this).click(function () { //这个视频被点击后执行
                var img = $(this).attr('vpath');//获取视频预览图
                var video = $(this).attr('ipath');//获取视频路径
                $('.videos').html("<video id=\"video\" poster='" + img + "' style='width: 100%;' src='" + video + "' preload=\"auto\" controls=\"controls\" autoplay=\"autoplay\"></video><img onClick=\"close1()\" class=\"vclose\" src=\"http://sucai.suoluomei.cn/sucai_zs/images/20190816134237-%E5%85%B3%E9%97%AD%20%281%29.png\"/>");
                $('.videos').show();
            });
        });
        function close1() {
            var v = document.getElementById('video');//获取视频节点
            $('.videos').css("display", "none");//点击关闭按钮关闭暂停视频
            v.pause();
            $('.videos').html();
        }

    </script>
</body>

</html>

vpath是设置的视频封面 ipath是视频地址 直接copy看效果,这是移动端的,尺寸单位用的百分比,需要的话直接改样式就好了

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档