Skip to content

Instantly share code, notes, and snippets.

@jervert
Created November 14, 2013 11:48
Show Gist options
  • Save jervert/7465527 to your computer and use it in GitHub Desktop.
Save jervert/7465527 to your computer and use it in GitHub Desktop.
/*
* SliceAnchor - jQuery plugin for fake anchors
*
* Copyright (c) 2012 Antonio Rodríguez Ruiz
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://outbook.es
*
* Version: 1.0.0
*
* params:
* options {
* attrSelector: [String with anchor selector attribute]
* animateTime: [Integer with animation time in miliseconds]
* }
*
* html:
* <span role="link" tabindex="0" data-navigate-anchor="#destination-selector">[...]</span>
*
* initialize:
* $('.fake-link-selector').sliceAnchor({attrSelector: 'data-navigate-anchor', animateTime: 1000})
*
*/
$.fn.extend({
sliceAnchor: function (data) {
var defaults = {
attrSelector: 'data-navigate-anchor',
animationTime: 50
},
options = $.extend(true, defaults, data),
navigateAnchor = function (event) {
var destinationSelector = $(event.currentTarget).attr(options.attrSelector),
$destination = $(destinationSelector);
$('html, body').animate({
scrollTop: $destination.offset().top
}, options.animationTime, function () {
$destination.focus();
});
};
return this.each(function () {
$(this).on('click.navigateAnchor', navigateAnchor);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment