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

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


<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>Documenttitle>
    <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("");
                $('.videos').show();
            });
        });
        function close1() {
            var v = document.getElementById('video');//获取视频节点
            $('.videos').css("display", "none");//点击关闭按钮关闭暂停视频
            v.pause();
            $('.videos').html();
        }

    script>
body>

html>

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

你可能感兴趣的:(jQuery,video)