Repository navigation
Expand file tree
/
Copy pathjquery.creep.js
More file actions
140 lines (93 loc) · 3.07 KB
/
Copy pathjquery.creep.js
File metadata and controls
140 lines (93 loc) · 3.07 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Creep.js
* http://creepjs.com
* Author: James Pederson (jpederson.com)
* Licensed under the MIT, GPL licenses.
* Version: 1.1.0
*/
;(function( $, window, document, undefined ){
// let's start our plugin logic
$.extend($.fn, {
// Creep when you click the selected element(s)
creep: function( options ){
// set our options from the defaults, overriding with the
// parameter we pass into this function.
options = $.extend( {}, $.fn.creep.options, options );
// Iterate through all the matching elements and return
// the jquery object to preserve chaining.
return this.each(function(){
// Store a jQuery object for our element so we can use it
// inside our other bindings.
var elem = $(this);
// bind to click event
elem.click(function( event ) {
var href = elem.attr("href"),
href_trimmed = href.replace( "#", "" );
// only do this if it's an anchor link
if ( href.match( "#" ) && href !== "#" && !href.match( "http" ) ) {
// prevent default click propagation
event.preventDefault();
// do the scroll
creepToElement( href_trimmed, options );
// fallback to prevent jitter
return false;
}
});
});
},
// Creep to the selected element
creepTo: function( options ){
// set our options from the defaults, overriding with the
// parameter we pass into this function.
options = $.extend( {}, $.fn.creep.options, options );
// Iterate through all the matching elements and return
// the jquery object to preserve chaining.
return this.each(function(){
// Store a jQuery object for our element so we can use it
// inside our other bindings.
var elem = $(this);
// bind to click event
elem.click(function( event ) {
var href = elem.attr("href"),
href_trimmed = href.replace( "#", "" );
// only do this if it's an anchor link
if ( href.match( "#" ) && href !== "#" && !href.match( "http" ) ) {
// prevent default click propagation
event.preventDefault();
// creep to the element
creepToElement( href_trimmed, options );
// fallback to prevent jitter
return false;
}
});
});
}
});
// scroll handler
var creepToElement = function( id, options ) {
// grab the element to scroll to based on the name
var dest = $("a[name='"+ id +"']");
// if that didn't work, look for an element with our ID
if ( typeof( dest.offset() ) === "undefined" ) {
dest = $("#"+id);
}
// if the destination element exists
if ( typeof( dest.offset() ) !== "undefined" ) {
// do the scroll
$('html, body').animate({
scrollTop: dest.offset().top + options.offset
}, options.speed );
// if we have pushState
if ( history.pushState ) {
history.pushState( null, null, '#'+id );
}
}
};
// DEFAULTS
// Set up some default options for our plugin that can be overridden
// as needed when we actually instantiate our plugin link elements.
$.fn.creep.options = {
offset: 0,
speed: 1000
};
})( jQuery, window, document );