-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.plugin.animated-anchor.js
100 lines (86 loc) · 3.03 KB
/
jquery.plugin.animated-anchor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* =============================================================
* animated-anchor plugin v0.2
* http://
* =============================================================
* Copyright 2012 Steven Wirges, Inc.
*
* Licensed under the MIT License
*
*
* // HOW TO USE
*
* Example :
* $('.anchor').animatedanchor();
*
* Options:
*
* Available options and their defaults:
*
* offset:114,
* delay:1000,
* easing:"linear",
*
* $('.anchor').animatedanchor({option1, 'value'});
*
*
* Methods:
* scrollTo(element)
* ============================================================== */
!function ($) {
"use strict"; // jshint ;_;
var methods = {
init : function (options) {
$.fn.animatedanchor.options = $.extend({}, $.fn.animatedanchor.defaults, options);
var options = $.fn.animatedanchor.options;
var hash = window.location.hash;
if(hash != "") {
hash = hash.replace('#_', '#');
methods.scrollTo($(hash));
} else {
$('html,body').animate({scrollTop: 0}, options.delay, options.easing);
}
return this.each(function(){
$(this).bind('click', methods.click);
});
},
click : function () {
if (location.pathname.replace(/^\//,'') === this.pathname.replace(/^\//,'')
&& location.hostname === this.hostname) {
var options = $.fn.animatedanchor.options;
var $target =$(this.hash)
$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
if ($target.length) {
var targetOffset = $target.offset().top - options.offset;
window.location.hash = "#_" + $($target).attr('id');
$('html,body').animate({scrollTop: targetOffset}, options.delay, options.easing);
return false;
}
}
},
// redundant because of performance
scrollTo :function (element) {
var options = $.fn.animatedanchor.options;
var $target = $(element);
if ($target.length) {
var targetOffset = $target.offset().top - options.offset;
$('html,body').animate({scrollTop: targetOffset}, options.delay, options.easing);
return false;
}
}
};
$.fn.animatedanchor = function ( method ) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.animated-anchor' );
}
};
$.fn.animatedanchor.defaults = {
offset:114,
delay:1000,
easing:"linear",
};
$.fn.animatedanchor.options = {};
}(window.jQuery);