-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathanchor.js
111 lines (95 loc) · 2.8 KB
/
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
101
102
103
104
105
106
107
108
109
110
111
/**
* anchor.js - jQuery Plugin
* Jump to a specific section smoothly
*
* @dependencies jQuery v1.5.0 http://jquery.com
* @author Cornel Boppart <[email protected]>
* @copyright Author
* @version 1.0.6 (06/01/2016)
*/
;(function ($) {
window.anchor = {
/**
* Default settings
*
*/
settings: {
transitionDuration: 2000,
transitionTimingFunction: 'swing',
labels: {
error: 'Couldn\'t find any section'
}
},
/**
* Initializes the plugin
*
* @param {object} options The plugin options (Merged with default settings)
* @return {object} this The current element itself
*/
init: function (options) {
// Apply merged settings to the current object
$(this).data('settings', $.extend(anchor.settings, options));
return this.each(function () {
var $this = $(this);
$this.unbind('click').click(function (event) {
event.preventDefault();
anchor.jumpTo(
anchor.getTopOffsetPosition($this),
$this.data('settings')
);
});
});
},
/**
* Gets the top offset position
*
* @param {object} $object The root object to get sections position from
* @return {int} topOffsetPosition The top offset position
*/
getTopOffsetPosition: function ($object) {
var href = $object.attr('href'),
$section = $($(href).get(0)),
documentHeight = $(document).height(),
browserHeight = $(window).height();
if (!$section || $section.length < 1) {
throw new ReferenceError(anchor.settings.labels.error);
}
if (($section.offset().top + browserHeight) > documentHeight) {
return documentHeight - browserHeight;
} else {
return $section.offset().top;
}
},
/**
* Jumps to the specific position
*
* @param {int} topOffsetPosition The top offset position
* @param {object} settings The object specific settings
* @return {void}
*/
jumpTo: function (topOffsetPosition, settings) {
var $viewport = $('html, body');
$viewport.animate(
{scrollTop: topOffsetPosition},
settings.transitionDuration,
settings.transitionTimingFunction
);
// Stop the animation immediately, if a user manually scrolls during the animation.
$viewport.bind('scroll mousedown DOMMouseScroll mousewheel keyup', function(event){
if (event.which > 0 || event.type === 'mousedown' || event.type === 'mousewheel') {
$viewport.stop().unbind('scroll mousedown DOMMouseScroll mousewheel keyup');
}
});
}
};
$.fn.anchor = function (method) {
// Method calling logic
if (anchor[method]) {
return anchor[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return anchor.init.apply(this, arguments);
} else {
return $.error('Method ' + method + ' does not exist on jQuery.anchor');
}
};
})(jQuery);