IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> html5 网页视频水印 | js代码实现示例 -> 正文阅读

[JavaScript知识库]html5 网页视频水印 | js代码实现示例

示例图

原理

通过html的页面布局,在video播放器层面,通过js控制dom,插入文本水印代码。

代码示例

index.html

<!DOCTYPE html>
<html>
<head>
	<meta name="renderer" content="webkit" />
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>html5视频水印</title>
	<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
  <link rel="stylesheet" type="text/css" href="css/waterMarkVideo.css">
</head>
<body>
  <div id="container">
            <div style="position: absolute; width: 100%; left: 0; top: 0;height:100%;overflow:hidden">
              <div class="watermark video-watermark-multiple video-watermark-item">
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
              <div class="noselect"><span class="span"><span></div>
            </div>
             <video width="640px" height="360px">
                 <source src="https://blz-videos.nosdn.127.net/1/OverWatch/AnimatedShots/Overwatch_TheatricalTeaser_WeAreOverwatch_zhCN.mp4" type="video/mp4"/>
             </video>
             <div class="pos">
                 <img src=" img/i-play.png" width="40px" id="playBtn" />
                 <div style="padding-left:80px;padding-right:250px;">
                     <progress  value="0" id="progress"></progress>
                 </div>
                 <div class="timebox">
                     <span id="time1">
                         00:00:00
                     </span>/
                     <span id="time2">
                         00:00:00
                     </span>
                 </div>
                 <img src="img/i-fs.png" width="35px" id="full"/>
            </div>
         </div>
  </div> 
<script type="text/javascript">
    $(".span").html("water-mark");
    var video = document.querySelector("video");
    var playBtn = document.getElementById("playBtn");
    playBtn.onclick = function(){
        if(video.paused){
            video.play();
            playBtn.src = "img/i-pause.png";
        }else{
            video.pause();
            playBtn.src = "img/i-play.png";
       }
    }
    var full = document.getElementById("full");
    var container = document.getElementById("container");
    var i = 1;//定义一个变量,用于记录是否为全屏状态
    full.onclick = function(){
       i++;//对2取余,为0则全屏,否则关闭全屏
       if(i%2==0){//点击开启全屏
           if(container.requestFullScreen){
                container.requestFullScreen()
           }else if(container.webkitRequestFullScreen){
                container.webkitRequestFullScreen()//谷歌
           }else if(container.mozRequestFullScreen){
                container.mozRequestFullScreen()//火狐
           }else if(container.msRequestFullscreen){
                container.msRequestFullscreen()//ie
           }
           $('video').css({"width":"100%","height":"100%"});
           $(".pos").css("width","100%");
           full.src = "img/i-exitfs.png";
       }else{//关闭全屏
           if(document.cancelFullscreen){
                document.cancelFullscreen();
           }else if(document.webkitCancelFullScreen){
                document.webkitCancelFullScreen();
           }else if(document.mozCancelFullScreen){
                document.mozCancelFullScreen();
           }else if(document.msExitFullscreen){
                document.msExitFullscreen();
           }
           $('video').css({"width":"640px","height":"360px"});
           $(".pos").css("width","640px");
           full.src = "img/i-fs.png";
       }
        
    }
    function number1(s){
            if(s<10){
                s="0"+s
            }else{
                s=""+s
            }
            return s;
    }
    var time1 = document.getElementById("time1");
    var time2 = document.getElementById("time2");
    var t1;
     //视频获取时间单位为秒,要对它进行一些数据的处理
    video.oncanplay = function(){  
         t1 = video.duration;//获取视频总时长
         var h = parseInt(t1/3600);
         var m = parseInt(t1%3600/60);
         var s = parseInt(t1%60);    
         time2.innerHTML = number1(h) + ":"+ number1(m) + ":" + number1(s);//值显示在span标签中
     }
     //获取当前视频播放的时间
    video.ontimeupdate = function(){
         var t2 = video.currentTime;
         var h = parseInt(t2/3600);
         var m = parseInt(t2%3600/60);
         var s = parseInt(t2%60);
         time1.innerHTML = number1(h) + ":"+ number1(m) + ":" + number1(s);
         progress.max = t2;
         progress.value = t2/t1*progress.max;
    }
    video.onended = function(){
        progress.value = 0;     
        time1.innerHTML = "00"+":"+"00"+":"+"00";//播放结束时时间清0;
        playBtn.src = "img/i-play.png";//播放结束时按钮恢复
    }
              
    progress.onclick = function(e){
          //获取当前位置距离父级最左边的距离
          //left = progress.offsetLeft;
          //获取鼠标距离屏幕最左边的距离
          //e.clientX
          //获取鼠标距离当前元素最左边的距离
           var left  = e.offsetX;
           console.log(left);
           console.log(t1);
           video.currentTime = left/360*t1;
    }
</script>
</body>
</html>

?核心代码

$(".span").html("water-mark");

"water-mark"字段添加要打的水印内容。也可以动态添加水印内容。

样式waterMarkVideo.css代码

body,div,img{
    margin: 0;
    padding: 0;
    border: 0;
}
#container{
    width: 640px;
   border: 1px solid pink;
}
.pos{
   width: 640px;
   height: 60px;
   background: rgba(0,0,0,0.5);
   margin-top: -63px;
   position: relative;
   
}
#playBtn{
   position:relative;
   top: 10px;
   left: 20px;
   cursor: pointer;
}
#progress{
   height: 12px;
   width: 100%;
   position: relative;
   top: -27px;
   cursor: pointer;
   
}
.timebox{
   position:absolute;
   top: 20px;
   right: 80px;
   cursor: pointer;
   color: white;
}
#full{
   position:absolute;
   top: 12px;
   right: 20px;
   cursor: pointer;
}
.video-watermark-item {
    left: 15%;
    top: 10%;
    font-size: 20px;
    color: rgba(240,240,240,0.3);
    font-family: 方正黑体;
    -o-transform: translate(-15%, -50%) rotate(-35deg);
    -webkit-transform: translate(-15%, -50%) rotate(-35deg);
    -moz-transform: translate(-15%, -50%) rotate(-35deg);
    -ms-transform: translate(-15%, -50%) rotate(-35deg);
    transform: translate(-15%, -50%) rotate(-35deg);
    position: absolute;
    overflow: hidden;
}
.noselect {
  display: inline-block;
  padding: 100px;
}

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-02-16 12:59:54  更:2022-02-16 13:00:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 11:00:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码