-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-menuscrollspy.js
129 lines (106 loc) · 3.76 KB
/
jquery-menuscrollspy.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Scroll Spy jQuery Plugin for your Menus
* This watches the window scroll and fires events when elements are scrolled into viewport.
*
* throttle() and getTime() taken from Underscore.js
* https://github.com/jashkenas/underscore
*
* @author Copyright 2017 Zeshan Ahmed
* @author-uri http://www.zeshanahmed.com/
* @license https://raw.github.com/thesmart/jquery-scrollspy/master/LICENSE
* @package https://github.com/zeshanshani/menuScrollSpy
* @version 0.0.1
*/
(function($) {
$.fn.menuScrollSpy = function( options ) {
// Plugin Options
var settings = $.extend({
offsetTop: 0,
activeClassName: 'active'
}, options);
// Plugin Settings
var defaults = {
offsetTop: settings.offsetTop
};
console.log( settings.offsetTop, defaults.offsetTop );
// Check if offsetTop is string
// If it is, then search for the elements on page with that selector.
if ( typeof settings.offsetTop === 'string' ) {
defaults.offsetTop = 0;
// search for the element on page.
if ( $(settings.offsetTop).length > 0 ) {
// Check if the elements are more then 1.
if ( $(settings.offsetTop).length > 1 ) {
// Run each loop to add each element's height in the
// offsetTop.
$(settings.offsetTop).each(function(i, el) {
console.log( $(this).outerHeight() );
defaults.offsetTop += $(this).outerHeight();
});
} else {
// If the length is 1, add the main element's height to the
// offsetTop.
defaults.offsetTop = $(settings.offsetTop).outerHeight();
}
}
}
console.log( settings.offsetTop, defaults.offsetTop );
var $window = $(window),
$menu,
$menuLinks,
$link,
$firstLink,
$firstSection,
$section;
return this.each(function() {
$menu = $(this);
$menuLinks = $menu.find('a[href^="#"]');
$window.scroll(function() {
menuLinks();
});
});
// Scroll Function
function menuLinks() {
$menuLinks.each(function(i, el) {
if ( typeof $firstLink === 'undefined' && i === 0 ) {
$firstLink = $(this);
var firstLinkHref = $firstLink.attr('href');
if ( $(firstLinkHref).length ) {
$firstSection = $(firstLinkHref);
}
console.log( $firstLink, $firstSection );
}
$link = $(this);
var sectionID = $link.attr('href');
// Check if link has # href and is not only character
// if ( href.indexOf('#') === 0 && href !== '#' && sectionID ) {
// Store the link href and check if element with this ID exists on the page.
if ( $(sectionID).length > 0 ) {
$section = $(sectionID);
console.log( 'link has it.' );
// runScrollFunction();
if (
$window.scrollTop() >= $section.offset().top - defaults.offsetTop
&& $window.scrollTop() <= ( $section.offset().top + $section.outerHeight() ) - defaults.offsetTop
) {
console.log( $window.scrollTop(), $section.offset().top, defaults.offsetTop );
console.log( $section, $link, $menu, $menuLinks );
console.log( 'window exceeds it...' );
if ( ! $link.hasClass('active') ) {
$menuLinks.removeClass('active');
$link.addClass('active');
}
} else {
if ( $link.hasClass('active') ) {
$link.addClass('active');
}
}
if ( $window.scrollTop() <= $firstSection.offset().top - defaults.offsetTop ) {
console.log( 'removing class' );
$menuLinks.removeClass('active');
}
}
});
}
}
}(jQuery));