forked from LiranCohen/stickUp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stickUp.js
122 lines (113 loc) · 3.42 KB
/
stickUp.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
jQuery(
function($) {
$(document).ready(function(){
var contentButton = [];
var contentTop = [];
var content = [];
var lastScrollTop = 0;
var scrollDir = '';
var itemClass = '';
var itemHover = '';
var menuSize = null;
var stickyHeight = 0;
var stickyMarginB = 0;
var currentMarginT = 0;
var topMargin = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
scrollDir = 'down';
} else {
scrollDir = 'up';
}
lastScrollTop = st;
});
$.fn.stickUp = function( options ) {
// adding a class to users div
$(this).addClass('stuckMenu');
//getting options
var objn = 0;
if(options != null) {
for(var o in options.parts) {
if (options.parts.hasOwnProperty(o)){
content[objn] = options.parts[objn];
objn++;
}
}
if(objn == 0) {
console.log('error:needs arguments');
}
itemClass = options.itemClass;
itemHover = options.itemHover;
if(options.topMargin != null) {
if(options.topMargin == 'auto') {
topMargin = parseInt($('.stuckMenu').css('margin-top'));
} else {
if(isNaN(options.topMargin) && options.topMargin.search("px") > 0){
topMargin = parseInt(options.topMargin.replace("px",""));
} else if(!isNaN(parseInt(options.topMargin))) {
topMargin = parseInt(options.topMargin);
} else {
console.log("incorrect argument, ignored.");
topMargin = 0;
}
}
} else {
topMargin = 0;
}
menuSize = $('.'+itemClass).size();
}
stickyHeight = parseInt($(this).height());
stickyMarginB = parseInt($(this).css('margin-bottom'));
currentMarginT = parseInt($(this).next().closest('div').css('margin-top'));
vartop = parseInt($(this).offset().top);
//$(this).find('*').removeClass(itemHover);
}
$(document).on('scroll', function() {
varscroll = parseInt($(document).scrollTop());
if(menuSize != null){
for(var i=0;i < menuSize;i++)
{
contentTop[i] = $('#'+content[i]+'').offset().top;
function bottomView(i) {
contentView = $('#'+content[i]+'').height()*.4;
testView = contentTop[i] - contentView;
//console.log(varscroll);
if(varscroll > testView){
$('.'+itemClass).removeClass(itemHover);
$('.'+itemClass+':eq('+i+')').addClass(itemHover);
} else if(varscroll < 50){
$('.'+itemClass).removeClass(itemHover);
$('.'+itemClass+':eq(0)').addClass(itemHover);
}
}
if(scrollDir == 'down' && varscroll > contentTop[i]-50 && varscroll < contentTop[i]+50) {
$('.'+itemClass).removeClass(itemHover);
$('.'+itemClass+':eq('+i+')').addClass(itemHover);
}
if(scrollDir == 'up') {
bottomView(i);
}
}
}
if(vartop < varscroll + topMargin){
$('.stuckMenu').addClass('isStuck');
$('.stuckMenu').next().closest('div').css({
'margin-top': stickyHeight + stickyMarginB + currentMarginT + 'px'
}, 10);
$('.stuckMenu').css("position","fixed");
$('.isStuck').css({
top: '0px'
}, 10, function(){
});
};
if(varscroll + topMargin < vartop){
$('.stuckMenu').removeClass('isStuck');
$('.stuckMenu').next().closest('div').css({
'margin-top': currentMarginT + 'px'
}, 10);
$('.stuckMenu').css("position","relative");
};
});
});
});