好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > html5新增的定時器requestAnimationFrame實現進度條功能

html5新增的定時器requestAnimationFrame實現進度條功能

熱門標簽:漳州人工外呼系統排名 跟電銷機器人做同事 中紳電銷智能機器人 濟南辦理400電話 ai電銷機器人連接網關 鶴壁手機自動外呼系統怎么安裝 威海營銷外呼系統招商 農村住宅地圖標注 鄭州電銷外呼系統違法嗎

在requestAnimationFrame出現之前,我們一般都用setTimeout和setInterval,那么html5為什么新增一個requestAnimationFrame,他的出現是為了解決什么問題?

優勢與特點:

1)requestAnimationFrame會把每一幀中的所有DOM操作集中起來,在一次重繪或回流中就完成,并且重繪或回流的時間間隔緊緊跟隨瀏覽器的刷新頻率

2)在隱藏或不可見的元素中,requestAnimationFrame將不會進行重繪或回流,這當然就意味著更少的CPU、GPU和內存使用量

3)requestAnimationFrame是由瀏覽器專門為動畫提供的API,在運行時瀏覽器會自動優化方法的調用,并且如果頁面不是激活狀態下的話,動畫會自動暫停,有效節省了CPU開銷

一句話就是:這玩意性能高,不會卡屏,根據不同的瀏覽器自動調整幀率。如果看不懂或者不理解,也沒有什么關系,這玩意跟瀏覽器渲染原理有關。我們先學會使用它!

如何使用requestAnimationFrame?

使用方式跟定時器setTimeout差不多,不同之處在于,他不需要設置時間間隔參數

     var timer = requestAnimationFrame( function(){
            console.log( '定時器代碼' );
        } );

參數是一個回調函數,返回值是一個整數,用來表示定時器的編號.

<!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>
    <script>
        window.onload = function(){
            var aInput = document.querySelectorAll( "input" ),
                timer = null;
            aInput[0].onclick = function(){
                timer = requestAnimationFrame( function say(){
                    console.log( 1 );
                    timer = requestAnimationFrame( say );
                } );
            };
            aInput[1].onclick = function(){
                cancelAnimationFrame( timer );
            }
        }
    </script>
</head>
<body>
    <input type="button" value="開啟">
    <input type="button" value="關閉">
</body>
</html>

cancelAnimationFrame用來關閉定時器

這個方法需要處理兼容:

 簡單的兼容:

 window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

如果瀏覽器都不認識AnimationFrame,就用setTimeout兼容.

運用3種不同的定時器(setTimeout, setInterval, requestAnimationFrame)實現一個進度條的加載

一、setInterval方式:

<!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>
        div{
            width:0px;
            height:40px;
            border-radius:20px;
            background:#09f;
            text-align:center;
            font:bold 30px/40px '微軟雅黑';
            color:white;
        }
    </style>
    <script>
        window.onload = function(){
            var oBtn = document.querySelector( "input" ),
                oBox = document.querySelector( "div" ),
                timer = null, curWidth = 0,
                getStyle = function( obj, name, value ){
                    if( obj.currentStyle ) {
                        return obj.currentStyle[name];
                    }else {
                        return getComputedStyle( obj, false )[name];
                    }
                };
            oBtn.onclick = function(){
                clearInterval( timer );
                oBox.style.width = '0';
                timer = setInterval( function(){
                    curWidth = parseInt( getStyle( oBox, 'width' ) );
                    if ( curWidth < 1000 ) {
                        oBox.style.width = oBox.offsetWidth + 10 + 'px';
                        oBox.innerHTML = parseInt( getStyle( oBox, 'width' ) ) / 10 + '%';
                    }else {
                        clearInterval( timer );
                    }
                }, 1000 / 60 );
            }
        }
    </script>
</head>
<body>
    <div>0%</div>
    <p><input type="button" value="ready!Go"></p>
</body>
</html>

二、setTimeout方式

