js控制video控件禁止快進(jìn) 但是可以后退 完美解決

我與寂寞對(duì)坐卷中,荒唐的是我比寂寞更寂寞崩溪。

最近項(xiàng)目接了個(gè)新需求 就是 在項(xiàng)目中(APP)視頻播放,不可以快進(jìn),但事可以后退,在后退成功的時(shí)候,還可以快進(jìn)到自然播放的最大時(shí)間處, 我翻遍所有的api, 發(fā)現(xiàn)并沒有這個(gè)api 也沒有解決方案,所以我覺定自己寫一個(gè)video控件

<!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>
    <link rel="stylesheet" href="./font_m6b36j857h/demo.css">
    <link rel="stylesheet" href="./font_m6b36j857h/iconfont.css">
    
    <style>
        .container {
            position: relative;
            width: 640px;
            height: 360px;
            border: 1px solid red;
            margin: 100px auto;
        }
        
        .container video {
            width: 100%;
            height: 100%;
        }
        .container  .control {
            position: absolute;
            left: 20px;
            bottom: 10px;
            width: 600px;
            height: 30px;
            
            background: rgba(0, 0, 0, .5)
            
        }
        .control .switch {
            position: absolute;
            left: 10px;
            top: 5px;

        }
        .control .icon {
            cursor: pointer;
           color: #fff;
           font-size: 20px;
        }
        .control .process {
            position: absolute;
            left: 36px;
            top: 7px;
            width: 350px;
            height: 18px;
            border-radius: 9px;
            background-color: #555;
            overflow: hidden;
            cursor: pointer;

        }
        .control .process .bar {
            position: absolute;
            left: 0;
            top: 0;
            width: 0px;
            height: 100%;
            background-color: #fff;
        }
        .control .time {
            position: absolute;
            right: 60px;
            top: 6px;
            color: #fff;
        }
        .control .sound {
            position: absolute;
            right: 33px;
            top: 6px;
            
        }
        .control .full {
            position: absolute;
            right: 6px;
            top: 6px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <video src="https://act.mcake.com/fangli/2018/pc/zhou-nian/video/zhounian-7.mp4" poster="./images/poster.jpg"></video>
        <div class="control">
            <i class="icon icon-play switch" id="switch">123</i>
            <div class="process">
                <div class="bar"></div>
            </div>
            <div class="time">
                <span>00:00:00</span> /
                <span>00:00:00</span> 
            </div>
            <i class="icon icon-sound sound">123</i>
            <i class="icon iconfont full" id="full">123</i>
        </div>
    </div>
    <script>    
        var btn = document.querySelector('#switch');
        var video = document.querySelector('video');
        var spans = document.querySelectorAll('.control .time span');
        var process = document.querySelector('.process');
        var bar = document.getElementsByClassName('bar')[0];
        var full = document.querySelector('#full');
        // console.log(spans[0]);
        var total = 0;
        var processWidth = process.offsetWidth;
        // console.log(processWidth);
        var currenttime = undefined;
        var endbarwidth = undefined;

        var num = 1;
        
        //視頻開始和暫停切換
        btn.onclick = function (){
            // console.log(video.paused);
            if(video.paused) {
                video.play();
                this.classList.remove('icon-play');
                this.classList.add('icon-pause');
            } else {
                video.pause();
                this.classList.remove('icon-pause');
                this.classList.add('icon-play');
            }
        }
        
        //獲取視頻時(shí)間
        //當(dāng)視頻可以播放的時(shí)候才可以獲取不然獲取不到
        video.oncanplay = function (){
            total = video.duration;
            
            // console.log(total); //以秒為單位
            spans[1].innerHTML = getTime(total);
            // console.log(spans[1]);
        }
        //當(dāng)視頻播放
        video.ontimeupdate = function(){
            currenttime = video.currentTime;
            spans[0].innerHTML = getTime(currenttime);
            barWidth    = processWidth * currenttime / total; // 當(dāng)前時(shí)間
            endbarwidth = processWidth * currenttime / total; // 記錄最長時(shí)間
            bar.style.width = barWidth + 'px';
        }

        //點(diǎn)擊進(jìn)度條下面的凹槽 也算是障眼法
        process.onclick = function (e) {
            num += 1;
            if ( num == 2) {
                localStorage.setItem('endbarwidth', endbarwidth);
            }
            console.log(e.offsetX);
            var losW = undefined;
            if (localStorage.getItem('endbarwidth')) {
                losW = parseFloat(localStorage.getItem('endbarwidth'))
            }
            console.log(losW);
            if (e.offsetX < losW) { // 
                video.currentTime = total * e.offsetX / processWidth;
            } else if (e.offsetX > barWidth) {
                return;
            } else {
                video.currentTime = total * e.offsetX / processWidth;
            }
        }

        //視頻結(jié)束時(shí)
        video.onended = function (){
            video.currentTime = 0;
            btn.classList.remove('icon-pause');
            btn.classList.add('icon-play');
        }
        
        //全屏開啟
        full.onclick = function(){
            if(video.webkitRequestFullScreen){
                video.webkitRequestFullScreen();
            } else if (video.mozRequestFullScreen){
                video.mozRequestFullScreen();
            } else if (video.msRequestFullScreen) {
                video.msRequestFullScreen();
            } else if (video.oRequestFullScreen){
                video.oRequestFullScreen();
            } else {
                video.requestFullScreen();
            }
            
        }
        

        function getTime(time){
            var hours = Math.floor(time%86400/3600);

            var minutes = Math.floor(time%86400%3600/60);

            var seconds = Math.floor(time%60);
            // console.log(seconds);

            hours = hours > 10 ? hours : '0' + hours;
            minutes = minutes > 10 ? minutes : '0' + minutes;
            seconds = seconds > 10 ? seconds : '0' + seconds;

            var str = '';
            str = hours + ':' + minutes + ':' + seconds;
            // console.log(str);
            return str;
        }
    </script>
</body>
</html>

這就是普通js寫的 后續(xù)我會(huì)將此代碼移到 Vue.js里面 可以關(guān)注一下 這個(gè)代碼貼過去就能用 , 如果覺的不好的話 可以自己寫一個(gè)

我的QQ: 2489757828 有問題的話可以一同探討
我的私人博客: 李繼玄

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市因俐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖领曼,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蛮穿,居然都是意外死亡庶骄,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門绪撵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瓢姻,“玉大人,你說我怎么就攤上這事音诈』眉睿” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵细溅,是天一觀的道長褥傍。 經(jīng)常有香客問我,道長喇聊,這世上最難降的妖魔是什么恍风? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮誓篱,結(jié)果婚禮上朋贬,老公的妹妹穿的比我還像新娘。我一直安慰自己窜骄,他們只是感情好锦募,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著邻遏,像睡著了一般糠亩。 火紅的嫁衣襯著肌膚如雪虐骑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天赎线,我揣著相機(jī)與錄音廷没,去河邊找鬼。 笑死垂寥,一個(gè)胖子當(dāng)著我的面吹牛颠黎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播矫废,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼盏缤,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了蓖扑?” 一聲冷哼從身側(cè)響起唉铜,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎律杠,沒想到半個(gè)月后潭流,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡柜去,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年灰嫉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嗓奢。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡讼撒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出股耽,到底是詐尸還是另有隱情根盒,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布物蝙,位于F島的核電站炎滞,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏诬乞。R本人自食惡果不足惜册赛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望震嫉。 院中可真熱鬧森瘪,春花似錦、人聲如沸票堵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽换衬。三九已至痰驱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瞳浦,已是汗流浹背担映。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叫潦,地道東北人蝇完。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像矗蕊,于是被迫代替她去往敵國和親短蜕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 教程一:視頻截圖(Tutorial 01: Making Screencaps) 首先我們需要了解視頻文件的一些基...
    90后的思維閱讀 4,698評(píng)論 0 3
  • 寫在前面 現(xiàn)在視頻業(yè)務(wù)越來越流行了傻咖,播放器也比較多朋魔,作為前端工程師如何打造一個(gè)屬于自己的播放器呢?最快最有效的方式...
  • 奔向遠(yuǎn)方 那只是年少輕狂 不管你是否回頭 背后總有倆束熾熱的目光 我們總是一路向前奔走 那目光一路追隨 直至遙不可...
    佛系又陽光閱讀 116評(píng)論 0 1
  • 22/3 Has been wanted to start to write blog for so long. ...
    快樂很簡(jiǎn)單閱讀 234評(píng)論 0 0
  • 總是愛無病呻吟 好比每次翻朋友圈會(huì)發(fā)現(xiàn)每個(gè)人的變化 眼睛變好看了 工作似乎很有樂趣 去哪里玩真好 看我自己好像是一...
    肉醬味芝士閱讀 176評(píng)論 0 0