婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > html5新增的定時器requestAnimationFrame實現(xiàn)進(jìn)度條功能

html5新增的定時器requestAnimationFrame實現(xiàn)進(jìn)度條功能

熱門標(biāo)簽:漳州人工外呼系統(tǒng)排名 跟電銷機(jī)器人做同事 中紳電銷智能機(jī)器人 濟(jì)南辦理400電話 ai電銷機(jī)器人連接網(wǎng)關(guān) 鶴壁手機(jī)自動外呼系統(tǒng)怎么安裝 威海營銷外呼系統(tǒng)招商 農(nóng)村住宅地圖標(biāo)注 鄭州電銷外呼系統(tǒng)違法嗎

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

優(yōu)勢與特點:

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

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

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

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

如何使用requestAnimationFrame?

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

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

參數(shù)是一個回調(diào)函數(shù),返回值是一個整數(shù),用來表示定時器的編號.

<!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="關(guān)閉">
</body>
</html>

cancelAnimationFrame用來關(guān)閉定時器

這個方法需要處理兼容:

 簡單的兼容:

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

如果瀏覽器都不認(rèn)識AnimationFrame,就用setTimeout兼容.

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

一、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>

 

標(biāo)簽:甘南 文山 萍鄉(xiāng) 紅河 營口 惠州 蘇州 咸陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5新增的定時器requestAnimationFrame實現(xiàn)進(jìn)度條功能》,本文關(guān)鍵詞  html5,新增,的,定時器,requestAnimationFrame,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《html5新增的定時器requestAnimationFrame實現(xiàn)進(jìn)度條功能》相關(guān)的同類信息!
  • 本頁收集關(guān)于html5新增的定時器requestAnimationFrame實現(xiàn)進(jìn)度條功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 西乌珠穆沁旗| 渭南市| 永清县| 太原市| 墨玉县| 阳信县| 安吉县| 连州市| 巫溪县| 台湾省| 独山县| 定边县| 凉城县| 宁蒗| 正镶白旗| 青河县| 石阡县| 曲靖市| 福鼎市| 崇左市| 葵青区| 兖州市| 乡城县| 达日县| 崇信县| 昌吉市| 随州市| 乳山市| 昌宁县| 开化县| 安庆市| 北京市| 新乡市| 柯坪县| 建昌县| 吴忠市| 北辰区| 城市| 清河县| 视频| 姚安县|