<script>
        window.onload = function(){
            var oBtn = document.querySelector( "input" ),
                oBox = document.querySelector( "div" ),
                timer = null, curWidth = 0,
                getStyle = function( obj, name, value ){
                    if( obj.currentStyle ) {
                        return obj.currentStyle[name];
                    }else {
                        return getComputedStyle( obj, false )[name];
                    }
                };
            oBtn.onclick = function(){
                clearTimeout( timer );
                oBox.style.width = '0';
                timer = setTimeout( function go(){
                    curWidth = parseInt( getStyle( oBox, 'width' ) );
                    if ( curWidth < 1000 ) {
                        oBox.style.width = oBox.offsetWidth + 10 + 'px';
                        oBox.innerHTML = parseInt( getStyle( oBox, 'width' ) ) / 10 + '%';
                        timer = setTimeout( go, 1000 / 60 );
                    }else {
                        clearInterval( timer );
                    }
                }, 1000 / 60 );
            }
        }
    </script>

    三、requestAnimationFrame方式   

 <!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>
        div{
            width:0px;
            height:40px;
            border-radius:20px;
            background:#09f;
            text-align:center;
            font:bold 30px/40px '微軟雅黑';
            color:white;
        }
    </style>
    <script>
        window.onload = function(){
            var oBtn = document.querySelector( "input" ),
                oBox = document.querySelector( "div" ),
                timer = null, curWidth = 0,
                getStyle = function( obj, name, value ){
                    if( obj.currentStyle ) {
                        return obj.currentStyle[name];
                    }else {
                        return getComputedStyle( obj, false )[name];
                    }
                };
            oBtn.onclick = function(){
                cancelAnimationFrame( timer );
                oBox.style.width = '0';
                timer = requestAnimationFrame( function go(){
                    curWidth = parseInt( getStyle( oBox, 'width' ) );
                    if ( curWidth < 1000 ) {
                        oBox.style.width = oBox.offsetWidth + 10 + 'px';
                        oBox.innerHTML = parseInt( getStyle( oBox, 'width' ) ) / 10 + '%';
                        timer = requestAnimationFrame( go );
                    }else {
                        cancelAnimationFrame( timer );
                    }
                } );
            }
        }
    </script>
</head>
<body>
    <div>0%</div>
    <p><input type="button" value="ready!Go"></p>
</body>
</html>

 

標簽:甘南 文山 萍鄉 紅河 營口 惠州 蘇州 咸陽

巨人網絡通訊聲明:本文標題《html5新增的定時器requestAnimationFrame實現進度條功能》,本文關鍵詞  html5,新增,的,定時器,requestAnimationFrame,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《html5新增的定時器requestAnimationFrame實現進度條功能》相關的同類信息!
  • 本頁收集關于html5新增的定時器requestAnimationFrame實現進度條功能的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 免费一级肉体全黄毛片| 看黄a夫片爽爽影院| 中国一级片免费看| 亚洲成年网| 亚洲精品人成中文无码毛片A| 欧美vpswindows极品| 男人扒女人内裤肌肌对肌肌视频| zoofilia杂交videos新另类| 快穿堵住不许流出来h| 调教老师(高H,调教)| 青娱乐视觉极品盛宴| avtt手机天堂网一本热系列| 撕开美女衣服免费吃胸视频| 太深?拔出来?要高潮了视频| 韩国主播vip福利视频19禁| 69欧美另类xxxxx高清| 国产乡下三级农村妇女| 欧美精品无套玩弄人妻| 中国桃色cameltoe| 亚洲国产精华推荐单单品榜| 亚洲AV无码秘?希岛爱理| 亚洲精品女同午夜在线| HD法国骚妇口射| 欧美办公室激情videos高清| 竹菊精品视频一区二区| 白丝护士把我夹的好爽视频| 久久精品日产第一区二区三区的功能| 粉嫩在线观看| 国产又黄又粗又猛又爽的学生视频| 国产美女一级毛片久久| 久操-9c.lu| 成人小视频在线播放| 被邻居侵犯调教成了里番acg| 超碰cao草棚gao进入81国| 一女多男NP爽文| 午夜精品一级A片免费播| 公交车H系列辣文n| free性欧美贵妇ⅩXXOO| 俄罗斯大白腚| 国产91对白在线| 欧美激情精品久久久久久免费印度|