From 69c76ba266a62c75274a234c8e7c9a98704b592c Mon Sep 17 00:00:00 2001 From: jtsternberg Date: Fri, 15 May 2015 14:48:35 +0000 Subject: [PATCH] Prevent browser windows from moving/blinking on initial click of anchor link. Props [@ImBigWill](https://github.com/WebDevStudios/Hash-Link-Scroll-Offset/pull/9). git-svn-id: https://plugins.svn.wordpress.org/hash-link-scroll-offset/trunk@1161234 b8457f37-d9ea-0310-8a92-e5e31aec5664 --- Gruntfile.js | 15 +++++++-------- assets/js/hash-link-scroll-offset.js | 10 +++++++--- assets/js/hash-link-scroll-offset.min.js | 4 ++-- assets/js/src/hash-link-scroll-offset.js | 8 ++++++-- hash-link-scroll-offset.php | 2 +- readme.txt | 10 ++++++++-- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 5b5a205..15981de 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -64,7 +64,7 @@ module.exports = function( grunt ) { test: { files: ['assets/js/test/**/*.js'] }, - + cssmin: { options: { banner: '/*! <%= pkg.title %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' + @@ -75,16 +75,16 @@ module.exports = function( grunt ) { }, minify: { expand: true, - + cwd: 'assets/css/src/', src: ['hash-link-scroll-offset.css'], - + dest: 'assets/css/', ext: '.min.css' } }, watch: { - + styles: { files: ['assets/css/src/*.css'], tasks: ['cssmin'], @@ -92,7 +92,7 @@ module.exports = function( grunt ) { debounceDelay: 500 } }, - + scripts: { files: ['assets/js/src/**/*.js', 'assets/js/vendor/**/*.js'], tasks: ['jshint', 'concat', 'uglify'], @@ -108,13 +108,12 @@ module.exports = function( grunt ) { grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-cssmin'); - + grunt.loadNpmTasks('grunt-contrib-watch'); // Default task. - + grunt.registerTask( 'default', ['jshint', 'concat', 'uglify', 'cssmin'] ); - grunt.util.linefeed = '\n'; }; diff --git a/assets/js/hash-link-scroll-offset.js b/assets/js/hash-link-scroll-offset.js index b769a86..da23e48 100644 --- a/assets/js/hash-link-scroll-offset.js +++ b/assets/js/hash-link-scroll-offset.js @@ -1,4 +1,4 @@ -/*! Hash Link Scroll Offset - v0.1.0 - 2015-04-23 +/*! Hash Link Scroll Offset - v0.1.0 - 2015-05-15 * http://webdevstudios.com * Copyright (c) 2015; * Licensed GPLv2+ */ /*jslint browser: true */ @@ -13,8 +13,12 @@ window.Hash_Link_Scroll_Offset = (function(window, document, $, undefined){ app.offset = app.getOffset(); + // cache jQuery selector results + app.$html_and_body = $('html, body'); + // Handle clicking hash links - $( 'a[href^="#"]:not(.no-scroll)' ).on( 'click', function() { + $( 'a[href^="#"]:not(.no-scroll)' ).on( 'click', function( evt ) { + evt.preventDefault(); app.scrollToHash( this.hash ); }); @@ -53,7 +57,7 @@ window.Hash_Link_Scroll_Offset = (function(window, document, $, undefined){ return; } - $('html, body').stop().animate({ + app.$html_and_body.stop().animate({ 'scrollTop': $element_to_scroll_to.offset().top - app.offset // scroll and offset }, 900 ); diff --git a/assets/js/hash-link-scroll-offset.min.js b/assets/js/hash-link-scroll-offset.min.js index bb1032e..aafc3f9 100644 --- a/assets/js/hash-link-scroll-offset.min.js +++ b/assets/js/hash-link-scroll-offset.min.js @@ -1,4 +1,4 @@ -/*! Hash Link Scroll Offset - v0.1.0 - 2015-04-23 +/*! Hash Link Scroll Offset - v0.1.0 - 2015-05-15 * http://webdevstudios.com * Copyright (c) 2015; * Licensed GPLv2+ */ -window.Hash_Link_Scroll_Offset=function(o,t,n){"use strict";var s={};return s.init=function(){s.offset=s.getOffset(),n('a[href^="#"]:not(.no-scroll)').on("click",function(){s.scrollToHash(this.hash)}),setTimeout(function(){o.location.hash&&(o.scrollTo(0,0),s.scrollToHash(o.location.hash))},10)},s.getOffset=function(){var t=o.hlso_offset||0;return n("#wpadminbar").length&&(t=""+(parseInt(t,10)+32)),t},s.scrollToHash=function(o){var t=n(o);t.length||(t=n('[name="'+o.substr(1)+'"]')),t.length&&n("html, body").stop().animate({scrollTop:t.offset().top-s.offset},900)},n(t).ready(s.init),s}(window,document,jQuery); \ No newline at end of file +window.Hash_Link_Scroll_Offset=function(t,o,n){"use strict";var s={};return s.init=function(){s.offset=s.getOffset(),s.$html_and_body=n("html, body"),n('a[href^="#"]:not(.no-scroll)').on("click",function(t){t.preventDefault(),s.scrollToHash(this.hash)}),setTimeout(function(){t.location.hash&&(t.scrollTo(0,0),s.scrollToHash(t.location.hash))},10)},s.getOffset=function(){var o=t.hlso_offset||0;return n("#wpadminbar").length&&(o=""+(parseInt(o,10)+32)),o},s.scrollToHash=function(t){var o=n(t);o.length||(o=n('[name="'+t.substr(1)+'"]')),o.length&&s.$html_and_body.stop().animate({scrollTop:o.offset().top-s.offset},900)},n(o).ready(s.init),s}(window,document,jQuery); \ No newline at end of file diff --git a/assets/js/src/hash-link-scroll-offset.js b/assets/js/src/hash-link-scroll-offset.js index ed610da..607fe78 100644 --- a/assets/js/src/hash-link-scroll-offset.js +++ b/assets/js/src/hash-link-scroll-offset.js @@ -18,8 +18,12 @@ window.Hash_Link_Scroll_Offset = (function(window, document, $, undefined){ app.offset = app.getOffset(); + // cache jQuery selector results + app.$html_and_body = $('html, body'); + // Handle clicking hash links - $( 'a[href^="#"]:not(.no-scroll)' ).on( 'click', function() { + $( 'a[href^="#"]:not(.no-scroll)' ).on( 'click', function( evt ) { + evt.preventDefault(); app.scrollToHash( this.hash ); }); @@ -58,7 +62,7 @@ window.Hash_Link_Scroll_Offset = (function(window, document, $, undefined){ return; } - $('html, body').stop().animate({ + app.$html_and_body.stop().animate({ 'scrollTop': $element_to_scroll_to.offset().top - app.offset // scroll and offset }, 900 ); diff --git a/hash-link-scroll-offset.php b/hash-link-scroll-offset.php index 13bacd6..2028304 100644 --- a/hash-link-scroll-offset.php +++ b/hash-link-scroll-offset.php @@ -3,7 +3,7 @@ * Plugin Name: Hash Link Scroll Offset * Plugin URI: http://webdevstudios.com * Description: Offset the scroll position of anchored links. Handy if you have a sticky header that covers linked material. - * Version: 0.1.2 + * Version: 0.1.3 * Author: WebDevStudios * Author URI: http://webdevstudios.com * Donate link: http://webdevstudios.com diff --git a/readme.txt b/readme.txt index 37b2927..c534abc 100644 --- a/readme.txt +++ b/readme.txt @@ -3,8 +3,8 @@ Contributors: webdevstudios, jtsternberg Donate link: http://webdevstudios.com Tags: Requires at least: 3.6.0 -Tested up to: 3.6.0 -Stable tag: 0.1.2 +Tested up to: 4.2.2 +Stable tag: 0.1.3 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -32,6 +32,9 @@ Use the `no-scroll` class on any hash links that are not meant to scroll to an a == Changelog == += 0.1.3 = +* Prevent browser windows from moving/blinking on initial click of anchor link. Props [@ImBigWill](https://github.com/WebDevStudios/Hash-Link-Scroll-Offset/pull/9). + = 0.1.2 = * Automatically compensate for admin bar. Props [@salcode](https://github.com/WebDevStudios/Hash-Link-Scroll-Offset/pull/5). @@ -43,6 +46,9 @@ Use the `no-scroll` class on any hash links that are not meant to scroll to an a == Upgrade Notice == += 0.1.3 = +* Prevent browser windows from moving/blinking on initial click of anchor link. Props [@ImBigWill](https://github.com/WebDevStudios/Hash-Link-Scroll-Offset/pull/9). + = 0.1.2 = * Automatically compensate for admin bar. Props [@salcode](https://github.com/WebDevStudios/Hash-Link-Scroll-Offset/pull/5).