sitedarts/es6/sitedarts.js

72 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-06-15 23:20:04 +00:00
/*global jQuery */
2011-01-13 22:42:05 +00:00
/*!
* jQuery siteDarts
* Examples and documentation at: http://elfsternberg.com/projects/sitedarts/
* Copyright (c) 2011 E. M. Sternberg
2016-06-15 23:20:04 +00:00
* Version: 0.9
2011-01-13 22:42:05 +00:00
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Requires: jQuery v1.3.2 or later
*/
;(function($) {
$.fn.siteDarts = function(options) {
2016-06-15 23:20:04 +00:00
return this.each(() => {
options = $.extend(true, {}, $.fn.siteDarts.defaults, options);
var names = $('a[name]');
2011-01-13 22:42:05 +00:00
2016-06-15 23:20:04 +00:00
var reposition = () => {
2011-01-13 22:42:05 +00:00
var prop = $(window).height();
var doch = $(document).height();
names.each(function(dex) {
2016-06-15 23:20:04 +00:00
var n = $(this).attr('name');
2011-01-13 22:42:05 +00:00
var d = $(this).offset().top;
2016-06-15 23:20:04 +00:00
$("li." + options.markitem + ' a[href="#' + n + '"]')
.parent()
.animate({top: prop * (d / doch) - dex * 7});
2011-01-13 22:42:05 +00:00
});
2016-06-15 23:20:04 +00:00
};
2011-01-13 22:42:05 +00:00
2016-06-15 23:20:04 +00:00
var buildDarts = () => {
2011-01-13 22:42:05 +00:00
if ($('#' + options.marklist)) {
2016-06-15 23:20:04 +00:00
$('#' + options.marklist).remove();
2011-01-13 22:42:05 +00:00
}
var l = $('<ul id="' + options.marklist + '"></ul>').appendTo('body');
names.each(function() {
2011-01-13 22:42:05 +00:00
var o = $('<li class="' + options.markitem + '"><a href="#' +
$(this).attr('name') + '">' + $(this).text() + '</a></li>').appendTo(l);
2016-06-15 23:20:04 +00:00
2011-01-13 22:42:05 +00:00
$('a', o).click(function(event) {
event.preventDefault();
2016-06-15 23:20:04 +00:00
var target = $(event.target).attr('href').replace('#', '');
2011-01-13 22:42:05 +00:00
var targetOffset = $('a[name="' + target + '"]').offset().top;
2016-06-15 23:20:04 +00:00
$('html body').animate(
{
scrollTop: targetOffset
},
{
duration: 800,
easing: options.easing,
complete: function() {
window.location.hash = target;
}
});
2011-01-13 22:42:05 +00:00
});
});
2016-06-15 23:20:04 +00:00
};
2011-01-13 22:42:05 +00:00
buildDarts();
reposition();
});
};
$.fn.siteDarts.defaults = {
marklist: 'marklist',
markitem: 'marklistitem',
easing: 'easeOutQuint'
2011-01-13 22:42:05 +00:00
};
})(jQuery);