window resize 事件 执行优化

浏览器窗口变化了1像素时就触发resize事件,然后随着变化不断重复触发。应该注意不要在这个事件的处理程序中加入大计算量的代码,因为这些代码有可能被频繁执行,从而导致浏览器反映明显变慢。

 

如果必须加则让resize事件按指定频率执行。避免不断执行。

-----------------------------------例子----------------------------------------------------------------------------------

<!DOCTYPE html>
<html>
    <head>
        <title>resize 频率</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
        <style type="text/css">
            body
            {
                margin: 0px;
                padding: 0px;
                display:-moz-box;
                display:-webkit-box;
                display:box;
                -moz-box-orient:vertical;
                -webkit-box-orient:vertical;
                box-orient:vertical;
                width: 100%;
                overflow: hidden;
            }
            body>header
            {
                    height:200px;
                    background-color: #338822;
            }
            body>footer
            {
                    height:100px;
                     background-color: #338822;
            }
            body>div
            {
                    -moz-box-flex:1;
                    -webkit-box-flex:1;
                    box-flex:1;
                     background-color: #778822;
            }
        </style>
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded",function(event){
                window.resizeWait = false;
                window.resizeInterval = 500;
                var setfullScreen=function(){
                     document.body.style.height=document.documentElement.clientHeight+"px";
                }
                var resize = function resize(event){
                    if(!window.resizeWait){
                        window.resizeWait = true;
                        setTimeout(function(){
                           setfullScreen();
                            window.resizeWait = false;
                        },1000);
                    }
                };
                window.addEventListener("resize",resize,false);
                setfullScreen();
            },false);
        </script>
    </head>
    <body>
        <header>标题</header>
        <div>
            <article>
                <h1>svg实现</h1>
                <section>
                    <nav>标题</nav>
                </section>
            </article>
            <article>
                <h1>css实现</h1>
            </article>
            <article>
                <h1>canvas实现</h1>
            </article>
        </div>
        <footer>COPYRIGHT@张森</footer>
    </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值