亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

  發(fā)布時間:2018-12-13 16:11:35   作者:佚名   我要評論
html5為什么新增一個requestAnimationFrame,他的出現是為了解決什么問題?帶著這些問題跟隨腳本之家小編一起學習吧

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

優(yōu)勢與特點:

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

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

3)requestAnimationFrame是由瀏覽器專門為動畫提供的API,在運行時瀏覽器會自動優(yōu)化方法的調用,并且如果頁面不是激活狀態(tài)下的話,動畫會自動暫停,有效節(jié)省了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給漢字加拼音加進度條的實現代碼

    這篇文章主要介紹了html5給漢字加拼音加進度條的實現代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一
    2020-04-07
  • 基于HTML5 SVG實現的圓形滑塊進度條特效源碼

    基于HTML5 SVG實現的圓形滑塊進度條特效源碼是一段通過滑塊拖動控制進度條數值,默認支持設置進度條最大數值。非常有意思,歡迎有興趣的朋友前來下載使用
    2019-11-25
  • HTML5超炫酷粒子效果的進度條的實現示例

    這篇文章主要介紹了HTML5超炫酷粒子效果的進度條的實現示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來
    2019-08-23
  • HTML5觸摸事件實現移動端簡易進度條的實現方法

    這篇文章主要介紹了HTML5觸摸事件實現移動端簡易進度條的實現方法的相關資料,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-04
  • HTML5實現自帶進度條和滑塊滑桿效果

    本文給大家介紹了HTML5實現自帶進度條和滑塊滑桿效果,運用progress標簽,設置好min和max數值就好,可以用value獲取其中的進度值。具體實現代碼大家參考下本文
    2018-04-17
  • HTML5實現儀表盤進度條特效代碼

    儀表盤進度條HTML5特效是一款基于HTML5 Canvas制作的自定義三個儀表盤進度條,非常不錯,喜歡的朋友快來下載體驗吧
    2020-09-25

最新評論