From 5c2650abc89d033e85386f51de69bf8a1c96235b Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Mon, 6 Feb 2012 23:17:28 +0000 Subject: [PATCH] Click empty toolbar to scroll to top, props ericmann, fixes #18758 git-svn-id: https://develop.svn.wordpress.org/trunk@19846 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/js/admin-bar.dev.js | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/wp-includes/js/admin-bar.dev.js b/wp-includes/js/admin-bar.dev.js index 5191fc5843..0479c56cd4 100644 --- a/wp-includes/js/admin-bar.dev.js +++ b/wp-includes/js/admin-bar.dev.js @@ -59,6 +59,14 @@ if ( typeof(jQuery) != 'undefined' ) { target.siblings('.ab-sub-wrapper').find('.ab-item').each(refresh); }); + $('#wpadminbar').click( function(e) { + if ( e.target.id != 'wpadminbar' && e.target.id != 'wp-admin-bar-top-secondary' ) + return; + + e.preventDefault(); + $('html, body').animate({ scrollTop: 0 }, 'fast'); + }); + }); } else { (function(d, w) { @@ -178,6 +186,40 @@ if ( typeof(jQuery) != 'undefined' ) { } } return false; + }, + + scrollToTop = function(t) { + var distance, speed, step, steps, timer, speed_step; + + // Ensure that the #wpadminbar was the target of the click. + if ( t.id != 'wpadminbar' && t.id != 'wp-admin-bar-top-secondary' ) + return; + + distance = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; + + if ( distance < 1 ) + return; + + speed_step = distance > 800 ? 130 : 100; + speed = Math.min( 12, Math.round( distance / speed_step ) ); + step = distance > 800 ? Math.round( distance / 30 ) : Math.round( distance / 20 ); + steps = []; + timer = 0; + + // Animate scrolling to the top of the page by generating steps to + // the top of the page and shifting to each step at a set interval. + while ( distance ) { + distance -= step; + if ( distance < 0 ) + distance = 0; + steps.push( distance ); + + setTimeout( function() { + window.scrollTo( 0, steps.shift() ); + }, timer * speed ); + + timer++; + } }; addEvent(w, 'load', function() { @@ -198,6 +240,10 @@ if ( typeof(jQuery) != 'undefined' ) { }); addEvent(aB, 'click', clickShortlink ); + + addEvent(aB, 'click', function(e) { + scrollToTop( e.target || e.srcElement ); + }); } if ( w.location.hash )