Javascript - 滚动事件的效率,只运行一次代码

时间:2013-06-19 09:03:37

标签: javascript performance if-statement

这些if语句总让我头晕目眩。如果页面带有标题,并且标记上方有隐藏标题。

<div id="headerfloat" style="display:none;">
    <p>Floated header</p>
</div>
<div id="header">
    <p>Header</p>
</div>

这个想法是,每当页面向下滚动超过225px时,#headerfloat应该出现,并且当topScroll小于225px时消失。我设法使用javascript和jQuery,但是当我在iPhone上测试它时,它非常缓慢。而且我很确定这是因为代码是在每个滚动事件中运行的。即使#headerfloat可见,代码仍会执行。即使它没有那个时候。

因此,我需要确保代码只在需要时运行一次。我的第一个想法是添加和删除.open.closed等类到#headerfloat。并在滚动事件期间运行if语句。但这是最有效的方式吗?

我到目前为止,丑陋的片段:

jQuery(window).scroll(function () {
    var headerfloat = $("#header_float");
    var top = jQuery(window).scrollTop();
    if (top > 225) // height of float header
    {
        if (!$(headerfloat).hasClass("closed")) {
            $(headerfloat).addClass("boxshadow", "", 100, "easeInOutQuad").slideDown().addClass("open").removeClass("closed");
        }
    } else {
        $(headerfloat).removeClass("boxshadow", "", 100, "easeInOutQuad").removeClass("closed").slideUp();
    }
});

编辑:所以在laconbass的精彩回应之后,这是我最终得到的代码:

var mainHeader = $('#header')
          , top_limit = mainHeader.outerHeight()
          , $window = $(window)
        ;

        var header_float = $('#header_float')

        bindEvent();

        function bindEvent() {
            $window.scroll( scrollEvent );
        }

        function scrollEvent() {
            var top = $window.scrollTop();
            // avoid any logic if nothing must be done
            if ( top < top_limit && !header_float.is(':visible')
                || top > top_limit && header_float.is(':visible')
            ) return;
            // unbind the scroll event to avoid its execution
            // until slide animation is complete
            $window.unbind( 'scroll' );
            // show/hide the header
            if ( top > top_limit ) {
                header_float.slideDown( 400, bindEvent );
            } else {
                header_float.slideUp( 400, bindEvent );
            }
        };

2 个答案:

答案 0 :(得分:2)

你开始的片段看起来有点难看。

I've made one on jsfiddle for your pleasure and reference

我假设了以下内容:

  • 当页面向下滚动时,您想要一个fixed定位标题(又名fixed header)。
  • fixed headed是页面主标头的副本,类为fixed
  • 当页面向下滚动超过标题高度时,会显示
  • fixed header
  • 当页面向上滚动足以显示主页面标题时,
  • fixed header被隐藏。

效果提示:

  • 缓存jQuery对象,以避免每次执行事件处理程序时都生成新查询。
  • 在显示/隐藏动画之前取消绑定事件处理程序,然后重新绑定它。
  • 在事件处理程序上,尽快返回以避免不必要的逻辑。请记住,在执行JavaScript时,浏览器呈现过程将被阻止。
var mainHeader = $('header')
  , header = mainHeader.clone().addClass('fixed').appendTo('body')
  , top_limit = header.outerHeight()
;

bindEvents();

function bindEvents() {
    $(window).scroll( scrollEvent );
}

function scrollEvent() {
    var top = $(window).scrollTop();
    // avoid any logic if nothing must be done
    if ( top < top_limit && !header.is(':visible')
        || top > top_limit && header.is(':visible')
    ) return;
    // unbind the scroll event to avoid its execution
    // until slide animation is complete
    $(window).unbind( 'scroll' );
    // show/hide the header
    if ( top > top_limit ) {
        header.slideDown( 400, bindEvents );
    } else {
        header.slideUp( 400, bindEvents );
    }
};
<header>
    <h1>Awesome header</h1>
</header>
<div>
    <!-- the page content -->
</div>
/* the real code needed */
header.fixed {
    display: none;
    position: fixed;
    top: 0;
}

答案 1 :(得分:0)

双向:

1,在滚动时,如果您已完成所需,请删除滚动事件。

2,var变量,默认为false,滚动时,如果变量为false,你想要,将变量设置为true ;如果变量已经为真,则不执行任何操作(或其他任何操作)

相关问题