From 7ed04565bb160c3df4629497ce298811dc97fecc Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Tue, 16 Apr 2013 22:35:55 +0000 Subject: [PATCH] Twenty Thirteen: further performance for functions.js scroll event callback, props obenland. See #23875. git-svn-id: http://core.svn.wordpress.org/trunk@24005 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../themes/twentythirteen/functions.php | 2 +- .../themes/twentythirteen/js/functions.js | 33 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/wp-content/themes/twentythirteen/functions.php b/wp-content/themes/twentythirteen/functions.php index 03fffcc096..41065405a0 100644 --- a/wp-content/themes/twentythirteen/functions.php +++ b/wp-content/themes/twentythirteen/functions.php @@ -223,7 +223,7 @@ function twentythirteen_scripts_styles() { wp_enqueue_script( 'jquery-masonry' ); // Loads JavaScript file with functionality specific to Twenty Thirteen. - wp_enqueue_script( 'twentythirteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20130211', true ); + wp_enqueue_script( 'twentythirteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20130416', true ); // Loads our main stylesheet. wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri() ); diff --git a/wp-content/themes/twentythirteen/js/functions.js b/wp-content/themes/twentythirteen/js/functions.js index d3f8c0e876..0c9a8d1c10 100644 --- a/wp-content/themes/twentythirteen/js/functions.js +++ b/wp-content/themes/twentythirteen/js/functions.js @@ -5,12 +5,13 @@ */ ( function( $ ) { - var html = $( 'html' ), - body = $( 'body' ), - navbar = $( '#navbar' ), - _window = $( window ), - navbarOffset = -1, - toolbarOffset = body.is( '.admin-bar' ) ? 28 : 0, + var html = $( 'html' ), + body = $( 'body' ), + navbar = $( '#navbar' ), + _window = $( window ), + toolbarOffset = body.is( '.admin-bar' ) ? 28 : 0, + navbarOffset = navbar.offset().top - toolbarOffset, + scrollOffsetMethod = ( typeof window.scrollY === 'undefined' ), adjustFooter, adjustAnchor; @@ -48,16 +49,16 @@ /** * Displays the fixed navbar based on screen position. */ - _window.on( 'scroll.twentythirteen', function() { - var scrollOffset = ( typeof window.scrollY === 'undefined' ) ? document.documentElement.scrollTop : window.scrollY; - if ( navbarOffset < 0 ) - navbarOffset = navbar.offset().top - toolbarOffset; - - if ( scrollOffset >= navbarOffset && _window.innerWidth() > 644 ) - html.addClass( 'navbar-fixed' ); - else - html.removeClass( 'navbar-fixed' ); - } ); + if ( _window.innerWidth() > 644 ) { + _window.on( 'scroll.twentythirteen', function() { + var scrollOffset = scrollOffsetMethod ? document.documentElement.scrollTop : window.scrollY; + + if ( scrollOffset > navbarOffset ) + html.addClass( 'navbar-fixed' ); + else + html.removeClass( 'navbar-fixed' ); + } ); + } /** * Allows clicking the navbar to scroll to top.